from django.contrib.auth import authenticate, login
from django.template import RequestContext, loader
from django import template
--from django.http import HttpResponse, HttpResponseRedirect, StreamingHttpResponse
-# from django.http import FileResponse -> introduced in Django 1.7.4
++from django.http import HttpResponse, HttpResponseRedirect
++from django.http import FileResponse
from django.http import Http404
from django.shortcuts import render_to_response, redirect, get_object_or_404
from django.views.generic import *
cache_tmp = TelemetaCache(getattr(settings, 'FILE_UPLOAD_TEMP_DIR', os.path.join(MEDIA_ROOT, 'tmp')))
- class FixedFileWrapper(FileWrapper):
+ def serve_media(filename, content_type="", buffering=True):
+ if not settings.DEBUG:
+ return nginx_media_accel(filename, content_type=content_type,
+ buffering=buffering)
+ else:
- response = StreamingHttpResponse(stream_from_file(filename), content_type=content_type)
++ response = FileResponse(open(filename, 'rb'))
+ response['Content-Disposition'] = 'attachment; ' + 'filename=' + filename
+ return response
- def __iter__(self):
- self.filelike.seek(0)
- return self
-
- def send_file(request, filename, content_type='image/jpeg'):
- """
- Send a file through Django without loading the whole file into
- memory at once. The FileWrapper will turn the file object into an
- iterator for chunks of 8KB.
- """
- wrapper = FixedFileWrapper(file(filename, 'rb'))
- response = StreamingHttpResponse(wrapper, content_type=content_type)
- response['Content-Length'] = os.path.getsize(filename)
- return response
-
-
- def nginx_media_accel(request, filename):
- """Send a protected medie file through nginx with X-Accel-Redirect"""
+ def nginx_media_accel(media_path, content_type="", buffering=True):
+ """Send a protected media file through nginx with X-Accel-Redirect"""
response = HttpResponse()
- url = settings.MEDIA_URL + filename
- # let nginx determine the correct content type
- response['Content-Type'] = ""
+ url = settings.MEDIA_URL + os.path.relpath(media_path, settings.MEDIA_ROOT)
+ filename = os.path.basename(media_path)
+ response['Content-Disposition'] = "attachment; filename=%s" % (filename)
+ response['Content-Type'] = content_type
response['X-Accel-Redirect'] = url
+
+ if not buffering:
+ response['X-Accel-Buffering'] = 'no'
+
return response
-def render(request, template, data=None, mimetype=None):
+def render(request, template, data=None, content_type=None):
return render_to_response(template, data, context_instance=RequestContext(request),
- mimetype=mimetype)
+ content_type=content_type)
+
+
def stream_from_processor(decoder, encoder, flag):
pipe = decoder | encoder
for chunk in pipe.stream():
yield chunk
flag.value = True
flag.save()
--
--
--def stream_from_file(file):
-- chunk_size = 0x100000
-- f = open(file, 'r')
-- while True:
-- chunk = f.read(chunk_size)
-- if not len(chunk):
-- f.close()
-- break
-- yield chunk
--
++
def get_item_access(item, user):
# Item access rules according to this workflow: