feat: Identity JSON sync mechanism

- storage.rs: add local_profile field, check disk for profile.jpg in save_identity_file_by_pool
- tmdb_api.rs: trigger JSON sync after TMDb probe
- identity_api.rs: upload_profile_image triggers JSON sync
- identity_binding.rs: bind/unbind/merge trigger JSON sync
- get_identity_json: replace DB fallback with Lazy Sync (generates JSON from DB if missing)
- Fixes missing/obsolete JSON files for all identity mutations
This commit is contained in:
Accusys
2026-05-19 22:20:19 +08:00
parent 7680c202ef
commit 0eb08acaae
4 changed files with 129 additions and 54 deletions

View File

@@ -766,6 +766,11 @@ async fn upload_profile_image(
(StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"success": false, "message": format!("Failed to write file: {}", e)})))
})?;
// Sync identity JSON to reflect new profile image
let pool = state.db.pool().clone();
let uuid_clone = uuid_clean.clone();
let _ = crate::core::identity::storage::save_identity_file_by_pool(&pool, &uuid_clone).await;
Ok(Json(ProfileImageResponse {
success: true,
identity_uuid: uuid_clean,
@@ -815,45 +820,24 @@ async fn get_identity_json(
}
}
// 2. Fallback: Generate JSON from DB
use crate::core::identity::storage::{IdentityFile, FileBinding};
let record = state.db.get_identity_by_uuid(&clean).await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.ok_or(StatusCode::NOT_FOUND)?;
// 2. Lazy Sync: If file missing, generate from DB and save
if let Err(e) = crate::core::identity::storage::save_identity_file_by_pool(state.db.pool(), &clean).await {
tracing::warn!("[identity-json] Lazy sync failed for {}: {}", clean, e);
return Err(StatusCode::NOT_FOUND);
}
let id = record.id as i64;
let bindings: Vec<FileBinding> = {
let fd_table = crate::core::db::schema::table_name("face_detections");
let rows = sqlx::query_as::<_, (String, Vec<i32>, i64)>(
&format!("SELECT fd.file_uuid, COALESCE(array_agg(DISTINCT fd.trace_id) FILTER (WHERE fd.trace_id IS NOT NULL), '{{}}'::int[]), COUNT(*)::bigint FROM {} fd WHERE fd.identity_id = $1 GROUP BY fd.file_uuid ORDER BY fd.file_uuid", fd_table)
).bind(id).fetch_all(state.db.pool()).await.unwrap_or_default();
rows.into_iter().map(|(fu, tids, cnt)| FileBinding {
file_uuid: fu, trace_ids: tids, face_count: cnt,
}).collect()
};
// 3. Read the newly generated file
let p = crate::core::identity::storage::identity_file_path(&clean);
if p.exists() {
let data = std::fs::read(&p).map_err(|_| StatusCode::NOT_FOUND)?;
return Ok((
StatusCode::OK,
[("content-type".to_string(), "application/json".to_string())],
data,
));
}
let fmt_time = |dt: Option<chrono::DateTime<chrono::Utc>>| -> String {
dt.map(|d| d.to_rfc3339()).unwrap_or_else(|| chrono::Utc::now().to_rfc3339())
};
let file = IdentityFile {
version: 1,
identity_uuid: record.uuid.clone(),
name: record.name.clone(),
identity_type: record.identity_type.clone(),
source: record.source.clone(),
status: record.status.clone(),
tmdb_id: record.tmdb_id,
tmdb_profile: record.tmdb_profile.clone(),
metadata: record.metadata.clone(),
file_bindings: bindings,
created_at: fmt_time(record.created_at),
updated_at: fmt_time(record.updated_at),
};
let json = serde_json::to_string_pretty(&file).unwrap_or_default();
let bytes = json.into_bytes();
Ok((StatusCode::OK, [("content-type".to_string(), "application/json".to_string())], bytes))
Err(StatusCode::NOT_FOUND)
}
// ── Experiment: Identity Text Search ──────────────────────────