def get(self): token_guid = request.args.get('token', '') as_attachment = request.args.get('as_attachment', None) document_guid = cache.get(DOWNLOAD_TOKEN(token_guid)) cache.delete(DOWNLOAD_TOKEN(token_guid)) if not document_guid: raise BadRequest('Valid token required for download') document = Document.query.filter_by( document_guid=document_guid).first() if not document: raise NotFound( 'Could not find the document corresponding to the token') if as_attachment is not None: as_attachment = True if as_attachment == 'true' else False else: as_attachment = '.pdf' not in document.file_display_name.lower() if document.object_store_path: return ObjectStoreStorageService().download_file( path=document.object_store_path, display_name=document.file_display_name, as_attachment=as_attachment) else: return send_file(filename_or_fp=document.full_storage_path, attachment_filename=document.file_display_name, as_attachment=as_attachment)
def get(self): token_guid = request.args.get('token', '') doc_guid = cache.get(DOWNLOAD_TOKEN(token_guid)) cache.delete(DOWNLOAD_TOKEN(token_guid)) if not doc_guid: raise BadRequest('Valid token requred for download') doc = Document.query.filter_by(document_guid=doc_guid).first() if not doc: raise NotFound('Could not find the document corresponding to the token') not_pdf = '.pdf' not in doc.file_display_name.lower() return send_file(filename_or_fp=doc.full_storage_path, attachment_filename=doc.file_display_name, as_attachment=not_pdf)
def post(self, document_guid): if not document_guid: raise BadRequest('Must specify document GUID') token = str(uuid.uuid4()) cache.set(DOWNLOAD_TOKEN(token), document_guid, TIMEOUT_5_MINUTES) return {'token': token}
def get(self, token): if not token: raise BadRequest('Must specify token') doc_guid = cache.get(DOWNLOAD_TOKEN(token)) cache.delete(DOWNLOAD_TOKEN(token)) if not doc_guid: raise NotFound('Could not find token') doc = Document.find_by_document_guid(doc_guid) if not doc: raise NotFound('Could not find document') return send_file(filename_or_fp=doc.full_storage_path, attachment_filename=doc.filename, as_attachment=True)
def get(self): token_guid = request.args.get('token', '') attachment = request.args.get('as_attachment', None) doc_guid = cache.get(DOWNLOAD_TOKEN(token_guid)) cache.delete(DOWNLOAD_TOKEN(token_guid)) if not doc_guid: raise BadRequest('Valid token required for download') doc = Document.query.filter_by(document_guid=doc_guid).first() if not doc: raise NotFound( 'Could not find the document corresponding to the token') current_app.logger.debug(attachment) if attachment is not None: attach_style = True if attachment == 'true' else False else: attach_style = '.pdf' not in doc.file_display_name.lower() return send_file(filename_or_fp=doc.full_storage_path, attachment_filename=doc.file_display_name, as_attachment=attach_style)
def post(self, document_guid): if not document_guid: raise BadRequest('Must specify document GUID') doc = Document.find_by_document_guid(document_guid) if not doc: raise NotFound('Could not find document') if not doc.upload_completed_date: raise BadRequest('File upload not complete') token = str(uuid.uuid4()) cache.set(DOWNLOAD_TOKEN(token), document_guid, TIMEOUT_5_MINUTES) return {'token': token}
def test_download_file_happy_path(test_client, db_session, auth_headers, tmp_path): document = DocumentFactory(path_root=tmp_path, file_display_name='testfile.pdf') #write file test_data = 'Contents of file' file_path = document.full_storage_path os.makedirs(os.path.dirname(file_path), exist_ok=True) with open(file_path, "w") as f: f.write(test_data) token_guid = uuid.uuid4() #retieve file with token with mock.patch.object(cache, 'get') as mock_cache_get: mock_cache_get.return_value = document.document_guid get_resp = test_client.get(f'/documents?token={token_guid}') assert get_resp.status_code == 200 assert get_resp.data.decode() == test_data mock_cache_get.assert_called_with(DOWNLOAD_TOKEN(token_guid))