From 7d46314dc832e8cd955fe162813ca2f1a6f49ddd Mon Sep 17 00:00:00 2001 From: MUKAPP Date: Tue, 12 Aug 2025 21:47:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=97=B6=E7=94=B1=E4=BA=8E=20file:///=20?= =?UTF-8?q?=E5=89=8D=E7=BC=80=EF=BC=8C=E5=AF=BC=E8=87=B4=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=A2=AB=E8=AF=AF=E5=88=A4=E4=B8=BA=E4=B8=8D=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20(#2325)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes #2222 --- astrbot/core/file_token_service.py | 34 +++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/astrbot/core/file_token_service.py b/astrbot/core/file_token_service.py index 2ed46d433..ce5e2349a 100644 --- a/astrbot/core/file_token_service.py +++ b/astrbot/core/file_token_service.py @@ -2,6 +2,8 @@ import asyncio import os import uuid import time +from urllib.parse import urlparse, unquote +import platform class FileTokenService: @@ -15,7 +17,9 @@ class FileTokenService: async def _cleanup_expired_tokens(self): """清理过期的令牌""" now = time.time() - expired_tokens = [token for token, (_, expire) in self.staged_files.items() if expire < now] + expired_tokens = [ + token for token, (_, expire) in self.staged_files.items() if expire < now + ] for token in expired_tokens: self.staged_files.pop(token, None) @@ -32,15 +36,35 @@ class FileTokenService: Raises: FileNotFoundError: 当路径不存在时抛出 """ + + # 处理 file:/// + try: + parsed_uri = urlparse(file_path) + if parsed_uri.scheme == "file": + local_path = unquote(parsed_uri.path) + if platform.system() == "Windows" and local_path.startswith("/"): + local_path = local_path[1:] + else: + # 如果没有 file:/// 前缀,则认为是普通路径 + local_path = file_path + except Exception: + # 解析失败时,按原路径处理 + local_path = file_path + async with self.lock: await self._cleanup_expired_tokens() - if not os.path.exists(file_path): - raise FileNotFoundError(f"文件不存在: {file_path}") + if not os.path.exists(local_path): + raise FileNotFoundError( + f"文件不存在: {local_path} (原始输入: {file_path})" + ) file_token = str(uuid.uuid4()) - expire_time = time.time() + (timeout if timeout is not None else self.default_timeout) - self.staged_files[file_token] = (file_path, expire_time) + expire_time = time.time() + ( + timeout if timeout is not None else self.default_timeout + ) + # 存储转换后的真实路径 + self.staged_files[file_token] = (local_path, expire_time) return file_token async def handle_file(self, file_token: str) -> str: