| 
									
										
										
										
											2023-09-27 16:06:32 +08:00
										 |  |  | import services | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from controllers.console import api | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from controllers.console.datasets.error import (FileTooLargeError, NoFileUploadedError, TooManyFilesError, | 
					
						
							|  |  |  |                                                 UnsupportedFileTypeError) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from controllers.console.setup import setup_required | 
					
						
							|  |  |  | from controllers.console.wraps import account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from fields.file_fields import file_fields, upload_config_fields | 
					
						
							|  |  |  | from flask import current_app, request | 
					
						
							|  |  |  | from flask_login import current_user | 
					
						
							|  |  |  | from flask_restful import Resource, marshal_with | 
					
						
							|  |  |  | from libs.login import login_required | 
					
						
							| 
									
										
										
										
											2024-01-20 22:21:51 +08:00
										 |  |  | from services.file_service import FileService, ALLOWED_EXTENSIONS, UNSTRUSTURED_ALLOWED_EXTENSIONS | 
					
						
							| 
									
										
										
										
											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): | 
					
						
							|  |  |  |         file_size_limit = current_app.config.get("UPLOAD_FILE_SIZE_LIMIT") | 
					
						
							|  |  |  |         batch_count_limit = current_app.config.get("UPLOAD_FILE_BATCH_LIMIT") | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |         image_file_size_limit = current_app.config.get("UPLOAD_IMAGE_FILE_SIZE_LIMIT") | 
					
						
							| 
									
										
										
										
											2023-08-16 23:14:27 +08:00
										 |  |  |         return { | 
					
						
							|  |  |  |             'file_size_limit': file_size_limit, | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |             'batch_count_limit': batch_count_limit, | 
					
						
							|  |  |  |             'image_file_size_limit': image_file_size_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) | 
					
						
							|  |  |  |     def post(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # get file from request | 
					
						
							|  |  |  |         file = request.files['file'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # check file | 
					
						
							|  |  |  |         if 'file' not in request.files: | 
					
						
							|  |  |  |             raise NoFileUploadedError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if len(request.files) > 1: | 
					
						
							|  |  |  |             raise TooManyFilesError() | 
					
						
							| 
									
										
										
										
											2023-09-27 16:06:32 +08:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |             upload_file = FileService.upload_file(file, current_user) | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         return {'content': text} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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): | 
					
						
							|  |  |  |         etl_type = current_app.config['ETL_TYPE'] | 
					
						
							| 
									
										
										
										
											2024-01-20 22:21:51 +08:00
										 |  |  |         allowed_extensions = UNSTRUSTURED_ALLOWED_EXTENSIONS if etl_type == 'Unstructured' else ALLOWED_EXTENSIONS | 
					
						
							| 
									
										
										
										
											2023-12-18 23:24:06 +08:00
										 |  |  |         return {'allowed_extensions': allowed_extensions} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | api.add_resource(FileApi, '/files/upload') | 
					
						
							|  |  |  | api.add_resource(FilePreviewApi, '/files/<uuid:file_id>/preview') | 
					
						
							| 
									
										
										
										
											2023-12-19 09:23:49 +00:00
										 |  |  | api.add_resource(FileSupportTypeApi, '/files/support-type') |