Ticket #547: serve_file.patch
-
tutorial/tut09_files.py
old new 27 27 Use cherrypy.lib.static.serve_file for that; it takes four 28 28 arguments: 29 29 30 serve_file(path, content Type=None, disposition=None, name=None)30 serve_file(path, content_type=None, disposition=None, name=None) 31 31 32 32 Set "name" to the filename that you expect clients to use when they save 33 33 your file. Note that the "name" argument is ignored if you don't also 34 34 provide a "disposition" (usually "attachement"). You can manually set 35 "content Type", but be aware that if you also use the encoding tool, it35 "content_type", but be aware that if you also use the encoding tool, it 36 36 may choke if the file extension is not recognized as belonging to a known 37 Content-Type. Setting the content Type to "application/x-download" works37 Content-Type. Setting the content_type to "application/x-download" works 38 38 in most cases, and should prompt the user with an Open/Save dialog in 39 39 popular browsers. 40 40 -
lib/static.py
old new 13 13 from cherrypy.lib import cptools, http 14 14 15 15 16 def serve_file(path, content Type=None, disposition=None, name=None):16 def serve_file(path, content_type=None, disposition=None, name=None): 17 17 """Set status, headers, and body in order to serve the given file. 18 18 19 The Content-Type header will be set to the content Type arg, if provided.19 The Content-Type header will be set to the content_type arg, if provided. 20 20 If not provided, the Content-Type will be guessed by its extension. 21 21 22 22 If disposition is not None, the Content-Disposition header will be set … … 51 51 response.headers['Last-Modified'] = http.HTTPDate(stat.st_mtime) 52 52 cptools.validate_since() 53 53 54 if content Type is None:54 if content_type is None: 55 55 # Set content-type based on filename extension 56 56 ext = "" 57 57 i = path.rfind('.') 58 58 if i != -1: 59 59 ext = path[i:].lower() 60 content Type = mimetypes.types_map.get(ext, "text/plain")61 response.headers['Content-Type'] = content Type60 content_type = mimetypes.types_map.get(ext, "text/plain") 61 response.headers['Content-Type'] = content_type 62 62 63 63 if disposition is not None: 64 64 if name is None: … … 101 101 def fileRanges(): 102 102 for start, stop in r: 103 103 yield "--" + boundary 104 yield "\nContent-type: %s" % content Type104 yield "\nContent-type: %s" % content_type 105 105 yield ("\nContent-range: bytes %s-%s/%s\n\n" 106 106 % (start, stop - 1, c_len)) 107 107 bodyfile.seek(start) … … 132 132 if content_types: 133 133 r, ext = os.path.splitext(filename) 134 134 content_type = content_types.get(ext[1:], None) 135 serve_file(filename, content Type=content_type)135 serve_file(filename, content_type=content_type) 136 136 return True 137 137 except cherrypy.NotFound: 138 138 # If we didn't find the static file, continue handling the

