I’m building a django based database app that allows users to upload supporting documentation. We’re expecting about 1 million pages of pdf by the end of the project. The repository will be broken down into 3,000 – 5,000 different folders (this is specified by the client). I have a single db table recording these documents. The table includes the name of the document and the folder the document is stored in.
I’m looking for the best way of serving these document back up to the client. I’ve got a basic search tool that lists documents in an html table. I want the table to include an html link to the uploaded document. I’m just not sure how to create this link. More specifically I’m not sure how to serve the files themselves. I want to use django rather than serving them directly through apache, as I want to use django’s permissions system.
Can I use the staticfiles app for this? I’m concerned that staticfiles looks like it wants to copy files from one place to another (which probably isn’t practical given the size of the repository). I’m also not sure how staticfiles will cope with the dynamic folder structure. We won’t know the folder structure untill the files are actually uploaded. Lastly the django docs say specifically that the static files app is not designed for user uploaded documents.
Perhaps I just need some sort of django view, but I can’t get the following to work –
def document(request, path, name):
path = os.path.join(settings.DOCUMENT_ROOT, path, name)
pdf = open(path, 'r')
response = HttpResponse(pdf, mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=' + name
return response
When adobe reader tries to open the attachment it tells me the file’s damaged (or not supported).
Any input would be greatly appreciated!
OK for the time being I’m just going with a simple django view –