| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  | from typing import Literal | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-21 02:11:40 +09:00
										 |  |  | from flask import request | 
					
						
							| 
									
										
										
										
											2025-05-06 11:58:49 +08:00
										 |  |  | from flask_login import current_user | 
					
						
							|  |  |  | from flask_restful import Resource, marshal_with | 
					
						
							| 
									
										
										
										
											2024-12-18 11:02:40 +08:00
										 |  |  | from werkzeug.exceptions import Forbidden | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:32 +08:00
										 |  |  | import services | 
					
						
							| 
									
										
										
										
											2024-07-21 02:11:40 +09:00
										 |  |  | from configs import dify_config | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | from constants import DOCUMENT_EXTENSIONS | 
					
						
							| 
									
										
										
										
											2024-11-01 15:51:22 +08:00
										 |  |  | from controllers.common.errors import FilenameNotExistsError | 
					
						
							|  |  |  | from controllers.console.wraps import ( | 
					
						
							|  |  |  |     account_initialization_required, | 
					
						
							|  |  |  |     cloud_edition_billing_resource_check, | 
					
						
							|  |  |  |     setup_required, | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | from fields.file_fields import file_fields, upload_config_fields | 
					
						
							|  |  |  | from libs.login import login_required | 
					
						
							|  |  |  | from services.file_service import FileService | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 14:35:58 +08:00
										 |  |  | from .error import ( | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  |     FileTooLargeError, | 
					
						
							|  |  |  |     NoFileUploadedError, | 
					
						
							|  |  |  |     TooManyFilesError, | 
					
						
							|  |  |  |     UnsupportedFileTypeError, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | PREVIEW_WORDS_LIMIT = 3000 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FileApi(Resource): | 
					
						
							| 
									
										
										
										
											2023-08-16 23:14:27 +08:00
										 |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     @marshal_with(upload_config_fields) | 
					
						
							|  |  |  |     def get(self): | 
					
						
							|  |  |  |         return { | 
					
						
							| 
									
										
										
										
											2024-10-22 16:34:16 +08:00
										 |  |  |             "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, | 
					
						
							| 
									
										
										
										
											2024-11-04 15:55:34 +08:00
										 |  |  |             "workflow_file_upload_limit": dify_config.WORKFLOW_FILE_UPLOAD_LIMIT, | 
					
						
							| 
									
										
										
										
											2023-08-16 23:14:27 +08:00
										 |  |  |         }, 200 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     @marshal_with(file_fields) | 
					
						
							| 
									
										
										
										
											2024-08-28 18:55:47 +08:00
										 |  |  |     @cloud_edition_billing_resource_check("documents") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     def post(self): | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         file = request.files["file"] | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         source_str = request.form.get("source") | 
					
						
							|  |  |  |         source: Literal["datasets"] | None = "datasets" if source_str == "datasets" else None | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if "file" not in request.files: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             raise NoFileUploadedError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if len(request.files) > 1: | 
					
						
							|  |  |  |             raise TooManyFilesError() | 
					
						
							| 
									
										
										
										
											2024-11-01 15:51:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if not file.filename: | 
					
						
							|  |  |  |             raise FilenameNotExistsError | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-18 11:02:40 +08:00
										 |  |  |         if source == "datasets" and not current_user.is_dataset_editor: | 
					
						
							|  |  |  |             raise Forbidden() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-01 15:51:22 +08:00
										 |  |  |         if source not in ("datasets", None): | 
					
						
							|  |  |  |             source = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:32 +08:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-11-01 15:51:22 +08:00
										 |  |  |             upload_file = FileService.upload_file( | 
					
						
							|  |  |  |                 filename=file.filename, | 
					
						
							|  |  |  |                 content=file.read(), | 
					
						
							|  |  |  |                 mimetype=file.mimetype, | 
					
						
							|  |  |  |                 user=current_user, | 
					
						
							|  |  |  |                 source=source, | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:32 +08:00
										 |  |  |         except services.errors.file.FileTooLargeError as file_too_large_error: | 
					
						
							|  |  |  |             raise FileTooLargeError(file_too_large_error.description) | 
					
						
							|  |  |  |         except services.errors.file.UnsupportedFileTypeError: | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             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) | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:32 +08:00
										 |  |  |         text = FileService.get_file_preview(file_id) | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return {"content": text} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-19 09:23:49 +00:00
										 |  |  | class FileSupportTypeApi(Resource): | 
					
						
							| 
									
										
										
										
											2023-12-18 23:24:06 +08:00
										 |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     def get(self): | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         return {"allowed_extensions": DOCUMENT_EXTENSIONS} |