mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-11 06:39:59 +08:00
✨ feat(hook): 增强认证钩子和运行时缓存管理 (#2106)
* ✨ feat(hook): 增强认证钩子和运行时缓存管理 ``` ♻ refactor(hook): 移除未使用的配置项并优化缓存设置 移除 AUTH_HOOKS_CONCURRENCY_LIMIT 配置项,该配置项不再使用 ✨feat(auth_ban): 简化缓存配置并添加实体参数支持 将 BAN_CACHE_TTL 相关配置从动态配置改为常量定义, 移除复杂的 TTL 值转换逻辑,并为 auth_ban 函数添加可选的 entity 参数以支持外部传入实体信息 ♻ refactor(auth_limit): 移除未使用的配置依赖 移除 AUTH_LIMIT_NOTICE_CD 配置项,直接使用常量值 2 作为限制通知冷却时间 📦 依赖更新: update playwright dependency to version 1.57.0 in pyproject.toml and requirements.txt ``` * ✨ feat(auth_checker): 增强插件模块处理和预过滤逻辑,支持用户插件兼容性 * ✨ feat(http_utils): 添加内容缓存机制以优化HTTP响应处理 * ✨ feat(cache): 添加群组插件设置视图缓存类型并更新相关逻辑 * ✨ feat(renderer): 优化渲染引擎,增加内存缓存管理和HTML文档处理逻辑 * ✨ feat(sign_in): 添加好感度排行和好感度总排行命令 * ✨ feat(renderer): 增强浏览器实例管理和模板预处理,支持历史 include 语法兼容 * ✨ feat(renderer): 优化 Playwright 环境检查逻辑,增加结果缓存以提高性能 * ✨ feat(renderer): 增强模板文件渲染策略,优化资产加载路径处理 * 🚨 auto fix by pre-commit hooks * ✨ feat(log): 增加日志内容安全序列化,避免超长 base64 等污染日志 ✨ feat(log_sanitizer): 添加对嵌入超长 base64/data URI 的清理功能 ✨ feat(auth_checker): 添加 Alconna 快捷方式缓存检查,优化路由匹配逻辑 * feat(renderer): 添加渲染结果内存缓存功能以优化性能 feat(help): 实现帮助菜单图像缓存机制 feat(sign_in): 更新HTML卡片生成以支持动画禁用和剪裁 feat(superuser): 在启动时预热超级用户帮助缓存 feat(theme): 优化主题管理器的资源解析缓存 * 🚨 auto fix by pre-commit hooks * feat(renderer): 增加全页面视口最大宽度限制并优化内容尺寸计算 --------- Co-authored-by: ATTomatoo <1126160939@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: HibiKier <45528451+HibiKier@users.noreply.github.com>
This commit is contained in:
co-authored by
ATTomatoo
pre-commit-ci[bot]
HibiKier
parent
51f4773e14
commit
ce94f63d9a
@@ -1,6 +1,7 @@
|
||||
from collections.abc import Callable
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from jinja2 import (
|
||||
@@ -11,6 +12,7 @@ from jinja2 import (
|
||||
select_autoescape,
|
||||
)
|
||||
|
||||
from zhenxun.configs.config import Config
|
||||
from zhenxun.configs.path_config import THEMES_PATH
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.services.renderer.theme import DependencyCollector
|
||||
@@ -25,6 +27,11 @@ from zhenxun.utils.exception import RenderingError
|
||||
if TYPE_CHECKING:
|
||||
from .types import RenderContext
|
||||
|
||||
_LEGACY_INCLUDE_TAG_PATTERN = re.compile(
|
||||
r"\{\s*%\s*include\s+([\"'][^\"']+[\"'])\s*%\s*\}",
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
|
||||
|
||||
class RelativePathEnvironment(Environment):
|
||||
"""
|
||||
@@ -37,6 +44,22 @@ class RelativePathEnvironment(Environment):
|
||||
return path.replace(os.path.sep, "/")
|
||||
return super().join_path(template, parent)
|
||||
|
||||
def preprocess(
|
||||
self,
|
||||
source: str,
|
||||
name: str | None = None,
|
||||
filename: str | None = None,
|
||||
) -> str:
|
||||
"""
|
||||
兼容历史模板里的错误 include 语法:
|
||||
{
|
||||
% include './style.css' %
|
||||
}
|
||||
自动归一化为标准 Jinja 语法,避免页面样式丢失。
|
||||
"""
|
||||
normalized_source = _LEGACY_INCLUDE_TAG_PATTERN.sub(r"{% include \1 %}", source)
|
||||
return super().preprocess(normalized_source, name, filename)
|
||||
|
||||
|
||||
class JinjaTemplateEngine:
|
||||
"""
|
||||
@@ -143,6 +166,11 @@ class JinjaTemplateEngine:
|
||||
}
|
||||
return await base_template.render_async(**page_context)
|
||||
else:
|
||||
# 对于本身已是完整 HTML 文档的模板,直接返回,避免重复拼接大段 CSS。
|
||||
html_head = html_fragment.lstrip()[:32].lower()
|
||||
if html_head.startswith("<!doctype html") or html_head.startswith("<html"):
|
||||
return html_fragment
|
||||
|
||||
style_blocks: list[str] = []
|
||||
if theme_css_content:
|
||||
style_blocks.append(theme_css_content)
|
||||
@@ -210,13 +238,21 @@ class ComponentRenderStrategy(RenderStrategy):
|
||||
screenshot_options = final_render_options.copy()
|
||||
screenshot_options.pop("extra_css", None)
|
||||
screenshot_options.pop("frameless", None)
|
||||
screenshot_options.pop("_keep_html_content", None)
|
||||
|
||||
image_bytes = await context.screenshot_engine.render(
|
||||
html=html_content,
|
||||
base_url_path=THEMES_PATH.parent,
|
||||
**screenshot_options,
|
||||
)
|
||||
return RenderResult(image_bytes=image_bytes, html_content=html_content)
|
||||
keep_html_content = bool(
|
||||
context.render_options.get("_keep_html_content", False)
|
||||
)
|
||||
debug_mode = Config.get_config("UI", "DEBUG_MODE", False)
|
||||
return RenderResult(
|
||||
image_bytes=image_bytes,
|
||||
html_content=html_content if debug_mode or keep_html_content else None,
|
||||
)
|
||||
|
||||
|
||||
class TemplateFileRenderStrategy(RenderStrategy):
|
||||
@@ -230,8 +266,12 @@ class TemplateFileRenderStrategy(RenderStrategy):
|
||||
|
||||
template_dir = template_path.parent
|
||||
temp_loader = FileSystemLoader(str(template_dir))
|
||||
temp_env = Environment(
|
||||
loader=temp_loader,
|
||||
base_loader = context.template_engine.env.loader
|
||||
temp_env_loader = (
|
||||
ChoiceLoader([temp_loader, base_loader]) if base_loader else temp_loader
|
||||
)
|
||||
temp_env = RelativePathEnvironment(
|
||||
loader=temp_env_loader,
|
||||
enable_async=True,
|
||||
autoescape=select_autoescape(["html", "xml"]),
|
||||
)
|
||||
@@ -264,7 +304,17 @@ class TemplateFileRenderStrategy(RenderStrategy):
|
||||
if getattr(component, "is_page", False):
|
||||
final_render_options["frameless"] = True
|
||||
|
||||
screenshot_options = final_render_options.copy()
|
||||
screenshot_options.pop("_keep_html_content", None)
|
||||
|
||||
image_bytes = await context.screenshot_engine.render(
|
||||
html=html_content, base_url_path=template_dir, **final_render_options
|
||||
html=html_content, base_url_path=template_dir, **screenshot_options
|
||||
)
|
||||
keep_html_content = bool(
|
||||
context.render_options.get("_keep_html_content", False)
|
||||
)
|
||||
debug_mode = Config.get_config("UI", "DEBUG_MODE", False)
|
||||
return RenderResult(
|
||||
image_bytes=image_bytes,
|
||||
html_content=html_content if debug_mode or keep_html_content else None,
|
||||
)
|
||||
return RenderResult(image_bytes=image_bytes, html_content=html_content)
|
||||
|
||||
Reference in New Issue
Block a user