Merge pull request #327 from kuhnchris/dev-file-plrovider
(dev only) allow file download directly from django
This commit is contained in:
commit
b3f93ddef7
|
@ -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 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)
|
||||||
|
@ -694,11 +696,38 @@ 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 = {
|
# development direct file stream - DO NOT USE PRODUCTIVLY
|
||||||
'Content-Type': self.object.content_type,
|
if settings.DEBUG and 'runserver' in sys.argv:
|
||||||
'X-Accel-Redirect': self.object.media_file.url,
|
# get media URL
|
||||||
}
|
pth = self.object.media_file.url
|
||||||
return HttpResponse(headers=headers)
|
# remove "/media-data/"
|
||||||
|
pth = pth.split("/media-data/",1)[1]
|
||||||
|
# remove "/" (incase of absolute path)
|
||||||
|
pth = pth.split(str(settings.DOWNLOAD_ROOT).lstrip("/"),1)
|
||||||
|
|
||||||
|
# if we do not have a "/" at the beginning, it is not a absolute path...
|
||||||
|
if len(pth) > 1:
|
||||||
|
pth = pth[1]
|
||||||
|
else:
|
||||||
|
pth = pth[0]
|
||||||
|
|
||||||
|
|
||||||
|
# build final path
|
||||||
|
filepth = pathlib.Path(str(settings.DOWNLOAD_ROOT) + pth)
|
||||||
|
|
||||||
|
if filepth.exists():
|
||||||
|
# return file
|
||||||
|
response = FileResponse(open(filepth,'rb'))
|
||||||
|
return response
|
||||||
|
else:
|
||||||
|
return HttpResponseNotFound()
|
||||||
|
|
||||||
|
else:
|
||||||
|
headers = {
|
||||||
|
'Content-Type': self.object.content_type,
|
||||||
|
'X-Accel-Redirect': self.object.media_file.url,
|
||||||
|
}
|
||||||
|
return HttpResponse(headers=headers)
|
||||||
|
|
||||||
|
|
||||||
class TasksView(ListView):
|
class TasksView(ListView):
|
||||||
|
|
Loading…
Reference in New Issue