Skip to content

Instantly share code, notes, and snippets.

@hersonls
Created February 13, 2013 14:44
Show Gist options
  • Save hersonls/4945051 to your computer and use it in GitHub Desktop.
Save hersonls/4945051 to your computer and use it in GitHub Desktop.
Middleware for django-mediagenerator to fix cache with development mode
from mediagenerator.settings import DEV_MEDIA_URL, MEDIA_DEV_MODE
# Only load other dependencies if they're needed
if MEDIA_DEV_MODE:
from mediagenerator.utils import _refresh_dev_names, _backend_mapping
from django.http import HttpResponse, Http404
from django.utils.cache import patch_cache_control
from django.utils.http import http_date
import time
_REFRESH_DEV_NAMES_DONE = False
class MediaMiddleware(object):
"""
Middleware for serving and browser-side caching of media files.
This MUST be your *first* entry in MIDDLEWARE_CLASSES. Otherwise, some
other middleware might add ETags or otherwise manipulate the caching
headers which would result in the browser doing unnecessary HTTP
roundtrips for unchanged media.
"""
MAX_AGE = 60 * 60 * 24 * 365
def process_request(self, request):
if not MEDIA_DEV_MODE:
return
# We refresh the dev names only once for the server execution, so all
# media_url() calls are cached.
global _REFRESH_DEV_NAMES_DONE
if not _REFRESH_DEV_NAMES_DONE:
_refresh_dev_names()
_REFRESH_DEV_NAMES_DONE = True
if not request.path.startswith(DEV_MEDIA_URL):
return
filename = request.path[len(DEV_MEDIA_URL):]
try:
backend = _backend_mapping[filename]
except KeyError:
raise Http404('No such media file "%s"' % filename)
content, mimetype = backend.get_dev_output(filename)
if isinstance(content, unicode):
content = content.encode('utf-8')
response = HttpResponse(content, content_type=mimetype)
response['Content-Length'] = len(content)
# Cache manifest files MUST NEVER be cached or you'll be unable to update
# your cached app!!!
if response['Content-Type'] != 'text/cache-manifest' and \
response.status_code == 200:
patch_cache_control(response, public=True, max_age=self.MAX_AGE)
response['Expires'] = http_date(time.time() + self.MAX_AGE)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment