From 4f4a9b9e5507278a8b573a386a16b90bd50e1b4f Mon Sep 17 00:00:00 2001 From: LIghtJUNction Date: Sun, 26 Oct 2025 15:35:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(io.py):=20download=5Fdashboard=E5=A6=82?= =?UTF-8?q?=E6=9E=9C=E5=8F=91=E7=8E=B0=E6=B2=A1=E6=9C=89dist/assets/versio?= =?UTF-8?q?n=E6=96=87=E4=BB=B6=EF=BC=8C=E4=B8=8B=E8=BD=BD=E5=AE=8C?= =?UTF-8?q?=E6=AF=95=E8=87=AA=E5=8A=A8=E5=86=99=E5=85=A5=EF=BC=88=E4=BB=A5?= =?UTF-8?q?=E9=98=B2=E4=B8=87=E4=B8=80=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/cli/utils/basic.py | 2 +- astrbot/core/utils/io.py | 30 +++++++++++++++++++++++------- pyproject.toml | 1 + 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/astrbot/cli/utils/basic.py b/astrbot/cli/utils/basic.py index fabced48..9c9e1556 100644 --- a/astrbot/cli/utils/basic.py +++ b/astrbot/cli/utils/basic.py @@ -37,7 +37,7 @@ async def check_dashboard(astrbot_root: Path) -> None: ): click.echo("正在安装管理面板...") await download_dashboard( - path="data/dashboard.zip", + path="data", extract_path=str(astrbot_root), version=f"v{VERSION}", latest=False, diff --git a/astrbot/core/utils/io.py b/astrbot/core/utils/io.py index 723953f8..5e8662d0 100644 --- a/astrbot/core/utils/io.py +++ b/astrbot/core/utils/io.py @@ -1,4 +1,5 @@ import os +from pathlib import Path import ssl import shutil import socket @@ -150,7 +151,7 @@ async def download_file(url: str, path: str, show_progress: bool = False): f.write(chunk) downloaded_size += len(chunk) if show_progress: - elapsed_time = time.time() - start_time + elapsed_time = time.time() - start_time if time.time() - start_time > 0 else 1 speed = downloaded_size / 1024 / elapsed_time # KB/s print( f"\r下载进度: {downloaded_size / total_size:.2%} 速度: {speed:.2f} KB/s", @@ -221,10 +222,16 @@ async def download_dashboard( latest: bool = True, version: str | None = None, proxy: str | None = None, -): +) -> None: """下载管理面板文件""" + if path is None: - path = os.path.join(get_astrbot_data_path(), "dashboard.zip") + _path = Path(get_astrbot_data_path()).absolute() + else: + _path = Path(path).absolute() + + zip_path = _path / "dashboard.zip" + version_file = _path / "dist" / "assets" / "version" if latest or len(str(version)) != 40: ver_name = "latest" if latest else version @@ -233,7 +240,7 @@ async def download_dashboard( f"准备下载指定发行版本的 AstrBot WebUI 文件: {dashboard_release_url}" ) try: - await download_file(dashboard_release_url, path, show_progress=True) + await download_file(dashboard_release_url, str(zip_path), show_progress=True) except BaseException as _: if latest: dashboard_release_url = "https://github.com/Soulter/AstrBot/releases/latest/download/dist.zip" @@ -241,12 +248,21 @@ async def download_dashboard( dashboard_release_url = f"https://github.com/Soulter/AstrBot/releases/download/{version}/dist.zip" if proxy: dashboard_release_url = f"{proxy}/{dashboard_release_url}" - await download_file(dashboard_release_url, path, show_progress=True) + await download_file(dashboard_release_url, str(zip_path), show_progress=True) else: url = f"https://github.com/AstrBotDevs/astrbot-release-harbour/releases/download/release-{version}/dist.zip" logger.info(f"准备下载指定版本的 AstrBot WebUI: {url}") if proxy: url = f"{proxy}/{url}" - await download_file(url, path, show_progress=True) - with zipfile.ZipFile(path, "r") as z: + await download_file(url, str(zip_path), show_progress=True) + with zipfile.ZipFile(zip_path, "r") as z: z.extractall(extract_path) + + if version_file.exists(): + return + + # 写入dist/version + # https://github.com/AstrBotDevs/AstrBot/pull/3106 + # 实际上压根没有dist/version文件,这里需要写入 + with version_file.open("w") as f: + f.write(f"{version}") diff --git a/pyproject.toml b/pyproject.toml index 7acc3a81..9cc6a3df 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -89,6 +89,7 @@ target-version = "py310" [dependency-groups] dev = [ + "commitizen>=4.9.1", "pytest>=8.4.1", "pytest-asyncio>=1.1.0", "pytest-cov>=6.2.1",