Guest User

Untitled

a guest
Jan 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. def secured_download(func):
  2. """Wraps a view to server files securely ONLY when a
  3. ``FileField`` is returned
  4. Uses Django when ``DEBUG`` is on else NGINX
  5. """
  6. def wrapper(request, *args, **kwargs):
  7. response = func(request, *args, **kwargs)
  8. if isinstance(response, FieldFile):
  9. # response is a ``FieldFile`` we should serve it
  10. if settings.DEBUG:
  11. # only serve through Django when DEBUG is on
  12. stream = serve(request, response.name, settings.MEDIA_ROOT)
  13. stream['Content-Disposition'] = 'attachment; filename=%s' % \
  14. response.name
  15. return stream
  16. else:
  17. # Use X-Accel-Redirect to serve the file
  18. stream = HttpResponse('')
  19. stream['X-Accel-Redirect'] = response.url
  20. return stream
  21. return response
  22. return wrapper
Add Comment
Please, Sign In to add comment