support-formatter-api/support_formatter/routes/upload.py

67 lines
2 KiB
Python
Raw Permalink Normal View History

2024-09-30 18:40:26 +00:00
import os
import pathlib
2024-10-04 13:03:44 +00:00
import uuid
2024-09-30 18:40:26 +00:00
from typing import List
2024-10-04 13:03:44 +00:00
2024-09-30 18:40:26 +00:00
import marshmallow as ma
from flask_smorest.fields import Upload
from werkzeug.datastructures import FileStorage
from ..app import Application
from ..config import APISettings
2024-10-04 13:03:44 +00:00
from ..logic.csv_processor import FileTypeInvalidError, process_csv
from ..utils import generate_error
2024-09-30 18:40:26 +00:00
from . import formatter_routes as blp
APP = Application.get_instance()
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in APISettings.ALLOWED_EXTENSIONS
class MultipartFormSchema(ma.Schema):
username = ma.fields.String(required=True)
friendcode = ma.fields.String(required=False)
class MultipartFileSchema(ma.Schema):
servantdata = Upload()
cedata = Upload()
2024-10-03 19:34:46 +00:00
@blp.route("/upload", methods=["POST"])
2024-09-30 18:40:26 +00:00
@blp.arguments(MultipartFormSchema, location="form")
@blp.arguments(MultipartFileSchema, location="files")
@blp.response(200)
def upload_file(form: dict[str, str], files: dict[str, FileStorage]):
filepaths: List[pathlib.Path] = []
returndata = {
"username": form["username"],
"friendcode": form.get("friendcode", "")
}
for name, file in files.items():
if name not in ("servantdata", "cedata"):
2024-10-04 13:03:44 +00:00
return generate_error(406, message="Invalid form sent")
2024-09-30 18:40:26 +00:00
filepath = APISettings.FILE_SAVE_DIRECTORY / f"{uuid.uuid4()}.csv"
file.save(filepath)
if os.stat(filepath).st_size < 1 or not allowed_file(file.filename):
2024-10-04 13:03:44 +00:00
filepath.unlink(missing_ok=True)
2024-09-30 18:40:26 +00:00
continue
filepaths.append(filepath)
if len(filepaths) == 0:
2024-10-04 13:03:44 +00:00
return generate_error(406, message="No files provided")
2024-09-30 18:40:26 +00:00
try:
for f in filepaths:
result = process_csv(f)
returndata = returndata | result
2024-10-04 13:03:44 +00:00
f.unlink(missing_ok=True)
2024-09-30 18:40:26 +00:00
except FileTypeInvalidError:
for f in filepaths:
2024-10-04 13:03:44 +00:00
f.unlink(missing_ok=True)
return generate_error(500, message="Error whilst parsing uploaded file - please contact Firq on Fate/Sacc Order")
2024-09-30 18:40:26 +00:00
return returndata