Created
February 9, 2012 01:16
-
-
Save bryanchow/1776202 to your computer and use it in GitHub Desktop.
Django view for serving files using X-Accel-Redirect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://gist.github.com/1776202 | |
from os.path import join | |
from django.http import HttpResponse | |
from django.views.static import serve | |
from django.conf import settings | |
USE_X_ACCEL_REDIRECT = getattr(settings, 'USE_X_ACCEL_REDIRECT', False) | |
X_ACCEL_REDIRECT_PREFIX = getattr(settings, 'X_ACCEL_REDIRECT_PREFIX', '') | |
def serve_media(request, file_path): | |
""" | |
Django view that serves a media file using X_ACCEL_REDIRECT_PREFIX if | |
USE_X_ACCEL_REDIRECT is enabled in settings, or django.views.static.serve | |
if not (for development and testing). | |
""" | |
if USE_X_ACCEL_REDIRECT: | |
response = HttpResponse('') | |
response['X-Accel-Redirect'] = join('/', X_ACCEL_REDIRECT_PREFIX, file_path) | |
response['Content-Type'] = '' | |
else: | |
response = serve(request, file_path, settings.MEDIA_ROOT) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment