Files
ViGent2/Docs/DevLogs/Day20.md
Kevin Wong ee342cc40f 更新
2026-02-08 16:23:39 +08:00

104 lines
5.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## 🔧 代码质量与安全优化 (13:30)
### 概述
本日进行项目全面代码审查与优化,共处理 27 项优化点,完成 18 项核心修复。
### 已完成优化
#### 功能性修复
- [x] **P0-1**: LatentSync 回退逻辑空实现 → 改为 `raise RuntimeError`
- [x] **P1-1**: 任务状态接口无用户归属校验 → 添加用户认证依赖
- [x] **P1-2**: 前端 User 类型定义重复 → 统一到 `shared/types/user.ts`
#### 性能优化
- [x] **P1-3**: 参考音频列表 N+1 查询 → 使用 `asyncio.gather` 并发
- [x] **P1-4**: 视频上传整读内存 → 新增 `upload_file_from_path` 流式处理
- [x] **P1-5**: async 路由内同步阻塞 → `httpx.AsyncClient` 替换 `requests`
- [x] **P2-2**: GLM 服务同步调用 → `asyncio.to_thread` 包装
- [x] **P2-3**: Remotion 渲染启动慢 → 预编译 JS + `build:render` 脚本
#### 安全修复
- [x] **P1-8**: 硬编码 Cookie → 移至环境变量 `DOUYIN_COOKIE`
- [x] **P1-9**: 请求日志打印完整 headers → 敏感信息脱敏
- [x] **P2-10**: ffprobe 使用 `shell=True` → 改为参数列表
- [x] **P2-11**: CORS 配置 `*` + credentials → 从 `CORS_ORIGINS` 环境变量读取
#### 配置优化
- [x] **P2-5**: 存储服务硬编码路径 → 环境变量 `SUPABASE_STORAGE_LOCAL_PATH`
- [x] **P3-3**: Remotion `execSync` 同步调用 → promisified `exec` 异步
- [x] **P3-5**: LatentSync 相对路径 → 基于 `__file__` 绝对路径
### 暂不处理(收益有限)
- [~] **P1-6**: useHomeController 超大文件 (884行)
- [~] **P1-7**: 抖音/微信上传器重复代码(流程差异大)
### 低优先级(后续处理)
- [~] **P2-6~P2-9**: API 转发壳、前端 API 客户端混用、ESLint、重复逻辑
- [~] **P3-1~P3-4**: 阻塞式交互、Modal 过大、样式兼容层
### 涉及文件
- `backend/app/services/latentsync_service.py` - 回退逻辑
- `backend/app/modules/videos/router.py` - 任务状态认证
- `backend/app/modules/tools/router.py` - httpx 异步、Cookie 配置化
- `backend/app/services/glm_service.py` - 异步包装
- `backend/app/services/storage.py` - 流式上传、路径配置化
- `backend/app/services/video_service.py` - ffprobe 安全调用
- `backend/app/main.py` - CORS 配置、日志脱敏
- `backend/app/core/config.py` - 新增配置项
- `remotion/render.ts` - 异步 exec
- `remotion/package.json` - build:render 脚本
- `models/LatentSync/scripts/server.py` - 绝对路径
- `frontend/src/shared/types/user.ts` - 统一类型定义
### 新增环境变量
```bash
# .env 新增配置(均有默认值,无需必填)
CORS_ORIGINS=* # CORS 白名单
SUPABASE_STORAGE_LOCAL_PATH=/path/to/... # 本地存储路径
DOUYIN_COOKIE=... # 抖音视频下载 Cookie
```
### 重启要求
```bash
pm2 restart vigent2-backend
pm2 restart vigent2-latentsync
# Remotion 已自动编译
```
### 🎨 交互与体验优化 (17:00)
- [x] **UX-1**: PublishPage 图片加载优化 (`<img>``next/image`)
- [x] **UX-2**: 按钮 Loading 状态统一 (提取脚本弹窗 + 发布页)
- [x] **UX-3**: 骨架屏加载优化 (发布页加载中状态)
- [x] **UX-4**: 全局快捷键支持 (ESC 关闭弹窗, Enter 确认)
- [x] **UX-5**: 移除全局 GlobalTaskIndicator (视觉降噪)
- [x] **UX-6**: 视频生成完成自动刷新列表并选中最新
### 🐛 缺陷修复与回归治理 (17:30)
#### 严重缺陷修复
- [x] **BUG-1**: Remotion 渲染脚本路径解析错误 (导致标题字幕丢失)
- *原因*: `render.js` 预编译后使用了 `__dirname`,在 `dist` 目录下寻找源码失败。
- *修复*: 修改 `render.ts` 使用 `process.cwd()` 动态解析路径,并重新编译。
- [x] **BUG-2**: 发布页视频选择持久化失效 (Auth 异步竞态)
- *原因*: 页面加载时 `useAuth` 尚未返回用户 ID导致使用 `guest` Key 读取不到记录,随后被默认值覆盖。
- *修复*: 引入 `isVideoRestored` 状态机,强制等待 Auth 完成且 Video 列表加载完毕后,才执行恢复逻辑。
#### 回归问题治理
- [x] **REG-1**: 首页历史作品 ID 恢复后内容不显示
- *原因*: 持久化模块恢复了 ID`useGeneratedVideos` 未监听 ID 变化同步 URL。
- *修复*: 新增 `useEffect` 监听 `selectedVideoId` 变化并同步 `generatedVideo` URL。
- [x] **REG-2**: 首页/发布页“默认选中第一个”逻辑丢失
- *原因*: 重构移除旧逻辑后,新用户或无缓存用户进入页面无默认选中。
- *修复*: 在 `isRestored` 且无选中时,增加兜底逻辑自动选中列表第一项。
- [x] **REG-3**: 素材选择持久化失效 (闭包陷阱)
- *原因*: `useMaterials` 加载回调中捕获了旧的 `selectedMaterial` 状态,覆盖了已恢复的值。
- *修复*: 改为函数式状态更新 (`setState(prev => ...)`),确保基于最新状态判断。
- [x] **REF-1**: 持久化逻辑全站收敛与排查
- *优化*: 清理 `useBgm`, `useGeneratedVideos`, `useTitleSubtitleStyles` 中的冗余 `localStorage` 读取,统一由 `useHomePersistence` 管理。
- *排查*: 深度排查 `useRefAudios`, `useTitleSubtitleStyles` 等模块,确认逻辑健壮,无类似回归风险。