| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | from flask import Response | 
					
						
							|  |  |  | from flask_restful import Resource, reqparse | 
					
						
							| 
									
										
										
										
											2024-02-01 18:11:57 +08:00
										 |  |  | from werkzeug.exceptions import Forbidden, NotFound | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from controllers.files import api | 
					
						
							|  |  |  | from core.tools.tool_file_manager import ToolFileManager | 
					
						
							|  |  |  | from libs.exception import BaseHTTPException | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ToolFilePreviewApi(Resource): | 
					
						
							|  |  |  |     def get(self, file_id, extension): | 
					
						
							|  |  |  |         file_id = str(file_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         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") | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if not ToolFileManager.verify_file( | 
					
						
							|  |  |  |             file_id=file_id, | 
					
						
							|  |  |  |             timestamp=args["timestamp"], | 
					
						
							|  |  |  |             nonce=args["nonce"], | 
					
						
							|  |  |  |             sign=args["sign"], | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         ): | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             raise Forbidden("Invalid request.") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             result = ToolFileManager.get_file_generator_by_tool_file_id( | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |                 file_id, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if not result: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 raise NotFound("file is not found") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             generator, mimetype = result | 
					
						
							|  |  |  |         except Exception: | 
					
						
							|  |  |  |             raise UnsupportedFileTypeError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Response(generator, mimetype=mimetype) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | api.add_resource(ToolFilePreviewApi, "/files/tools/<uuid:file_id>.<string:extension>") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class UnsupportedFileTypeError(BaseHTTPException): | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |     error_code = "unsupported_file_type" | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     description = "File type not allowed." | 
					
						
							|  |  |  |     code = 415 |