🐛 fix(*): 修复代码审查中发现的问题

- **Bug 修复**
  - `message.py`: 批量发送时使用 `(index, msg)` 元组替代 `messages.index(msg)`,避免重复 dict 查找错误
  - `message.py`: 多张图片逐张发送,不再静默丢弃后续图片
  - `plugin.py`: API Key 日志只打印"已配置/自动生成",不再泄露密钥

- **潜在问题修复**
  - `message.py`: lambda 闭包添加默认参数绑定,防止循环变量捕获问题
  - `upload.py`: 文件超限后消费剩余 multipart 数据,避免 reader 状态异常
  - `config.py`: PORT 环境变量非法值容错,默认回退 8081
  - `plugin.py`: cleanup task 保存引用,on_close 时正确取消,避免热重载泄漏

- **代码风格**
  - `message.py`: 无插值 f-string 改为普通字符串
  - `upload.py`: read_chunk 硬编码提取为 CHUNK_SIZE 常量
This commit is contained in:
2026-05-02 15:28:54 +08:00
parent 67e328942c
commit b86a2d4c4e
4 changed files with 214 additions and 160 deletions

View File

@@ -14,6 +14,8 @@ logger = logging.getLogger("webhook-plugin.upload")
# 文件最大保留秒数(默认 24 小时)
FILE_TTL_SECONDS: int = 24 * 60 * 60
# 读取块大小
CHUNK_SIZE: int = 65536
def _check_extension(filename: str) -> bool:
@@ -58,11 +60,14 @@ async def upload_handler(request: web.Request) -> web.Response:
chunks: list[bytes] = []
total_size = 0
while True:
chunk = await part.read_chunk(65536)
chunk = await part.read_chunk(CHUNK_SIZE)
if not chunk:
break
total_size += len(chunk)
if total_size > MAX_UPLOAD_SIZE:
# 消费剩余数据,避免 multipart reader 状态异常
while await part.read_chunk(CHUNK_SIZE):
pass
return error(
f"file too large, max {MAX_UPLOAD_SIZE // (1024*1024)} MB",
code=413,