feat: FaceFilterSettings 改用文件存储而非数据库
架构变更: - GET /api/v1/file-profile: 从 .profile.json 读取 face_filter - PUT /api/v1/file-profile: 写入 .profile.json 而非数据库 - 数据库不再存储 face_filter (仅存储路径/文件名) 优势: - 文件是 primary source of truth - 架构更简单、更清晰 - 便于跨设备同步 - 避免 DB 迁移复杂度 新增工具: - migrate_face_filter_to_files.py: 迁移现有数据库数据到文件 - 支持增量迁移(只迁移有变化的文件) - 支持 dry-run 测试 测试: - ✅ 编译通过 - ⏳ 功能测试待进行 对应设计文档: - .opencode/plans/persistence_design/01_overview.md - 三段式实体文件持久化架构
This commit is contained in:
+73
-81
@@ -994,6 +994,7 @@ pub async fn get_file_profile_handler(
|
||||
) -> Result<Json<FileProfile>, StatusCode> {
|
||||
let videos_table = schema::table_name("videos");
|
||||
|
||||
// Load basic file info from database
|
||||
let row: Option<(
|
||||
String,
|
||||
String,
|
||||
@@ -1004,10 +1005,9 @@ pub async fn get_file_profile_handler(
|
||||
i32,
|
||||
f64,
|
||||
i64,
|
||||
Option<serde_json::Value>,
|
||||
)> = sqlx::query_as(&format!(
|
||||
"SELECT file_uuid, file_name, file_path, status, duration, width, height, fps, \
|
||||
COALESCE(total_frames, 0), face_filter FROM {} WHERE file_uuid = $1",
|
||||
COALESCE(total_frames, 0) FROM {} WHERE file_uuid = $1",
|
||||
videos_table
|
||||
))
|
||||
.bind(¶ms.file_uuid)
|
||||
@@ -1028,18 +1028,19 @@ pub async fn get_file_profile_handler(
|
||||
height,
|
||||
fps,
|
||||
total_frames,
|
||||
face_filter,
|
||||
) = row.ok_or(StatusCode::NOT_FOUND)?;
|
||||
|
||||
let face_filter = face_filter
|
||||
.and_then(|v| serde_json::from_value::<FaceFilterSettings>(v).ok())
|
||||
.unwrap_or_else(FaceFilterSettings::defaults);
|
||||
|
||||
// Enrich with on-disk profile data (birth, history, key_frame)
|
||||
// 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
|
||||
let face_filter = disk_profile
|
||||
.as_ref()
|
||||
.and_then(|p| p.face_filter.clone())
|
||||
.unwrap_or_else(FaceFilterSettings::defaults);
|
||||
|
||||
// Fallback for zombie files: if DB fields are empty, try to recover from profile
|
||||
let effective_name = if file_name.is_empty() {
|
||||
disk_profile
|
||||
@@ -1091,9 +1092,9 @@ pub async fn update_file_profile_handler(
|
||||
) -> Result<Json<serde_json::Value>, StatusCode> {
|
||||
let videos_table = schema::table_name("videos");
|
||||
|
||||
// Update database fields (path, name) if provided
|
||||
let mut updates: Vec<String> = Vec::new();
|
||||
let mut binds: Vec<String> = Vec::new();
|
||||
let mut face_filter_bind: Option<String> = None;
|
||||
|
||||
if let Some(s) = req.file_path.as_ref().filter(|s| !s.is_empty()) {
|
||||
updates.push(format!("file_path = ${}", updates.len() + 1));
|
||||
@@ -1103,83 +1104,74 @@ pub async fn update_file_profile_handler(
|
||||
updates.push(format!("file_name = ${}", updates.len() + 1));
|
||||
binds.push(s.clone());
|
||||
}
|
||||
if let Some(ff) = req.face_filter.as_ref() {
|
||||
updates.push(format!("face_filter = ${}::jsonb", updates.len() + 1));
|
||||
match serde_json::to_value(ff) {
|
||||
Ok(v) => face_filter_bind = Some(v.to_string()),
|
||||
Err(e) => {
|
||||
tracing::error!("[FileProfile] Invalid face_filter: {}", e);
|
||||
return Err(StatusCode::BAD_REQUEST);
|
||||
}
|
||||
|
||||
// Update database if path or name changed
|
||||
if !updates.is_empty() {
|
||||
let file_uuid_idx = updates.len() + 1;
|
||||
updates.push("updated_at = CURRENT_TIMESTAMP".to_string());
|
||||
|
||||
let sql = format!(
|
||||
"UPDATE {} SET {} WHERE file_uuid = ${}",
|
||||
videos_table,
|
||||
updates.join(", "),
|
||||
file_uuid_idx
|
||||
);
|
||||
|
||||
let mut query = sqlx::query(&sql);
|
||||
for b in &binds {
|
||||
query = query.bind(b);
|
||||
}
|
||||
}
|
||||
query = query.bind(&req.file_uuid);
|
||||
|
||||
if updates.is_empty() {
|
||||
return Ok(Json(serde_json::json!({
|
||||
"success": true,
|
||||
"message": "No changes"
|
||||
})));
|
||||
}
|
||||
|
||||
let file_uuid_idx = updates.len() + 1;
|
||||
updates.push("updated_at = CURRENT_TIMESTAMP".to_string());
|
||||
|
||||
let sql = format!(
|
||||
"UPDATE {} SET {} WHERE file_uuid = ${}",
|
||||
videos_table,
|
||||
updates.join(", "),
|
||||
file_uuid_idx
|
||||
);
|
||||
|
||||
let mut query = sqlx::query(&sql);
|
||||
for b in &binds {
|
||||
query = query.bind(b);
|
||||
}
|
||||
if let Some(fb) = &face_filter_bind {
|
||||
query = query.bind(fb);
|
||||
}
|
||||
query = query.bind(&req.file_uuid);
|
||||
|
||||
let result = query.execute(state.db.pool()).await;
|
||||
|
||||
match result {
|
||||
Ok(res) if res.rows_affected() > 0 => {
|
||||
// Update on-disk profile if path/name/face_filter changed
|
||||
if req.file_path.is_some() || req.file_name.is_some() || req.face_filter.is_some() {
|
||||
let output_dir = crate::core::config::OUTPUT_DIR.as_str();
|
||||
if let Some(mut profile) = crate::core::file_profile::FileProfile::load_from_disk(
|
||||
output_dir,
|
||||
&req.file_uuid,
|
||||
) {
|
||||
let new_path = req
|
||||
.file_path
|
||||
.clone()
|
||||
.unwrap_or_else(|| profile.current.path.clone());
|
||||
let new_name = req
|
||||
.file_name
|
||||
.clone()
|
||||
.unwrap_or_else(|| profile.current.file_name.clone());
|
||||
profile.update_current_path(&new_path, &new_name);
|
||||
if let Some(ref ff) = req.face_filter {
|
||||
profile.face_filter = Some(ff.clone());
|
||||
}
|
||||
if let Err(e) = profile.save_to_disk(output_dir) {
|
||||
tracing::warn!("[FileProfile] Failed to update on-disk profile: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Json(serde_json::json!({
|
||||
"success": true,
|
||||
"message": "File profile updated",
|
||||
"file_uuid": req.file_uuid
|
||||
})))
|
||||
}
|
||||
Ok(_) => Err(StatusCode::NOT_FOUND),
|
||||
Err(e) => {
|
||||
let result = query.execute(state.db.pool()).await;
|
||||
if let Err(e) = result {
|
||||
tracing::error!("[FileProfile] Update failed: {}", e);
|
||||
Err(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
return Err(StatusCode::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
// Update .profile.json (primary source for face_filter)
|
||||
let output_dir = crate::core::config::OUTPUT_DIR.as_str();
|
||||
let mut profile = crate::core::file_profile::FileProfile::load_from_disk(output_dir, &req.file_uuid)
|
||||
.unwrap_or_else(|| {
|
||||
// Create new profile if doesn't exist
|
||||
crate::core::file_profile::FileProfile::from_db_record(
|
||||
&req.file_uuid,
|
||||
req.file_name.as_deref().unwrap_or(""),
|
||||
req.file_path.as_deref().unwrap_or(""),
|
||||
"video",
|
||||
"",
|
||||
0.0,
|
||||
0,
|
||||
0,
|
||||
0.0,
|
||||
0,
|
||||
)
|
||||
});
|
||||
|
||||
// Update path/name if provided
|
||||
if let Some(ref new_path) = req.file_path {
|
||||
if let Some(ref new_name) = req.file_name {
|
||||
profile.update_current_path(new_path, new_name);
|
||||
}
|
||||
}
|
||||
|
||||
// Update face_filter (primary source)
|
||||
if let Some(ref ff) = req.face_filter {
|
||||
profile.face_filter = Some(ff.clone());
|
||||
}
|
||||
|
||||
// Save to disk
|
||||
if let Err(e) = profile.save_to_disk(output_dir) {
|
||||
tracing::error!("[FileProfile] Failed to save to disk: {}", e);
|
||||
return Err(StatusCode::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
Ok(Json(serde_json::json!({
|
||||
"success": true,
|
||||
"message": "File profile updated",
|
||||
"file_uuid": req.file_uuid
|
||||
})))
|
||||
}
|
||||
|
||||
// ─── Face Groups ───
|
||||
|
||||
Reference in New Issue
Block a user