Skip to content

Commit

Permalink
add API for file upload by access_token
Browse files Browse the repository at this point in the history
  • Loading branch information
HadronCollider committed Sep 20, 2024
1 parent 90c3571 commit 5704eae
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions app/routes/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("/<task_id>", methods=["GET"])
@login_required
def get_status(task_id):
Expand Down

0 comments on commit 5704eae

Please sign in to comment.