#NEWLINE# def serve( request , path , document_root = None , show_indexes = False ) : #NEWLINE# #INDENT# "\n    Serve static files below a given point in the directory structure.\n\n    To use, put a URL pattern such as::\n\n        (r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root' : '/path/to/my/files/'})\n\n    in your URLconf. You must provide the ``document_root`` param. You may\n    also set ``show_indexes`` to ``True`` if you'd like to serve a basic index\n    of the directory.  This index view will use the template hardcoded below,\n    but if you'd like to override it, you can create a template called\n    ``static/directory_index.html``.\n    " #NEWLINE# path = posixpath . normpath ( unquote ( path ) ) #NEWLINE# path = path . lstrip ( '/' ) #NEWLINE# newpath = '' #NEWLINE# for part in path . split ( '/' ) : #NEWLINE# #INDENT# if ( not part ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# ( drive , part ) = os . path . splitdrive ( part ) #NEWLINE# ( head , part ) = os . path . split ( part ) #NEWLINE# if ( part in ( os . curdir , os . pardir ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# newpath = os . path . join ( newpath , part ) . replace ( '\\' , '/' ) #NEWLINE# #UNINDENT# if ( newpath and ( path != newpath ) ) : #NEWLINE# #INDENT# return HttpResponseRedirect ( newpath ) #NEWLINE# #UNINDENT# fullpath = os . path . join ( document_root , newpath ) #NEWLINE# if os . path . isdir ( fullpath ) : #NEWLINE# #INDENT# if show_indexes : #NEWLINE# #INDENT# return directory_index ( newpath , fullpath ) #NEWLINE# #UNINDENT# raise Http404 ( _ ( 'Directory indexes are not allowed here.' ) ) #NEWLINE# #UNINDENT# if ( not os . path . exists ( fullpath ) ) : #NEWLINE# #INDENT# raise Http404 ( ( _ ( '"%(path)s" does not exist' ) % { #NEWLINE# #INDENT# 'path' : fullpath , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# #UNINDENT# statobj = os . stat ( fullpath ) #NEWLINE# ( mimetype , encoding ) = mimetypes . guess_type ( fullpath ) #NEWLINE# mimetype = ( mimetype or 'application/octet-stream' ) #NEWLINE# if ( not was_modified_since ( request . META . get ( 'HTTP_IF_MODIFIED_SINCE' ) , statobj . st_mtime , statobj . st_size ) ) : #NEWLINE# #INDENT# return HttpResponseNotModified ( ) #NEWLINE# #UNINDENT# response = CompatibleStreamingHttpResponse ( open ( fullpath , 'rb' ) , content_type = mimetype ) #NEWLINE# response [ 'Last-Modified' ] = http_date ( show_indexes . st_mtime ) #NEWLINE# if stat . S_ISREG ( statobj . st_mode ) : #NEWLINE# #INDENT# response [ 'Content-Length' ] = statobj . st_size #NEWLINE# #UNINDENT# if encoding : #NEWLINE# #INDENT# response [ 'Content-Encoding' ] = encoding #NEWLINE# #UNINDENT# return response