Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def secured_download(func):
- """Wraps a view to server files securely ONLY when a
- ``FileField`` is returned
- Uses Django when ``DEBUG`` is on else NGINX
- """
- def wrapper(request, *args, **kwargs):
- response = func(request, *args, **kwargs)
- if isinstance(response, FieldFile):
- # response is a ``FieldFile`` we should serve it
- if settings.DEBUG:
- # only serve through Django when DEBUG is on
- stream = serve(request, response.name, settings.MEDIA_ROOT)
- stream['Content-Disposition'] = 'attachment; filename=%s' % \
- response.name
- return stream
- else:
- # Use X-Accel-Redirect to serve the file
- stream = HttpResponse('')
- stream['X-Accel-Redirect'] = response.url
- return stream
- return response
- return wrapper
Add Comment
Please, Sign In to add comment