Underground-Digital/Workflow-Engine
0
1from flask import Response, request2from flask_restful import Resource, reqparse3from werkzeug.exceptions import NotFound4 5import services6from controllers.files import api7from controllers.files.error import UnsupportedFileTypeError8from services.account_service import TenantService9from services.file_service import FileService10 11 12class ImagePreviewApi(Resource):13 """14 Deprecated15 """16 17 def get(self, file_id):18 file_id = str(file_id)19 20 timestamp = request.args.get("timestamp")21 nonce = request.args.get("nonce")22 sign = request.args.get("sign")23 24 if not timestamp or not nonce or not sign:25 return {"content": "Invalid request."}, 40026 27 try:28 generator, mimetype = FileService.get_image_preview(29 file_id=file_id,30 timestamp=timestamp,31 nonce=nonce,32 sign=sign,33 )34 except services.errors.file.UnsupportedFileTypeError:35 raise UnsupportedFileTypeError()36 37 return Response(generator, mimetype=mimetype)38 39 40class FilePreviewApi(Resource):41 def get(self, file_id):42 file_id = str(file_id)43 44 parser = reqparse.RequestParser()45 parser.add_argument("timestamp", type=str, required=True, location="args")46 parser.add_argument("nonce", type=str, required=True, location="args")47 parser.add_argument("sign", type=str, required=True, location="args")48 parser.add_argument("as_attachment", type=bool, required=False, default=False, location="args")49 50 args = parser.parse_args()51 52 if not args["timestamp"] or not args["nonce"] or not args["sign"]:53 return {"content": "Invalid request."}, 40054 55 try:56 generator, upload_file = FileService.get_file_generator_by_file_id(57 file_id=file_id,58 timestamp=args["timestamp"],59 nonce=args["nonce"],60 sign=args["sign"],61 )62 except services.errors.file.UnsupportedFileTypeError:63 raise UnsupportedFileTypeError()64 65 response = Response(66 generator,67 mimetype=upload_file.mime_type,68 direct_passthrough=True,69 headers={},70 )71 if upload_file.size > 0:72 response.headers["Content-Length"] = str(upload_file.size)73 if args["as_attachment"]:74 response.headers["Content-Disposition"] = f"attachment; filename={upload_file.name}"75 76 return response77 78 79class WorkspaceWebappLogoApi(Resource):80 def get(self, workspace_id):81 workspace_id = str(workspace_id)82 83 custom_config = TenantService.get_custom_config(workspace_id)84 webapp_logo_file_id = custom_config.get("replace_webapp_logo") if custom_config is not None else None85 86 if not webapp_logo_file_id:87 raise NotFound("webapp logo is not found")88 89 try:90 generator, mimetype = FileService.get_public_image_preview(91 webapp_logo_file_id,92 )93 except services.errors.file.UnsupportedFileTypeError:94 raise UnsupportedFileTypeError()95 96 return Response(generator, mimetype=mimetype)97 98 99api.add_resource(ImagePreviewApi, "/files/<uuid:file_id>/image-preview")100api.add_resource(FilePreviewApi, "/files/<uuid:file_id>/file-preview")101api.add_resource(WorkspaceWebappLogoApi, "/files/workspaces/<uuid:workspace_id>/webapp-logo")102 