mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-05 03:39:59 +08:00
@@ -1,7 +1,17 @@
|
||||
import nonebot
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from pydantic import BaseModel
|
||||
from strenum import StrEnum
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
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()
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
from fastapi import APIRouter, FastAPI
|
||||
from fastapi import FastAPI, APIRouter
|
||||
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 update_webui_assets
|
||||
from ..config import PUBLIC_PATH
|
||||
from .data_source import COMMAND_NAME, update_webui_assets
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -23,13 +23,16 @@ async def favicon():
|
||||
async def init_public(app: FastAPI):
|
||||
try:
|
||||
if not PUBLIC_PATH.exists():
|
||||
await update_webui_assets()
|
||||
folders = await update_webui_assets()
|
||||
else:
|
||||
folders = [x.name for x in PUBLIC_PATH.iterdir()]
|
||||
app.include_router(router)
|
||||
for pathname in ["css", "js", "fonts", "img"]:
|
||||
for pathname in folders:
|
||||
logger.debug(f"挂载文件夹: {pathname}")
|
||||
app.mount(
|
||||
f"/{pathname}",
|
||||
StaticFiles(directory=PUBLIC_PATH / pathname, check_dir=True),
|
||||
name=f"public_{pathname}",
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"初始化 web ui assets 失败", "Web UI assets", e=e)
|
||||
logger.error("初始化 WebUI资源 失败", COMMAND_NAME, e=e)
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
from datetime import datetime
|
||||
from pydantic import BaseModel
|
||||
from zhenxun.configs.path_config import DATA_PATH, TEMP_PATH
|
||||
|
||||
|
||||
class PublicData(BaseModel):
|
||||
etag: str
|
||||
update_time: datetime
|
||||
|
||||
|
||||
COMMAND_NAME = "webui_update_assets"
|
||||
|
||||
WEBUI_DATA_PATH = DATA_PATH / "web_ui"
|
||||
PUBLIC_PATH = WEBUI_DATA_PATH / "public"
|
||||
TMP_PATH = TEMP_PATH / "web_ui"
|
||||
|
||||
GITHUB_API_COMMITS = "https://api.github.com/repos/HibiKier/zhenxun_bot_webui/commits"
|
||||
WEBUI_ASSETS_DOWNLOAD_URL = (
|
||||
"https://github.com/HibiKier/zhenxun_bot_webui/archive/refs/heads/dist.zip"
|
||||
)
|
||||
@@ -1,4 +1,3 @@
|
||||
import os
|
||||
import shutil
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
@@ -7,14 +6,20 @@ from nonebot.utils import run_sync
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.http_utils import AsyncHttpx
|
||||
from zhenxun.utils.github_utils import parse_github_url
|
||||
|
||||
from .config import COMMAND_NAME, PUBLIC_PATH, TMP_PATH, WEBUI_ASSETS_DOWNLOAD_URL
|
||||
from ..config import TMP_PATH, PUBLIC_PATH, WEBUI_DIST_GITHUB_URL
|
||||
|
||||
COMMAND_NAME = "WebUI资源管理"
|
||||
|
||||
|
||||
async def update_webui_assets():
|
||||
webui_assets_path = TMP_PATH / "webui_assets.zip"
|
||||
download_url = await parse_github_url(
|
||||
WEBUI_DIST_GITHUB_URL
|
||||
).get_archive_download_url()
|
||||
if await AsyncHttpx.download_file(
|
||||
WEBUI_ASSETS_DOWNLOAD_URL, webui_assets_path, follow_redirects=True
|
||||
download_url, webui_assets_path, follow_redirects=True
|
||||
):
|
||||
logger.info("下载 webui_assets 成功...", COMMAND_NAME)
|
||||
return await _file_handle(webui_assets_path)
|
||||
@@ -30,10 +35,9 @@ def _file_handle(webui_assets_path: Path):
|
||||
logger.debug("解压 webui_assets 成功...", COMMAND_NAME)
|
||||
else:
|
||||
raise Exception("解压 webui_assets 失败,文件不存在...", COMMAND_NAME)
|
||||
download_file_path = (
|
||||
TMP_PATH / [x for x in os.listdir(TMP_PATH) if (TMP_PATH / x).is_dir()][0]
|
||||
)
|
||||
download_file_path = TMP_PATH / next(iter(TMP_PATH.iterdir()))
|
||||
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()]
|
||||
|
||||
Reference in New Issue
Block a user