I am wondering what is the best and possibly easiest way to serve files from GridFS using Pyramid. I use nginx as a proxy server (for ssl) and waitress as my application server.
The file types I need to be able to serve are the following: mp3, pdf, jpg, png
The files should be accessible through the following url "/files/{userid}/{filename}"
Right now the files are opened by the right application on client-side because I explicitly set the content-type in my code like so:
if filename[-3:] == "pdf":
response = Response(content_type='application/pdf')
elif filename[-3:] in ["jpg", "png"]:
response = Response(content_type='image/*')
elif filename[-3:] in ["mp3"]:
response = Response(content_type='audio/mp3')
else:
response = Response(content_type="application/*")
response.app_iter = file #file is a GridFS file object
return response
The only thing is that I can't stream the mp3s properly. I use audio.js to play them. They open up and play but no track length is shown and I can't seek them. I know it has something to do with the "accept-ranges" property but I can't seem to set it right. Does it have to do with nginx or waitress? Or am I just not setting the header correctly?
I would like to use something as easy as return FileResponse(file)
like specified here but my file does not come from the filesystem directly... Is there a plug and play way to make this work?
Any advice would be really appreciated!
Thank you very much for your help!
I found a solution on this blog.
The idea is to use a patched DataApp
from paste.fileapp
. All the details are in the post, and now my app behaves just like I want!