<s> from . settings import DEV_MEDIA_URL , MEDIA_DEV_MODE <EOL> if MEDIA_DEV_MODE : <EOL> from . utils import _refresh_dev_names , _backend_mapping <EOL> from django . http import HttpResponse , Http404 <EOL> from django . utils . cache import patch_cache_control <EOL> from django . utils . http import http_date <EOL> import time <EOL> TEXT_MIME_TYPES = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class MediaMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> MAX_AGE = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> def process_request ( self , request ) : <EOL> if not MEDIA_DEV_MODE : <EOL> return <EOL> _refresh_dev_names ( ) <EOL> if not request . path . startswith ( DEV_MEDIA_URL ) : <EOL> return <EOL> filename = request . path [ len ( DEV_MEDIA_URL ) : ] <EOL> try : <EOL> backend = _backend_mapping [ filename ] <EOL> except KeyError : <EOL> raise Http404 ( '<STR_LIT>' <EOL> % filename ) <EOL> content , mimetype = backend . get_dev_output ( filename ) <EOL> if not mimetype : <EOL> mimetype = '<STR_LIT>' <EOL> if isinstance ( content , unicode ) : <EOL> content = content . encode ( '<STR_LIT:utf-8>' ) <EOL> if mimetype . startswith ( '<STR_LIT>' ) or mimetype in TEXT_MIME_TYPES : <EOL> mimetype += '<STR_LIT>' <EOL> response = HttpResponse ( content , content_type = mimetype ) <EOL> response [ '<STR_LIT>' ] = len ( content ) <EOL> if response [ '<STR_LIT:Content-Type>' ] != '<STR_LIT>' and response . status_code == <NUM_LIT:200> : <EOL> patch_cache_control ( response , public = True , max_age = self . MAX_AGE ) <EOL> response [ '<STR_LIT>' ] = http_date ( time . time ( ) + self . MAX_AGE ) <EOL> return response </s>
