diff --git a/app/api/v1/api.py b/app/api/v1/api.py index c9e89d0..6bf6f88 100644 --- a/app/api/v1/api.py +++ b/app/api/v1/api.py @@ -7,8 +7,8 @@ from apiflask import APIBlueprint from flask import session from app import rpc -from app.api.v1.exception.api import ImageUploadError, UserInfoError -from app.api.v1.schema.api import ImageIn +from app.api.v1.exception.api import ImageUploadError, UserInfoError, ImageNotFound +from app.api.v1.schema.api import ImageIn, ImagePreSignUrlOut, ImagePreSignUrlIn from app.util.auth import login_required api = APIBlueprint('api', __name__) @@ -18,7 +18,7 @@ api = APIBlueprint('api', __name__) @api.doc(summary='上传图片', description='上传图片') @api.input(ImageIn, location='files') @login_required -def wechat_login(files_data): +def upload_image(files_data): f = files_data['image'] try: result = rpc.storage.upload(file_name=str(session['user_id']+f'_{f.filename}'), @@ -41,6 +41,21 @@ def wechat_login(files_data): return {'msg': 'uploading image fail'} +@api.get('/images/presign_url') +@api.input(ImagePreSignUrlIn, location='query') +@api.output(ImagePreSignUrlOut) +@api.doc(summary='获取图片预签名链接', description='获取图片预签名链接') +def get_image_presign_url(query_data): + try: + result = rpc.storage.get_presign_url(query_data['avatar_id'], + 'bodyrecord', + bucket='bodyrecord', + expire_time=query_data.get('expire_time', 3600)) + except Exception as e: + raise ImageNotFound(extra_data={'error_docs': str(e)}) + return result + + @api.get('/hello') def hello(): result_one = f'我是您的专属接口提供服务器:' diff --git a/app/api/v1/exception/api.py b/app/api/v1/exception/api.py index 53ab0f7..0d2fd2c 100644 --- a/app/api/v1/exception/api.py +++ b/app/api/v1/exception/api.py @@ -9,3 +9,8 @@ class ImageUploadError(HTTPError): class UserInfoError(HTTPError): status_code = 500 message = '设置用户信息失败' + + +class ImageNotFound(HTTPError): + status_code = 404 + message = '获取图片链接失败' diff --git a/app/api/v1/schema/api.py b/app/api/v1/schema/api.py index c4a7970..f7c5ce0 100644 --- a/app/api/v1/schema/api.py +++ b/app/api/v1/schema/api.py @@ -1,12 +1,17 @@ from apiflask import Schema -from apiflask.fields import File +from apiflask.fields import File, URL, String, Integer class ImageIn(Schema): image = File() -class FileOut(Schema): - file = File() +class ImagePreSignUrlIn(Schema): + avatar_id = String(required=True) + expire_time = Integer() + + +class ImagePreSignUrlOut(Schema): + image_url = URL()