I'm writing a web application in python/pyramid that handles the upload of a file (via jquery uploader). The code for the upload is:
@view_config(route_name='file.upload', renderer='json')
def file_upload(request):
for item, filestorage in request.POST.items():
f = File.create(filestorage)
u = Upload.create(f.hash)
return {
'url': request.route_url('file.get', uploadid=u.urlid)
}
where then File.create makes a sha1 of the file and moves it in a permanent location while putting the metadata in a database. (File is a SQLAlchemy class actually)
The problem here is that the view callable is called after that the file transfer to the server is complete. This poses two problems: the first one is that i'm unable to reject the file transfer if it's bigger than some size. The second is that i have to wait to receive the whole file and then i can start hashing it.
What i would like to obtain is to start process the file - some sort of stream - as soon i'm getting the data so i can hash it while the user is uploading it and i can stop the transfer if the size is bigger than some value.