mirror of
https://github.com/langgenius/dify.git
synced 2025-11-14 18:32:19 +00:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: lyzno1 <yuanyouhuilyz@gmail.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Hanqing Zhao <sherry9277@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Harry <xh001x@hotmail.com>
107 lines
3.3 KiB
Python
107 lines
3.3 KiB
Python
from typing import Literal
|
|
|
|
from flask import request
|
|
from flask_login import current_user
|
|
from flask_restx import Resource, marshal_with
|
|
from werkzeug.exceptions import Forbidden
|
|
|
|
import services
|
|
from configs import dify_config
|
|
from constants import DOCUMENT_EXTENSIONS
|
|
from controllers.common.errors import (
|
|
FilenameNotExistsError,
|
|
FileTooLargeError,
|
|
NoFileUploadedError,
|
|
TooManyFilesError,
|
|
UnsupportedFileTypeError,
|
|
)
|
|
from controllers.console.wraps import (
|
|
account_initialization_required,
|
|
cloud_edition_billing_resource_check,
|
|
setup_required,
|
|
)
|
|
from extensions.ext_database import db
|
|
from fields.file_fields import file_fields, upload_config_fields
|
|
from libs.login import login_required
|
|
from models import Account
|
|
from services.file_service import FileService
|
|
|
|
PREVIEW_WORDS_LIMIT = 3000
|
|
|
|
|
|
class FileApi(Resource):
|
|
@setup_required
|
|
@login_required
|
|
@account_initialization_required
|
|
@marshal_with(upload_config_fields)
|
|
def get(self):
|
|
return {
|
|
"file_size_limit": dify_config.UPLOAD_FILE_SIZE_LIMIT,
|
|
"batch_count_limit": dify_config.UPLOAD_FILE_BATCH_LIMIT,
|
|
"image_file_size_limit": dify_config.UPLOAD_IMAGE_FILE_SIZE_LIMIT,
|
|
"video_file_size_limit": dify_config.UPLOAD_VIDEO_FILE_SIZE_LIMIT,
|
|
"audio_file_size_limit": dify_config.UPLOAD_AUDIO_FILE_SIZE_LIMIT,
|
|
"workflow_file_upload_limit": dify_config.WORKFLOW_FILE_UPLOAD_LIMIT,
|
|
}, 200
|
|
|
|
@setup_required
|
|
@login_required
|
|
@account_initialization_required
|
|
@marshal_with(file_fields)
|
|
@cloud_edition_billing_resource_check("documents")
|
|
def post(self):
|
|
source_str = request.form.get("source")
|
|
source: Literal["datasets"] | None = "datasets" if source_str == "datasets" else None
|
|
|
|
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
|
|
|
|
if source == "datasets" and not current_user.is_dataset_editor:
|
|
raise Forbidden()
|
|
|
|
if source not in ("datasets", None):
|
|
source = None
|
|
|
|
if not isinstance(current_user, Account):
|
|
raise ValueError("Invalid user account")
|
|
|
|
try:
|
|
upload_file = FileService(db.engine).upload_file(
|
|
filename=file.filename,
|
|
content=file.read(),
|
|
mimetype=file.mimetype,
|
|
user=current_user,
|
|
source=source,
|
|
)
|
|
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
|
|
|
|
|
|
class FilePreviewApi(Resource):
|
|
@setup_required
|
|
@login_required
|
|
@account_initialization_required
|
|
def get(self, file_id):
|
|
file_id = str(file_id)
|
|
text = FileService(db.engine).get_file_preview(file_id)
|
|
return {"content": text}
|
|
|
|
|
|
class FileSupportTypeApi(Resource):
|
|
@setup_required
|
|
@login_required
|
|
@account_initialization_required
|
|
def get(self):
|
|
return {"allowed_extensions": DOCUMENT_EXTENSIONS}
|