From 5704eaef50a4a22ac8499a00b1a3eacf91351b93 Mon Sep 17 00:00:00 2001 From: Dmitry Ivanov Date: Fri, 20 Sep 2024 20:44:04 +0300 Subject: [PATCH] add API for file upload by access_token --- app/routes/tasks.py | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/app/routes/tasks.py b/app/routes/tasks.py index 8eedb8ad..001c4ecb 100644 --- a/app/routes/tasks.py +++ b/app/routes/tasks.py @@ -6,6 +6,7 @@ from flask_login import login_required, current_user from app.root_logger import get_root_logger +from app.routes.utils import check_access_token from app.utils import get_file_len, check_file from app.db import db_methods from app.db.db_types import Check @@ -85,6 +86,65 @@ def run_task(): db_methods.add_celery_task(task.id, file_id) # mapping celery_task to check (check_id = file_id) return {'task_id': task.id, 'check_id': str(file_id)} + +@tasks.route("/md", methods=["POST"]) +def run_md_task_by_api(): + if not check_access_token(request.args.get('access_token', None)): + return "No valid access token", 401 + + file = request.files.get('file') + criteria = request.args.get('criteria', None) + + # hardcoded + file_type = {'type': 'report', 'report_type': 'VKR'} + file_ext_type = file_type['type'] + + filename, extension = file.filename.rsplit('.', 1) + + if not file: + logger.critical("request doesn't include file") + return "request doesn't include file" + if get_file_len(file) * 2 + db_methods.get_storage() > current_app.config['MAX_SYSTEM_STORAGE']: + logger.critical('Storage overload has occured') + return 'storage_overload' + file_check_response = check_file(file, extension, ALLOWED_EXTENSIONS[file_ext_type], check_mime=True) + if file_check_response != "": + logger.info('По API загружен файл с ошибочным расширением: ' + file_check_response) + return file_check_response + + logger.info( + f"Запуск обработки файла {file.filename} по API с критериями {criteria}" + ) + + # save to file on disk for future checking + file_id = ObjectId() + filepath = join(UPLOAD_FOLDER, f"{file_id}.{extension}") + file.save(filepath) + # add file and file's info to db + file_id = db_methods.add_file_info_and_content("api_access_token", filepath, file_type, file_id) + # convert to pdf and save on disk and db + converted_id = db_methods.write_pdf(filename, filepath) + + check = Check({ + '_id': file_id, + 'conv_pdf_fs_id': converted_id, + 'user': 'api_access_token', + 'lms_user_id': None, + 'enabled_checks': criteria, + 'criteria': criteria, + 'file_type': file_type, + 'filename': file.filename, + 'score': -1, # score=-1 -> checking in progress + 'is_ended': False, + 'is_failed': False, + 'params_for_passback': None + }) + db_methods.add_check(file_id, check) # add check for parsed_file to db + task = create_task.delay(check.pack(to_str=True)) # add check to queue + db_methods.add_celery_task(task.id, file_id) # mapping celery_task to check (check_id = file_id) + return {'task_id': task.id, 'check_id': str(file_id)} + + @tasks.route("/", methods=["GET"]) @login_required def get_status(task_id):