Hi, I'm ThadeusB.

I code, I play, I love, I innovate

Let the web server stream your files, not response.download!

If your web server supports the X-Sendfile header, you are able to stream your uploaded files from the web server, instead of through web2py. Here is a snippet from Cherokee Website explaining the X-Sendfile header.

X-Sendfile is a special, non-standard HTTP header that has been supported by Cherokee for a while. At first you might think it is no big deal, but think again.It can be enabled in any CGI, FastCGI or SCGI backend. Basically its job is to instruct the web server to ignore the content of the response and replace it by whatever is specified in the header. The main advantage of this is that it will be Cherokee the one serving the file, making use of all its optimizations. It is useful for processing script-output of e.g. php, perl, ruby or any cgi.

This is particularly useful because it hands the load to Cherokee, all the response headers from the backend are forwarded, the whole application uses a lot less resources and performs several times faster not having to worry about a task best suited for a web server.

You retain the ability to check for special privileges or dynamically deciding anything contemplated by your backend’s logic, you speed up things a lot while having more resources freed, and you can even specify the delivery of files outside of the web server’s document root path. Of course, this is to be done solely in controlled environments. In short, it offers a huge performance gain at absolutely no cost.

Now lets implement this in web2py. Using the following database table

db.define_table('document', Field('file', 'upload'))

Replace your download function with the following.

def download():
    document = db(db.document.id == request.args(0)).select().first()
    response.headers['X-Sendfile'] = os.path.join(request.folder, 'uploads', document.file)