Files

138 lines
6.3 KiB
Python
Raw Permalink Normal View History

2026-09-08 14:25:32 +08:00
"""统一配置注册:把插件配置按文档标准接入 hexi.web_hub.web_config(Web 可读可改)。
- temp 清理配置(env pydantic) → register_model_config
- 群文件投递配置(打包/解压密码, env pydantic) → register_model_config
- S3 存储配置(原硬编码在 services/storage/s3.py) → register_config_items(store=s3 模块)
群策略(data/list.json v3)不在这里注册:它有独立的 Web 页面与 API
(见 web_hub.py),不再走配置抽屉里的裸 JSON 编辑。
"""
from __future__ import annotations
from nonebot import get_plugin_config
from pydantic import BaseModel
2026-09-08 14:25:32 +08:00
from hexi.web_hub.config_standard import register_config_items, register_model_config
from . import cleanup # noqa: F401
from .services.storage import s3 as _s3mod
# 插件模块名 = plugin_id(与 NoneBot 模块名一致)
_PLUGIN_ID = __package__
class GroupFileConfig(BaseModel):
# 上传群文件时打包成一个 zip(关掉则逐个上传原文件)
video_analysis_group_file_zip: bool = True
# 压缩包解压密码(留空 = 不加密; 设置后要求 pyzipper 可用, 否则放弃群文件上传)
video_analysis_group_file_password: str = ""
group_file_config = get_plugin_config(GroupFileConfig)
def group_file_settings() -> tuple[bool, str]:
"""群文件投递设置:(是否打包成 zip, 解压密码)。
运行期读取实例属性,所以 Web 保存后立即生效(apply 是 setattr)。
"""
return (
bool(group_file_config.video_analysis_group_file_zip),
str(group_file_config.video_analysis_group_file_password or ""),
)
def _reset_s3_caches(_values=None, store=None):
"""保存 S3 配置后清空懒加载客户端缓存,让下次上传用新配置重建。"""
if store is None:
return
for name in ("_local_s3_plana", "_local_s3_planb", "_local_s3_planc", "_public_s3"):
try:
setattr(store, name, None)
except Exception: # noqa: BLE001
pass
def register_config() -> None:
"""注册本插件全部配置(供 __init__.py 在导入期调用)。"""
# 1) temp 清理配置
register_model_config(
_PLUGIN_ID,
cleanup.cleanup_config,
fields=[
"video_analysis_temp_cleanup_mode",
"video_analysis_temp_max_age_days",
"video_analysis_temp_cleanup_hour",
],
labels={
"video_analysis_temp_cleanup_mode": "temp 清理模式",
"video_analysis_temp_max_age_days": "清理过期天数",
"video_analysis_temp_cleanup_hour": "每日清理时间(时)",
},
descriptions={
"video_analysis_temp_cleanup_mode": "auto=每日定时自动清理; manual=仅命令手动清理",
"video_analysis_temp_max_age_days": "超过该天数未修改的文件会被清理",
"video_analysis_temp_cleanup_hour": "自动模式下的每日清理小时(0-23)",
},
options={
"video_analysis_temp_cleanup_mode": [
{"value": "auto", "label": "自动(auto)"},
{"value": "manual", "label": "手动(manual)"},
]
},
types={"video_analysis_temp_cleanup_mode": "enum"},
# 保存后按新配置重设自动定时任务
apply_extra=lambda _values, _conf: cleanup.reload_cleanup_config(),
)
# 2) 群文件投递:打包 / 解压密码
register_model_config(
_PLUGIN_ID,
group_file_config,
fields=[
"video_analysis_group_file_zip",
"video_analysis_group_file_password",
],
labels={
"video_analysis_group_file_zip": "群文件打包成 zip",
"video_analysis_group_file_password": "压缩包解压密码",
},
descriptions={
"video_analysis_group_file_zip": "开启后群文件收到的是一个压缩包;关闭则逐个上传原文件",
"video_analysis_group_file_password": "留空 = 不加密;设置后用 AES-256 加密(需要 7-Zip/WinRAR 等工具解压),缺 pyzipper 时会放弃群文件上传而不是传明文",
},
types={
"video_analysis_group_file_zip": "bool",
"video_analysis_group_file_password": "password",
},
)
# 3) S3 存储配置(来源无关)
register_config_items(
_PLUGIN_ID,
[
{"key": "LOCAL_S3_PLANA_ENDPOINT", "label": "PLANA 端点", "type": "string", "secret": True},
{"key": "LOCAL_S3_PLANA_ACCESS_KEY", "label": "PLANA AccessKey", "type": "password", "secret": True},
{"key": "LOCAL_S3_PLANA_SECRET_KEY", "label": "PLANA SecretKey", "type": "password", "secret": True},
{"key": "LOCAL_S3_PLANA_BUCKET", "label": "PLANA 桶", "type": "string"},
{"key": "LOCAL_S3_PLANB_ENDPOINT", "label": "PLANB 端点", "type": "string", "secret": True},
{"key": "LOCAL_S3_PLANB_ACCESS_KEY", "label": "PLANB AccessKey", "type": "password", "secret": True},
{"key": "LOCAL_S3_PLANB_SECRET_KEY", "label": "PLANB SecretKey", "type": "password", "secret": True},
{"key": "LOCAL_S3_PLANB_BUCKET", "label": "PLANB 桶", "type": "string"},
{"key": "LOCAL_S3_PLANC_ENDPOINT", "label": "PLANC 端点", "type": "string", "secret": True},
{"key": "LOCAL_S3_PLANC_ACCESS_KEY", "label": "PLANC AccessKey", "type": "password", "secret": True},
{"key": "LOCAL_S3_PLANC_SECRET_KEY", "label": "PLANC SecretKey", "type": "password", "secret": True},
{"key": "LOCAL_S3_PLANC_BUCKET", "label": "PLANC 桶", "type": "string"},
{"key": "PUBLIC_S3_ENDPOINT", "label": "公网端点", "type": "string"},
{"key": "PUBLIC_S3_ACCESS_KEY", "label": "公网 AccessKey", "type": "password", "secret": True},
{"key": "PUBLIC_S3_SECRET_KEY", "label": "公网 SecretKey", "type": "password", "secret": True},
{"key": "PUBLIC_S3_BUCKET", "label": "公网桶", "type": "string"},
{"key": "PUBLIC_S3_REGION", "label": "公网 Region", "type": "string"},
{"key": "PUBLIC_S3_SECURE", "label": "公网 HTTPS", "type": "bool", "default": True},
{"key": "PUBLIC_S3_DOMAIN", "label": "公网访问域名", "type": "string"},
],
store=_s3mod,
apply_extra=_reset_s3_caches,
)