| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from flask import Response, request | 
					
						
							| 
									
										
										
										
											2024-10-23 13:12:34 +08:00
										 |  |  | from flask_restful import Resource, reqparse | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from werkzeug.exceptions import NotFound | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import services | 
					
						
							|  |  |  | from controllers.files import api | 
					
						
							| 
									
										
										
										
											2024-10-07 11:08:06 +08:00
										 |  |  | from controllers.files.error import UnsupportedFileTypeError | 
					
						
							| 
									
										
										
										
											2023-12-18 16:25:37 +08:00
										 |  |  | from services.account_service import TenantService | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from services.file_service import FileService | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ImagePreviewApi(Resource): | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     Deprecated | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get(self, file_id): | 
					
						
							|  |  |  |         file_id = str(file_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         timestamp = request.args.get("timestamp") | 
					
						
							|  |  |  |         nonce = request.args.get("nonce") | 
					
						
							|  |  |  |         sign = request.args.get("sign") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not timestamp or not nonce or not sign: | 
					
						
							|  |  |  |             return {"content": "Invalid request."}, 400 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             generator, mimetype = FileService.get_image_preview( | 
					
						
							|  |  |  |                 file_id=file_id, | 
					
						
							|  |  |  |                 timestamp=timestamp, | 
					
						
							|  |  |  |                 nonce=nonce, | 
					
						
							|  |  |  |                 sign=sign, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         except services.errors.file.UnsupportedFileTypeError: | 
					
						
							|  |  |  |             raise UnsupportedFileTypeError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Response(generator, mimetype=mimetype) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FilePreviewApi(Resource): | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |     def get(self, file_id): | 
					
						
							|  |  |  |         file_id = str(file_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-23 13:12:34 +08:00
										 |  |  |         parser = reqparse.RequestParser() | 
					
						
							|  |  |  |         parser.add_argument("timestamp", type=str, required=True, location="args") | 
					
						
							|  |  |  |         parser.add_argument("nonce", type=str, required=True, location="args") | 
					
						
							|  |  |  |         parser.add_argument("sign", type=str, required=True, location="args") | 
					
						
							|  |  |  |         parser.add_argument("as_attachment", type=bool, required=False, default=False, location="args") | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-23 13:12:34 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not args["timestamp"] or not args["nonce"] or not args["sign"]: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             return {"content": "Invalid request."}, 400 | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-10-23 13:12:34 +08:00
										 |  |  |             generator, upload_file = FileService.get_file_generator_by_file_id( | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |                 file_id=file_id, | 
					
						
							| 
									
										
										
										
											2024-10-23 13:12:34 +08:00
										 |  |  |                 timestamp=args["timestamp"], | 
					
						
							|  |  |  |                 nonce=args["nonce"], | 
					
						
							|  |  |  |                 sign=args["sign"], | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |             ) | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  |         except services.errors.file.UnsupportedFileTypeError: | 
					
						
							|  |  |  |             raise UnsupportedFileTypeError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-23 13:12:34 +08:00
										 |  |  |         response = Response( | 
					
						
							|  |  |  |             generator, | 
					
						
							|  |  |  |             mimetype=upload_file.mime_type, | 
					
						
							|  |  |  |             direct_passthrough=True, | 
					
						
							|  |  |  |             headers={}, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         if upload_file.size > 0: | 
					
						
							|  |  |  |             response.headers["Content-Length"] = str(upload_file.size) | 
					
						
							|  |  |  |         if args["as_attachment"]: | 
					
						
							|  |  |  |             response.headers["Content-Disposition"] = f"attachment; filename={upload_file.name}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return response | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-18 16:25:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class WorkspaceWebappLogoApi(Resource): | 
					
						
							|  |  |  |     def get(self, workspace_id): | 
					
						
							|  |  |  |         workspace_id = str(workspace_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         custom_config = TenantService.get_custom_config(workspace_id) | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         webapp_logo_file_id = custom_config.get("replace_webapp_logo") if custom_config is not None else None | 
					
						
							| 
									
										
										
										
											2023-12-18 16:25:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if not webapp_logo_file_id: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             raise NotFound("webapp logo is not found") | 
					
						
							| 
									
										
										
										
											2023-12-18 16:25:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             generator, mimetype = FileService.get_public_image_preview( | 
					
						
							|  |  |  |                 webapp_logo_file_id, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         except services.errors.file.UnsupportedFileTypeError: | 
					
						
							|  |  |  |             raise UnsupportedFileTypeError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Response(generator, mimetype=mimetype) | 
					
						
							| 
									
										
										
										
											2023-11-13 22:05:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | api.add_resource(ImagePreviewApi, "/files/<uuid:file_id>/image-preview") | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | api.add_resource(FilePreviewApi, "/files/<uuid:file_id>/file-preview") | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | api.add_resource(WorkspaceWebappLogoApi, "/files/workspaces/<uuid:workspace_id>/webapp-logo") |