Add basic media-handling (#2)

This commit is contained in:
zeitschlag 2023-05-21 14:45:52 +02:00
parent ef4cca21c8
commit c3794a4c9f
3 changed files with 27 additions and 2 deletions

View File

@ -1,3 +1,24 @@
from django.shortcuts import render import mimetypes
import os
from django.http import HttpResponse, FileResponse
from django.conf import settings
# Create your views here. # Create your views here.
def media(request, media_name):
#TODO: Check if user is valid
if settings.DEBUG:
# deliver file from Django
file_path = os.path.join(settings.MEDIA_ROOT, media_name)
mimetype = mimetypes.guess_type(media_name)
response = FileResponse(open(file_path, "rb"))
response["Content-Type"] = mimetype
return response
else:
mimetype = mimetypes.guess_type(image_name)
response = HttpResponse()
response["Content-Type"] = mimetype
response["X-Sendfile"] = os.path.join(settings.MEDIA_ROOT, media_name)
return response

View File

@ -111,7 +111,8 @@ TIME_ZONE = 'UTC'
USE_I18N = True USE_I18N = True
USE_TZ = True USE_TZ = True
MEDIA_ROOT = ""
MEDIA_URL = "media/"
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/ # https://docs.djangoproject.com/en/4.2/howto/static-files/

View File

@ -16,7 +16,10 @@ Including another URLconf
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path
from fan.views import media
urlpatterns = [ urlpatterns = [
path("media/<path:media_name>", media, name="media"),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]