| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | from flask import Response | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  | from flask_restful import Resource, reqparse  # type: ignore | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2024-10-07 11:08:06 +08:00
										 |  |  | from controllers.files.error import UnsupportedFileTypeError | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from core.tools.tool_file_manager import ToolFileManager | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-10-21 10:43:49 +08:00
										 |  |  |         parser.add_argument("as_attachment", type=bool, required=False, default=False, 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-10-21 10:43:49 +08:00
										 |  |  |             stream, tool_file = ToolFileManager.get_file_generator_by_tool_file_id( | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |                 file_id, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |             if not stream or not tool_file: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 raise NotFound("file is not found") | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         except Exception: | 
					
						
							|  |  |  |             raise UnsupportedFileTypeError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         response = Response( | 
					
						
							|  |  |  |             stream, | 
					
						
							|  |  |  |             mimetype=tool_file.mimetype, | 
					
						
							|  |  |  |             direct_passthrough=True, | 
					
						
							| 
									
										
										
										
											2024-10-22 17:24:42 +08:00
										 |  |  |             headers={}, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-10-22 17:24:42 +08:00
										 |  |  |         if tool_file.size > 0: | 
					
						
							|  |  |  |             response.headers["Content-Length"] = str(tool_file.size) | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         if args["as_attachment"]: | 
					
						
							|  |  |  |             response.headers["Content-Disposition"] = f"attachment; filename={tool_file.name}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return response | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | api.add_resource(ToolFilePreviewApi, "/files/tools/<uuid:file_id>.<string:extension>") |