使用新版模板 (#313)

* 🔥 删除现有模板

*  自动克隆模板仓库

* 🔥 删除 identicon 相关代码

* 🚚 修改静态文件路径

*  使用新模板进行渲染

*  每次渲染都获取一次模板, 以应对实时更新

*  TETR.IO 绑定图使用新模板

* 🚚 修改网络路径

*  TOP 绑定图使用新模板

*  TOS 绑定图使用新模板

* 🐛 防止截图超时

* 🐛 Pydantic V1 会把 float 转换成 int

* ✏️ 模板字段名写错了

*  兼容 Pydantic V1

*  TETR.IO 查询图使用新模板

* 🐛 在查询的用户没有历史记录时不去查询更多记录
This commit is contained in:
呵呵です
2024-05-10 09:41:05 +08:00
committed by GitHub
parent e47f1bb6f9
commit 716e392a3a
54 changed files with 280 additions and 1826 deletions

View File

@@ -2,17 +2,14 @@ from hashlib import sha256
from ipaddress import IPv4Address, IPv6Address
from typing import ClassVar
from aiofiles import open
from fastapi import FastAPI, Query, Response, status
from fastapi.responses import FileResponse, HTMLResponse
from fastapi import FastAPI, status
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from nonebot import get_app, get_driver
from nonebot.log import logger
from nonebot_plugin_localstore import get_cache_dir # type: ignore[import-untyped]
from pydantic import IPvAnyAddress
from ..templates import path
from .avatar import generate_identicon
from .templates import templates_dir
app = get_app()
@@ -43,32 +40,19 @@ class HostPage:
app.mount(
'/static',
StaticFiles(directory=path),
name='static',
'/host/assets',
StaticFiles(directory=templates_dir / 'assets'),
name='assets',
)
@app.get('/host/page/{page_hash}.html', status_code=status.HTTP_200_OK)
@app.get('/host/{page_hash}.html', status_code=status.HTTP_200_OK)
async def _(page_hash: str) -> HTMLResponse:
if page_hash in HostPage.pages:
return HTMLResponse(HostPage.pages[page_hash])
return NOT_FOUND
@app.get('/identicon')
async def _(md5: str = Query(regex=r'^[a-fA-F0-9]{32}$')):
identicon_path = cache_dir / 'identicon' / f'{md5}.svg'
if identicon_path.exists() is False:
identicon_path.parent.mkdir(parents=True, exist_ok=True)
result = await generate_identicon(md5)
async with open(identicon_path, mode='xb') as file:
await file.write(result)
return Response(result, media_type='image/svg+xml')
logger.debug('Identicon Cache hit!')
return FileResponse(identicon_path, media_type='image/svg+xml')
def get_self_netloc() -> str:
host: IPv4Address | IPv6Address | IPvAnyAddress = global_config.host
if isinstance(host, IPv4Address):