2019-12-27 18:50:03 +00:00
|
|
|
import io
|
|
|
|
import os
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
import connexion
|
|
|
|
from flask import send_file
|
|
|
|
|
2020-01-14 16:45:12 +00:00
|
|
|
from crc import session
|
2019-12-27 18:50:03 +00:00
|
|
|
from crc.api.common import ApiErrorSchema, ApiError
|
2020-01-24 16:52:52 +00:00
|
|
|
from crc.models.file import FileModelSchema, FileModel, FileDataModel, FileType
|
2020-03-04 18:40:25 +00:00
|
|
|
from crc.models.workflow import WorkflowSpecModel
|
2020-02-28 16:54:11 +00:00
|
|
|
from crc.services.file_service import FileService
|
2019-12-27 18:50:03 +00:00
|
|
|
|
|
|
|
|
2020-02-05 20:06:19 +00:00
|
|
|
def get_files(workflow_spec_id=None, study_id=None, workflow_id=None, task_id=None, form_field_key=None):
|
|
|
|
if all(v is None for v in [workflow_spec_id, study_id, workflow_id, task_id, form_field_key]):
|
2020-02-04 14:57:02 +00:00
|
|
|
return ApiErrorSchema().dump(ApiError('missing_parameter',
|
|
|
|
'Please specify at least one of workflow_spec_id, study_id, '
|
|
|
|
'workflow_id, and task_id for this file in the HTTP parameters')), 400
|
2019-12-27 18:50:03 +00:00
|
|
|
|
2020-02-10 21:19:23 +00:00
|
|
|
results = FileService.get_files(workflow_spec_id, study_id, workflow_id, task_id, form_field_key)
|
|
|
|
return FileModelSchema(many=True).dump(results)
|
2020-02-04 02:56:18 +00:00
|
|
|
|
2019-12-27 18:50:03 +00:00
|
|
|
|
2020-02-05 20:06:19 +00:00
|
|
|
def add_file(workflow_spec_id=None, study_id=None, workflow_id=None, task_id=None, form_field_key=None):
|
|
|
|
all_none = all(v is None for v in [workflow_spec_id, study_id, workflow_id, task_id, form_field_key])
|
|
|
|
missing_some = (workflow_spec_id is None) and (None in [study_id, workflow_id, task_id, form_field_key])
|
2020-02-04 14:57:02 +00:00
|
|
|
if all_none or missing_some:
|
|
|
|
return ApiErrorSchema().dump(ApiError('missing_parameter',
|
|
|
|
'Please specify either a workflow_spec_id or all 3 of study_id, '
|
|
|
|
'workflow_id, and task_id for this file in the HTTP parameters')), 404
|
2020-02-10 21:19:23 +00:00
|
|
|
if 'file' not in connexion.request.files:
|
|
|
|
return ApiErrorSchema().dump(ApiError('invalid_file',
|
|
|
|
'Expected a file named "file" in the multipart form request')), 404
|
2020-02-04 14:57:02 +00:00
|
|
|
|
2020-02-10 21:19:23 +00:00
|
|
|
file = connexion.request.files['file']
|
|
|
|
if workflow_spec_id:
|
2020-03-04 18:40:25 +00:00
|
|
|
workflow_spec = session.query(WorkflowSpecModel).filter_by(id=workflow_spec_id).first()
|
|
|
|
file_model = FileService.add_workflow_spec_file(workflow_spec, file.filename, file.content_type, file.stream.read())
|
2020-02-10 21:19:23 +00:00
|
|
|
else:
|
2020-02-11 20:03:25 +00:00
|
|
|
file_model = FileService.add_form_field_file(study_id, workflow_id, task_id, form_field_key, file.filename, file.content_type, file.stream.read())
|
2020-02-10 21:19:23 +00:00
|
|
|
|
|
|
|
return FileModelSchema().dump(file_model)
|
2019-12-27 18:50:03 +00:00
|
|
|
|
|
|
|
|
2020-01-31 15:39:19 +00:00
|
|
|
def update_file_data(file_id):
|
2020-01-14 16:45:12 +00:00
|
|
|
file_model = session.query(FileModel).filter_by(id=file_id).with_for_update().first()
|
2020-02-10 21:19:23 +00:00
|
|
|
file = connexion.request.files['file']
|
2019-12-27 18:50:03 +00:00
|
|
|
if file_model is None:
|
|
|
|
return ApiErrorSchema().dump(ApiError('no_such_file', 'The file id you provided does not exist')), 404
|
2020-02-10 21:19:23 +00:00
|
|
|
file_model = FileService.update_file(file_model, file.stream.read(), file.content_type)
|
|
|
|
return FileModelSchema().dump(file_model)
|
2019-12-27 18:50:03 +00:00
|
|
|
|
|
|
|
|
2020-01-31 15:39:19 +00:00
|
|
|
def get_file_data(file_id):
|
2020-02-10 21:19:23 +00:00
|
|
|
file_data = FileService.get_file_data(file_id)
|
2019-12-27 18:50:03 +00:00
|
|
|
if file_data is None:
|
|
|
|
return ApiErrorSchema().dump(ApiError('no_such_file', 'The file id you provided does not exist')), 404
|
|
|
|
return send_file(
|
2020-01-14 16:45:12 +00:00
|
|
|
io.BytesIO(file_data.data),
|
|
|
|
attachment_filename=file_data.file_model.name,
|
|
|
|
mimetype=file_data.file_model.content_type,
|
|
|
|
cache_timeout=-1 # Don't cache these files on the browser.
|
|
|
|
)
|
2019-12-27 18:50:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_file_info(file_id):
|
2020-01-14 16:45:12 +00:00
|
|
|
file_model = session.query(FileModel).filter_by(id=file_id).with_for_update().first()
|
2019-12-27 18:50:03 +00:00
|
|
|
if file_model is None:
|
|
|
|
return ApiErrorSchema().dump(ApiError('no_such_file', 'The file id you provided does not exist')), 404
|
2020-01-03 16:44:24 +00:00
|
|
|
return FileModelSchema().dump(file_model)
|
2019-12-27 18:50:03 +00:00
|
|
|
|
|
|
|
|
2020-01-31 15:39:19 +00:00
|
|
|
def update_file_info(file_id, body):
|
|
|
|
if file_id is None:
|
|
|
|
error = ApiError('unknown_file', 'Please provide a valid File ID.')
|
|
|
|
return ApiErrorSchema.dump(error), 404
|
|
|
|
|
|
|
|
file_model = session.query(FileModel).filter_by(id=file_id).first()
|
|
|
|
|
|
|
|
if file_model is None:
|
|
|
|
error = ApiError('unknown_file_model', 'The file_model "' + file_id + '" is not recognized.')
|
|
|
|
return ApiErrorSchema.dump(error), 404
|
|
|
|
|
|
|
|
file_model = FileModelSchema().load(body, session=session)
|
|
|
|
session.add(file_model)
|
|
|
|
session.commit()
|
|
|
|
return FileModelSchema().dump(file_model)
|
|
|
|
|
|
|
|
|
2019-12-27 18:50:03 +00:00
|
|
|
def delete_file(file_id):
|
2020-01-23 17:17:58 +00:00
|
|
|
session.query(FileDataModel).filter_by(file_model_id=file_id).delete()
|
2020-01-14 16:45:12 +00:00
|
|
|
session.query(FileModel).filter_by(id=file_id).delete()
|
2020-01-23 17:17:58 +00:00
|
|
|
session.commit()
|