Reputation: 3875
I'm building a small web-app with cherrypy.
In this app, I need to serve files in two different ways:
application/octet-stream
mime type, to force the download.Currently, I added an exposed method to stream files, one at /document/xx
, returning the correct mime type, the other one at /download/xx
with the octet-stream mime type.
But I want to avoid coding this myself. It can only bring bugs and security issues.
tl;dr: How can I force cherrypy's tools.staticdir
to force download?
See the full code of the app on github: https://github.com/aspyct/docrepo (note that it's still using the old 'config.ini' file, no config dictionary).
Upvotes: 0
Views: 1983
Reputation: 25194
You can force it by providing content_types
to the tool, that maps file extensions to MIME types. Like this.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cherrypy
path = os.path.abspath(os.path.dirname(__file__))
config = {
'global' : {
'server.socket_host' : '127.0.0.1',
'server.socket_port' : 8080,
'server.thread_pool' : 8
},
'/static' : {
'tools.staticdir.on' : True,
'tools.staticdir.dir' : os.path.join(path, 'static'),
'tools.staticdir.content_types' : {'html': 'application/octet-stream'}
}
}
if __name__ == '__main__':
cherrypy.quickstart(config = config)
If you don't know the extension beforehand, take a look at the tool's source code. There's barely two dozen of effective lines of code. Just make your own fine-tuned tool of the purpose.
Upvotes: 4