Skip to content

Commit

Permalink
Add support of nginx X-Accel-Redirect header
Browse files Browse the repository at this point in the history
  • Loading branch information
andreymal committed Mar 31, 2019
1 parent 1765fcf commit d38ead1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions mini_fiction/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ class Config(object):

STORY_DOWNLOAD_SLUGIFY_FILENAMES = False

ACCEL_REDIRECT_HEADER = None # set 'X-Accel-Redirect' if you use nginx

CONTACTS = [
{
'name': 'website',
Expand Down
14 changes: 9 additions & 5 deletions mini_fiction/views/story.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,15 @@ def download(story_id, filename):

# TODO: delete file if story was deleted
if not debug:
# FIXME: fix security before sending static file with nginx
# TODO: for example, /media/stories/{md5(updated + secret_random_story_key)}/filename.zip
# return redirect(path to media)
response = send_file(storage_path)
response.headers['Content-Type'] = fmt.content_type
if current_app.config.get('ACCEL_REDIRECT_HEADER'):
response = Response(b'', content_type=fmt.content_type)
response.headers[current_app.config['ACCEL_REDIRECT_HEADER']] = url_for(
'media', filename='stories/{}/{}'.format(story.id, full_filename)
)
else:
response = send_file(storage_path)
response.headers['Content-Type'] = fmt.content_type

response.headers['Content-Disposition'] = 'attachment'

else:
Expand Down

0 comments on commit d38ead1

Please sign in to comment.