From c5ccc1a08402973cb3312c8a58c74b9185be1037 Mon Sep 17 00:00:00 2001 From: Raven95676 Date: Thu, 15 May 2025 09:50:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(Video):=20=E5=A2=9E=E5=8A=A0=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E6=B6=88=E6=81=AF=E7=BB=84=E4=BB=B6=E7=9A=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=BD=AC=E6=8D=A2=E5=92=8C=E6=B3=A8=E5=86=8C=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/message/components.py | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/astrbot/core/message/components.py b/astrbot/core/message/components.py index a7474cb77..97b712d0b 100644 --- a/astrbot/core/message/components.py +++ b/astrbot/core/message/components.py @@ -250,6 +250,51 @@ class Video(BaseMessageComponent): return Video(file=url, **_) raise Exception("not a valid url") + async def convert_to_file_path(self) -> str: + """将这个视频统一转换为本地文件路径。这个方法避免了手动判断视频数据类型,直接返回视频数据的本地路径(如果是网络 URL,则会自动进行下载)。 + + Returns: + str: 视频的本地路径,以绝对路径表示。 + """ + url = self.file + if url and url.startswith("file:///"): + return url[8:] + elif url and url.startswith("http"): + download_dir = os.path.join(get_astrbot_data_path(), "temp") + video_file_path = os.path.join(download_dir, f"{uuid.uuid4().hex}") + await download_file(url, video_file_path) + if os.path.exists(video_file_path): + return os.path.abspath(video_file_path) + else: + raise Exception(f"download failed: {url}") + elif os.path.exists(url): + return os.path.abspath(url) + else: + raise Exception(f"not a valid file: {url}") + + async def register_to_file_service(self): + """ + 将视频注册到文件服务。 + + Returns: + str: 注册后的URL + + Raises: + Exception: 如果未配置 callback_api_base + """ + callback_host = astrbot_config.get("callback_api_base") + + if not callback_host: + raise Exception("未配置 callback_api_base,文件服务不可用") + + file_path = await self.convert_to_file_path() + + token = await file_token_service.register_file(file_path) + + logger.debug(f"已注册:{callback_host}/api/file/{token}") + + return f"{callback_host}/api/file/{token}" + class At(BaseMessageComponent): type: ComponentType = "At"