From: Thomas Fillon Date: Tue, 11 Apr 2017 14:20:04 +0000 (+0200) Subject: Merge branch 'feature/html5' into feature/dj1.8 X-Git-Tag: 1.7.0~2^2~21^2~9 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=e92390c2cf5b0250b4d51ab097d07ad07927a317;p=telemeta.git Merge branch 'feature/html5' into feature/dj1.8 Conflicts: telemeta/views/core.py telemeta/views/item.py --- e92390c2cf5b0250b4d51ab097d07ad07927a317 diff --cc telemeta/views/admin.py index c0e84bea,ec579eb6..e4119e31 --- a/telemeta/views/admin.py +++ b/telemeta/views/admin.py @@@ -20,11 -20,11 +20,13 @@@ # Authors: Olivier Guilyardi # Guillaume Pellerin +from django.apps import apps from telemeta.views.core import * + import telemeta.models + + class AdminView(object): """Provide Admin web UI methods""" diff --cc telemeta/views/collection.py index 703b4ee3,d440221a..333a59f2 --- a/telemeta/views/collection.py +++ b/telemeta/views/collection.py @@@ -22,9 -22,9 +22,10 @@@ from telemeta.views.core import * + from telemeta.views.core import serve_media from telemeta.views.epub import * + class CollectionView(object): """Provide Collections web UI methods""" diff --cc telemeta/views/core.py index e6b77f8c,32aa0b81..6857ccae --- a/telemeta/views/core.py +++ b/telemeta/views/core.py @@@ -38,7 -41,8 +41,8 @@@ from django.utils.decorators import met 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 * @@@ -91,59 -95,55 +95,45 @@@ class TelemetaBaseMixin(object) 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: