From 435e4cecd86589356db263d8b17f784fada18b60 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 17 Jul 2026 17:51:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20webpath=5Fto=5Fbas?= =?UTF-8?q?e64=20=E5=87=BD=E6=95=B0=EF=BC=8Cwebpath=20=E8=BD=AC=20base64?= =?UTF-8?q?=20data=20URI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ahserver/filestorage.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ahserver/filestorage.py b/ahserver/filestorage.py index 60462aa..d363704 100644 --- a/ahserver/filestorage.py +++ b/ahserver/filestorage.py @@ -224,6 +224,20 @@ def get_baseurl(): baseurl = os.environ.get('BASEURL') or os.environ.get('_LAST_BASEURL') return baseurl +def webpath_to_base64(webpath): + """将 webpath 转为 base64 data URI,供 kimi 等多模态 uapi 使用""" + import mimetypes + fs = FileStorage() + fp = fs.realPath(webpath) + if not os.path.isfile(fp): + return '' + mime, _ = mimetypes.guess_type(fp) + if not mime: + mime = 'application/octet-stream' + with open(fp, 'rb') as f: + b64 = base64.b64encode(f.read()).decode('utf-8') + return f'data:{mime};base64,{b64}' + async def base642file(b64str): filename = getFilenameFromBase64(b64str) if ',' in b64str: @@ -267,4 +281,7 @@ async def data2qrpath(data, logo=None): gen_qr(data, path) return fs.webpath(path) +from appPublic.registerfunction import registerFunction +registerFunction('webpath_to_base64', webpath_to_base64) +