2024-02-06 13:21:13 +08:00
|
|
|
from flask import request
|
|
|
|
from flask_restful import marshal_with
|
|
|
|
|
2024-01-12 12:34:01 +08:00
|
|
|
import services
|
2023-11-13 22:05:46 +08:00
|
|
|
from controllers.web import api
|
2024-01-12 12:34:01 +08:00
|
|
|
from controllers.web.error import FileTooLargeError, NoFileUploadedError, TooManyFilesError, UnsupportedFileTypeError
|
2023-11-13 22:05:46 +08:00
|
|
|
from controllers.web.wraps import WebApiResource
|
|
|
|
from fields.file_fields import file_fields
|
2024-01-12 12:34:01 +08:00
|
|
|
from services.file_service import FileService
|
2023-11-13 22:05:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
class FileApi(WebApiResource):
|
|
|
|
@marshal_with(file_fields)
|
|
|
|
def post(self, app_model, end_user):
|
|
|
|
# get file from request
|
2024-08-26 15:29:10 +08:00
|
|
|
file = request.files["file"]
|
2023-11-13 22:05:46 +08:00
|
|
|
|
|
|
|
# check file
|
2024-08-26 15:29:10 +08:00
|
|
|
if "file" not in request.files:
|
2023-11-13 22:05:46 +08:00
|
|
|
raise NoFileUploadedError()
|
|
|
|
|
|
|
|
if len(request.files) > 1:
|
|
|
|
raise TooManyFilesError()
|
|
|
|
try:
|
|
|
|
upload_file = FileService.upload_file(file, end_user)
|
|
|
|
except services.errors.file.FileTooLargeError as file_too_large_error:
|
|
|
|
raise FileTooLargeError(file_too_large_error.description)
|
|
|
|
except services.errors.file.UnsupportedFileTypeError:
|
|
|
|
raise UnsupportedFileTypeError()
|
|
|
|
|
|
|
|
return upload_file, 201
|
|
|
|
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
api.add_resource(FileApi, "/files/upload")
|