mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-08 05:10:01 +08:00
🐛 修复web zip更新路径问题
This commit is contained in:
@@ -110,7 +110,8 @@ async def _(
|
||||
try:
|
||||
result += await UpdateManager.update_webui(
|
||||
source_str, # type: ignore
|
||||
"dist",
|
||||
"test",
|
||||
True,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("WebUI更新失败...", "检查更新", session=session, e=e)
|
||||
|
||||
@@ -34,7 +34,7 @@ class UpdateManager:
|
||||
async def update_webui(
|
||||
cls,
|
||||
source: Literal["git", "ali"] | None,
|
||||
branch: str = "main",
|
||||
branch: str = "dist",
|
||||
force: bool = False,
|
||||
):
|
||||
"""更新WebUI
|
||||
|
||||
@@ -8,16 +8,6 @@ if sys.version_info >= (3, 11):
|
||||
else:
|
||||
from strenum import StrEnum
|
||||
|
||||
from zhenxun.configs.path_config import DATA_PATH, TEMP_PATH
|
||||
|
||||
WEBUI_STRING = "web_ui"
|
||||
PUBLIC_STRING = "public"
|
||||
|
||||
WEBUI_DATA_PATH = DATA_PATH / WEBUI_STRING
|
||||
PUBLIC_PATH = WEBUI_DATA_PATH / PUBLIC_STRING
|
||||
TMP_PATH = TEMP_PATH / WEBUI_STRING
|
||||
|
||||
WEBUI_DIST_GITHUB_URL = "https://github.com/HibiKier/zhenxun_bot_webui/tree/dist"
|
||||
|
||||
app = nonebot.get_app()
|
||||
|
||||
|
||||
@@ -3,41 +3,38 @@ from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
from ..config import PUBLIC_PATH
|
||||
from .data_source import COMMAND_NAME, update_webui_assets
|
||||
from zhenxun.utils.manager.zhenxun_repo_manager import ZhenxunRepoManager
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/")
|
||||
async def index():
|
||||
return FileResponse(PUBLIC_PATH / "index.html")
|
||||
return FileResponse(ZhenxunRepoManager.config.WEBUI_PATH / "index.html")
|
||||
|
||||
|
||||
@router.get("/favicon.ico")
|
||||
async def favicon():
|
||||
return FileResponse(PUBLIC_PATH / "favicon.ico")
|
||||
|
||||
|
||||
@router.get("/79edfa81f3308a9f.jfif")
|
||||
async def _():
|
||||
return FileResponse(PUBLIC_PATH / "79edfa81f3308a9f.jfif")
|
||||
return FileResponse(ZhenxunRepoManager.config.WEBUI_PATH / "favicon.ico")
|
||||
|
||||
|
||||
async def init_public(app: FastAPI):
|
||||
try:
|
||||
if not PUBLIC_PATH.exists():
|
||||
folders = await update_webui_assets()
|
||||
else:
|
||||
folders = [x.name for x in PUBLIC_PATH.iterdir() if x.is_dir()]
|
||||
if not ZhenxunRepoManager.check_webui_exists():
|
||||
await ZhenxunRepoManager.webui_update(branch="test")
|
||||
folders = [
|
||||
x.name for x in ZhenxunRepoManager.config.WEBUI_PATH.iterdir() if x.is_dir()
|
||||
]
|
||||
app.include_router(router)
|
||||
for pathname in folders:
|
||||
logger.debug(f"挂载文件夹: {pathname}")
|
||||
app.mount(
|
||||
f"/{pathname}",
|
||||
StaticFiles(directory=PUBLIC_PATH / pathname, check_dir=True),
|
||||
StaticFiles(
|
||||
directory=ZhenxunRepoManager.config.WEBUI_PATH / pathname,
|
||||
check_dir=True,
|
||||
),
|
||||
name=f"public_{pathname}",
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("初始化 WebUI资源 失败", COMMAND_NAME, e=e)
|
||||
logger.error("初始化 WebUI资源 失败", "WebUI", e=e)
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import zipfile
|
||||
|
||||
from nonebot.utils import run_sync
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.github_utils import GithubUtils
|
||||
from zhenxun.utils.http_utils import AsyncHttpx
|
||||
|
||||
from ..config import PUBLIC_PATH, TMP_PATH, WEBUI_DIST_GITHUB_URL
|
||||
|
||||
COMMAND_NAME = "WebUI资源管理"
|
||||
|
||||
|
||||
async def update_webui_assets():
|
||||
webui_assets_path = TMP_PATH / "webui_assets.zip"
|
||||
download_url = await GithubUtils.parse_github_url(
|
||||
WEBUI_DIST_GITHUB_URL
|
||||
).get_archive_download_urls()
|
||||
logger.info("开始下载 webui_assets 资源...", COMMAND_NAME)
|
||||
if await AsyncHttpx.download_file(
|
||||
download_url, webui_assets_path, follow_redirects=True
|
||||
):
|
||||
logger.info("下载 webui_assets 成功...", COMMAND_NAME)
|
||||
return await _file_handle(webui_assets_path)
|
||||
raise Exception("下载 webui_assets 失败", COMMAND_NAME)
|
||||
|
||||
|
||||
@run_sync
|
||||
def _file_handle(webui_assets_path: Path):
|
||||
logger.debug("开始解压 webui_assets...", COMMAND_NAME)
|
||||
if webui_assets_path.exists():
|
||||
tf = zipfile.ZipFile(webui_assets_path)
|
||||
tf.extractall(TMP_PATH)
|
||||
logger.debug("解压 webui_assets 成功...", COMMAND_NAME)
|
||||
else:
|
||||
raise Exception("解压 webui_assets 失败,文件不存在...", COMMAND_NAME)
|
||||
download_file_path = next(f for f in TMP_PATH.iterdir() if f.is_dir())
|
||||
shutil.rmtree(PUBLIC_PATH, ignore_errors=True)
|
||||
shutil.copytree(download_file_path / "dist", PUBLIC_PATH, dirs_exist_ok=True)
|
||||
logger.debug("复制 webui_assets 成功...", COMMAND_NAME)
|
||||
shutil.rmtree(TMP_PATH, ignore_errors=True)
|
||||
return [x.name for x in PUBLIC_PATH.iterdir() if x.is_dir()]
|
||||
Reference in New Issue
Block a user