use pathlib; .exists() check
This commit is contained in:
parent
b719fd5122
commit
598ee2bd0a
|
@ -1,8 +1,10 @@
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
from base64 import b64decode
|
from base64 import b64decode
|
||||||
|
import pathlib
|
||||||
|
import sys
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.http import FileResponse, Http404
|
from django.http import FileResponse, Http404, HttpResponseNotFound
|
||||||
from django.views.generic import TemplateView, ListView, DetailView
|
from django.views.generic import TemplateView, ListView, DetailView
|
||||||
from django.views.generic.edit import (FormView, FormMixin, CreateView, UpdateView,
|
from django.views.generic.edit import (FormView, FormMixin, CreateView, UpdateView,
|
||||||
DeleteView)
|
DeleteView)
|
||||||
|
@ -677,13 +679,8 @@ class MediaContent(DetailView):
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
def dispatch(self, request, *args, **kwargs):
|
||||||
self.object = self.get_object()
|
self.object = self.get_object()
|
||||||
headers = {
|
|
||||||
'Content-Type': self.object.content_type,
|
|
||||||
'X-Accel-Redirect': self.object.media_file.url,
|
|
||||||
}
|
|
||||||
|
|
||||||
# development direct file stream - DO NOT USE PRODUCTIVLY
|
# development direct file stream - DO NOT USE PRODUCTIVLY
|
||||||
if settings.PROVIDE_FILES_DIRECTLY:
|
if settings.DEBUG and 'runserver' in sys.argv:
|
||||||
# get media URL
|
# get media URL
|
||||||
pth = self.object.media_file.url
|
pth = self.object.media_file.url
|
||||||
# remove "/media-data/"
|
# remove "/media-data/"
|
||||||
|
@ -697,14 +694,22 @@ class MediaContent(DetailView):
|
||||||
else:
|
else:
|
||||||
pth = pth[0]
|
pth = pth[0]
|
||||||
|
|
||||||
# build final path
|
|
||||||
filepth = str(settings.DOWNLOAD_ROOT) + "/" + pth
|
|
||||||
|
|
||||||
|
# build final path
|
||||||
|
filepth = pathlib.Path(str(settings.DOWNLOAD_ROOT) + pth)
|
||||||
|
|
||||||
|
if filepth.exists():
|
||||||
# return file
|
# return file
|
||||||
response = FileResponse(open(filepth,'rb'))
|
response = FileResponse(open(filepth,'rb'))
|
||||||
return response
|
return response
|
||||||
|
else:
|
||||||
|
return HttpResponseNotFound()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
headers = {
|
||||||
|
'Content-Type': self.object.content_type,
|
||||||
|
'X-Accel-Redirect': self.object.media_file.url,
|
||||||
|
}
|
||||||
return HttpResponse(headers=headers)
|
return HttpResponse(headers=headers)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue