def commit_chunked_upload(root, path_): tools.validate_root_or_abort(root) # A missing object for /commit_chunked_upload gives a 400. upload_id = request.form['upload_id'] db_upload = _find_existing_upload(upload_id) if not db_upload: raise BasicError(400, 'Unknown upload_id {0}'.format(upload_id)) # Start by purging the table of all expired uploads. _purge_expired_uploads() # We retrieve a file-like object to the uploaded data, and use this as an # input stream to a regular file upload request. data_stream = file_store.retrieve_chunk_stream(upload_id) # Install a SHA1 hash calculating stream on the request. This will # allow us to compute the file's hash as it is written to disk. crypt_key = g.user.dbuser.password[:32] enc_stream = stream.AESEncryptionStream(data_stream, crypt_key) hash_stream = stream.ChecksumCalcStream(enc_stream) put_result = put.do_put(root, path_, hash_stream, hash_stream.hash, enc_stream.IV) # Close the input stream. data_stream.close() # Delete the temporary upload file. file_store.remove_chunked_upload(upload_id) # If we got here, it means that the storage is successful and the database # object can now be safely deleted. g.db_session.delete(db_upload) g.db_session.commit() return put_result
def _purge_expired_uploads(): clause = models.ChunkedUpload.expires < datetime.datetime.utcnow() expired_items = g.db_session.query(models.ChunkedUpload).filter(clause) for expired_upload in expired_items.all(): file_store.remove_chunked_upload(expired_upload.upload_id or '') expired_items.delete() g.db_session.commit()