Refactored to use Python tempfile

This commit is contained in:
mike cullerton 2021-09-15 13:33:55 -04:00
parent c335390025
commit 6ca8ba3e69
1 changed files with 13 additions and 20 deletions

View File

@ -5,7 +5,7 @@ from crc.scripts.script import Script
from crc.services.file_service import FileService
from crc.services.study_service import StudyService
import os
import tempfile
import zipfile
@ -27,35 +27,28 @@ class GetZippedFiles(Script):
if 'file_ids' in kwargs.keys():
doc_info = StudyService().get_documents_status(study_id)
if 'filename' in kwargs.keys():
zip_filename = kwargs['filename']
else:
zip_filename = 'attachments.zip'
# Delete the temporary zipfile if it already exist
if os.path.exists(zip_filename):
os.remove(zip_filename)
file_ids = kwargs['file_ids']
files = session.query(FileModel).filter(FileModel.id.in_(file_ids)).all()
if files:
# Create a temporary zipfile with the requested files
with zipfile.ZipFile(zip_filename, mode='x', compression=zipfile.ZIP_DEFLATED) as zfw:
for file in files:
zip_key_words = doc_info[file.irb_doc_code]['zip_key_words']
file_name = f'{study_id} {zip_key_words} {file.name}'
file_data = session.query(FileDataModel).filter(FileDataModel.file_model_id == file.id).first()
zfw.writestr(file_name, file_data.data)
with tempfile.NamedTemporaryFile() as temp_file:
with zipfile.ZipFile(temp_file, mode='w', compression=zipfile.ZIP_DEFLATED) as zfw:
for file in files:
zip_key_words = doc_info[file.irb_doc_code]['zip_key_words']
file_name = f'{study_id} {zip_key_words} {file.name}'
file_data = session.query(FileDataModel).filter(FileDataModel.file_model_id == file.id).first()
zfw.writestr(file_name, file_data.data)
# Add the zipfile to the DB, and grab the file_model
with open(zip_filename, mode='rb') as handle:
file_model = FileService().add_workflow_file(workflow_id, None, task.name, zip_filename, 'application/zip', handle.read())
# Delete the temporary zipfile
os.remove(zip_filename)
# Return the file_model
return file_model
with open(temp_file.name, mode='rb') as handle:
file_model = FileService().add_workflow_file(workflow_id, None, task.name, zip_filename,
'application/zip', handle.read())
return file_model
else:
raise ApiError(code='missing_file_ids',