+18


![dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)

![autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)




FFXN
GitHub
yyh
盐粒 Yanli
autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Tianle
dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Yunlu Wen
zyssyz123
Claude Opus 4.7
chariri
Asuka Minato
Copilot Autofix powered by AI
Nian
非法操作
Carmen Fernández Ruiz
wangxiaolei
QuantumGhost
L1nSn0w
Evan
Escape0707
Jingyi
Amr Sherif
ZHOU ZHICHEN
unknown
JzoNg
Xiyuan Chen
-LAN-
107bba0116
Signed-off-by: dependabot[bot] <[email protected]> Signed-off-by: EvanYao826 <[email protected]> Co-authored-by: yyh <[email protected]> Co-authored-by: 盐粒 Yanli <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Tianle <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Yunlu Wen <[email protected]> Co-authored-by: zyssyz123 <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]> Co-authored-by: chariri <[email protected]> Co-authored-by: Asuka Minato <[email protected]> Co-authored-by: Copilot Autofix powered by AI <[email protected]> Co-authored-by: Nian <[email protected]> Co-authored-by: 非法操作 <[email protected]> Co-authored-by: Carmen Fernández Ruiz <[email protected]> Co-authored-by: wangxiaolei <[email protected]> Co-authored-by: QuantumGhost <[email protected]> Co-authored-by: L1nSn0w <[email protected]> Co-authored-by: Evan <[email protected]> Co-authored-by: Escape0707 <[email protected]> Co-authored-by: Jingyi <[email protected]> Co-authored-by: Amr Sherif <[email protected]> Co-authored-by: ZHOU ZHICHEN <[email protected]> Co-authored-by: unknown <[email protected]> Co-authored-by: JzoNg <[email protected]> Co-authored-by: Xiyuan Chen <[email protected]> Co-authored-by: -LAN- <[email protected]>
89 lines
3.1 KiB
Python
89 lines
3.1 KiB
Python
from flask import request
|
|
|
|
import services
|
|
from controllers.common.errors import (
|
|
FilenameNotExistsError,
|
|
FileTooLargeError,
|
|
NoFileUploadedError,
|
|
TooManyFilesError,
|
|
UnsupportedFileTypeError,
|
|
)
|
|
from controllers.common.schema import register_schema_models
|
|
from controllers.web import web_ns
|
|
from controllers.web.wraps import WebApiResource
|
|
from extensions.ext_database import db
|
|
from fields.file_fields import FileResponse
|
|
from models.model import App, EndUser
|
|
from services.file_service import FileService
|
|
|
|
register_schema_models(web_ns, FileResponse)
|
|
|
|
|
|
@web_ns.route("/files/upload")
|
|
class FileApi(WebApiResource):
|
|
@web_ns.doc("upload_file")
|
|
@web_ns.doc(description="Upload a file for use in web applications")
|
|
@web_ns.doc(
|
|
responses={
|
|
201: "File uploaded successfully",
|
|
400: "Bad request - invalid file or parameters",
|
|
413: "File too large",
|
|
415: "Unsupported file type",
|
|
}
|
|
)
|
|
@web_ns.response(201, "File uploaded successfully", web_ns.models[FileResponse.__name__])
|
|
def post(self, app_model: App, end_user: EndUser):
|
|
"""Upload a file for use in web applications.
|
|
|
|
Accepts file uploads for use within web applications, supporting
|
|
multiple file types with automatic validation and storage.
|
|
|
|
Args:
|
|
app_model: The associated application model
|
|
end_user: The end user uploading the file
|
|
|
|
Form Parameters:
|
|
file: The file to upload (required)
|
|
source: Optional source type (datasets or None)
|
|
|
|
Returns:
|
|
dict: File information including ID, URL, and metadata
|
|
int: HTTP status code 201 for success
|
|
|
|
Raises:
|
|
NoFileUploadedError: No file provided in request
|
|
TooManyFilesError: Multiple files provided (only one allowed)
|
|
FilenameNotExistsError: File has no filename
|
|
FileTooLargeError: File exceeds size limit
|
|
UnsupportedFileTypeError: File type not supported
|
|
"""
|
|
if "file" not in request.files:
|
|
raise NoFileUploadedError()
|
|
|
|
if len(request.files) > 1:
|
|
raise TooManyFilesError()
|
|
|
|
file = request.files["file"]
|
|
if not file.filename:
|
|
raise FilenameNotExistsError
|
|
|
|
source = request.form.get("source")
|
|
if source not in ("datasets", None):
|
|
source = None
|
|
|
|
try:
|
|
upload_file = FileService(db.engine).upload_file(
|
|
filename=file.filename,
|
|
content=file.stream.read(),
|
|
mimetype=file.mimetype,
|
|
user=end_user,
|
|
source="datasets" if source == "datasets" else None,
|
|
)
|
|
except services.errors.file.FileTooLargeError as file_too_large_error:
|
|
raise FileTooLargeError(file_too_large_error.description)
|
|
except services.errors.file.UnsupportedFileTypeError:
|
|
raise UnsupportedFileTypeError()
|
|
|
|
response = FileResponse.model_validate(upload_file, from_attributes=True)
|
|
return response.model_dump(mode="json"), 201
|