feat: FaceFilterSettings 透明文件管理(自动迁移)

核心特性:
- 读取优先级: 文件 > 数据库 > 默认值
- 自动迁移: 数据库数据自动迁移到文件
- 向后兼容: 旧数据透明处理
- 用户无感知: 不需要手动迁移脚本

工作原理:
1. GET /file-profile
   - 优先从文件读取
   - 文件不存在时从数据库读取
   - 自动创建文件(透明迁移)

2. PUT /file-profile
   - 直接写入文件
   - 数据库不再存储新数据

优势:
- ✅ 零配置迁移(第一次访问自动迁移)
- ✅ 架构简化(文件即 primary source)
- ✅ 跨设备同步(只需同步文件)
- ✅ 调试方便(直接编辑 JSON)

删除:
- ❌ migrate_face_filter_to_files.py(不需要)

文档更新:
- docs_v1.0/DESIGN/FACE_FILTER_FILE_STORAGE.md
- 说明透明管理机制
- 添加自动迁移流程图

Git: 3658c288 (enhanced)
This commit is contained in:
Accusys
2026-10-09 16:38:20 +08:00
parent 3658c2888e
commit 1379cf59cf
2 changed files with 324 additions and 3 deletions
+31 -3
View File
@@ -1005,9 +1005,10 @@ pub async fn get_file_profile_handler(
i32,
f64,
i64,
Option<serde_json::Value>, // face_filter (for backward compatibility)
)> = sqlx::query_as(&format!(
"SELECT file_uuid, file_name, file_path, status, duration, width, height, fps, \
COALESCE(total_frames, 0) FROM {} WHERE file_uuid = $1",
COALESCE(total_frames, 0), face_filter FROM {} WHERE file_uuid = $1",
videos_table
))
.bind(&params.file_uuid)
@@ -1028,19 +1029,46 @@ pub async fn get_file_profile_handler(
height,
fps,
total_frames,
db_face_filter,
) = row.ok_or(StatusCode::NOT_FOUND)?;
// Load face_filter from .profile.json (primary source)
// Try to load face_filter from .profile.json (primary source)
let output_dir = crate::core::config::OUTPUT_DIR.as_str();
let disk_profile =
crate::core::file_profile::FileProfile::load_from_disk(output_dir, &file_uuid);
// Get face_filter from file, not database
// Priority: File > Database > Default
let face_filter = disk_profile
.as_ref()
.and_then(|p| p.face_filter.clone())
.or_else(|| {
// Fallback to database (backward compatibility)
db_face_filter
.and_then(|v| serde_json::from_value::<FaceFilterSettings>(v).ok())
})
.unwrap_or_else(FaceFilterSettings::defaults);
// Auto-migration: If face_filter exists in DB but not in file, write to file
if disk_profile.is_none() && db_face_filter.is_some() {
tracing::info!("[FileProfile] Auto-migrating face_filter to file for {}", file_uuid);
let mut new_profile = crate::core::file_profile::FileProfile::from_db_record(
&file_uuid,
&file_name,
&file_path,
"video",
"",
duration,
width,
height,
fps,
total_frames,
);
new_profile.face_filter = Some(face_filter.clone());
if let Err(e) = new_profile.save_to_disk(output_dir) {
tracing::warn!("[FileProfile] Auto-migration failed: {}", e);
}
}
// Fallback for zombie files: if DB fields are empty, try to recover from profile
let effective_name = if file_name.is_empty() {
disk_profile