How to set up a custom response header for static (public) pylons files?

How to add a custom header to files that pylons are used publicly?

+3
python pylons
source share
4 answers

a) Let your web server serve files from / public instead of paster and configure it to pass some special headers.

b) Add a special route and execute the files themselves

class FilesController(BaseController): def download(self, path) fapp = FileApp( path, headers=self.get_headers(path) ) return fapp(request.environ, self.start_response) 

c) There may be a way to overwrite the headers, and I just don't know how to do this.

+2
source share

Using the latest version of the route, you can use the Magic path_info 'function and follow the documentation from here to write your controller so it calls paster.DirectoryApp.

In my project, I wanted to serve any file in a public directory, including sub-folders, and ended this up as a controller to override the content_type:

 import logging from paste.fileapp import FileApp from paste.urlparser import StaticURLParser from pylons import config from os.path import basename class ForceDownloadController(StaticURLParser): def __init__(self, directory=None, root_directory=None, cache_max_age=None): if not directory: directory = config['pylons.paths']['static_files'] StaticURLParser.__init__(self, directory, root_directory, cache_max_age) def make_app(self, filename): headers = [('Content-Disposition', 'filename=%s' % (basename(filename)))] return FileApp(filename, headers, content_type='application/octetstream') 
0
source share

In the standard Pylons setup, public files are served from StaticUrlParser. This is usually configured in your /middleware.py configuration: make_app () function

You need to subclass StaticUrlParser as described by Antonin ENFRUN, although the call to his Controller is confused because it serves a different purpose. Add the following at the top of config / middleware.py:

 from paste.fileapp import FileApp from paste.urlparser import StaticURLParser class HeaderUrlParser(StaticURLParser): def make_app(self, filename): headers = # your headers here return FileApp(filename, headers, content_type='application/octetstream') 

then replace StaticUrlParser in config / middleware.py: make_app () with HeaderUrlParser

  static_app = StaticURLParser(config['pylons.paths']['static_files']) 

becomes

  static_app = HeaderURLParser(config['pylons.paths']['static_files']) 
0
source share

The easiest way to use FileApp for pylon- based streaming. The code below assumes your route provides some_file_identifier , but the other two variables are β€œmagic” (see explanation after the code).

 class MyFileController(BaseController): def serve(self, environ, start_response, some_file_identifier): path = self._convert_id_to_path(some_file_identifier) app = FileApp(path) return app(environ, start_response) 

Pylons automatically issue wsgi environ and start_response if you have variables of these names in your method signature. Otherwise, you do not need to set or create header headers, but if you do, you can use the capabilities built into FileApp to achieve this.

0
source share

All Articles