diff --git a/src/api/media_api.rs b/src/api/media_api.rs index e2d52aa4..a2b084af 100644 --- a/src/api/media_api.rs +++ b/src/api/media_api.rs @@ -892,6 +892,8 @@ async fn face_thumbnail( // Step 1: Check for pre-stored face crop if trace_id is provided if let Some(trace_id) = q.trace_id { let output_dir = crate::core::config::OUTPUT_DIR.as_str(); + + // Check .faces cache first let cached_path = std::path::PathBuf::from(output_dir) .join(".faces") .join(&file_uuid) @@ -908,7 +910,6 @@ async fn face_thumbnail( StatusCode::INTERNAL_SERVER_ERROR })?; - // Validate cached JPEG crate::core::thumbnail::validator::validate_jpeg(&bytes).map_err(|e| { tracing::warn!("[thumbnail] Cached JPEG validation failed: {}", e); StatusCode::INTERNAL_SERVER_ERROR @@ -922,6 +923,35 @@ async fn face_thumbnail( .unwrap()); } + // Fallback: check trace directory for key_face.jpg + let trace_key_face = std::path::PathBuf::from(output_dir) + .join(&file_uuid) + .join(format!("trace_{}", trace_id)) + .join("key_face.jpg"); + + if trace_key_face.exists() { + tracing::debug!( + "[thumbnail] Using trace key_face.jpg: {}", + trace_key_face.display() + ); + let bytes = tokio::fs::read(&trace_key_face).await.map_err(|e| { + tracing::warn!("[thumbnail] Failed to read key_face.jpg: {}", e); + StatusCode::INTERNAL_SERVER_ERROR + })?; + + crate::core::thumbnail::validator::validate_jpeg(&bytes).map_err(|e| { + tracing::warn!("[thumbnail] key_face.jpg validation failed: {}", e); + StatusCode::INTERNAL_SERVER_ERROR + })?; + + return Ok(Response::builder() + .status(StatusCode::OK) + .header(header::CONTENT_TYPE, "image/jpeg") + .header(header::CACHE_CONTROL, "public, max-age=86400") + .body(Body::from(bytes)) + .unwrap()); + } + // Cached file not found, fallback to ffmpeg tracing::debug!("[thumbnail] Cached file not found, falling back to ffmpeg"); } diff --git a/src/api/profile.rs b/src/api/profile.rs index 2b83f712..184d42dc 100644 --- a/src/api/profile.rs +++ b/src/api/profile.rs @@ -1161,12 +1161,19 @@ pub async fn update_file_profile_handler( pub struct FaceGroup { pub group_id: i32, pub name: String, - pub trace_ids: Vec, + #[serde(skip_serializing_if = "Option::is_none")] + pub trace_ids: Option>, pub trace_count: i32, pub representative_trace: i32, pub editable: bool, } +#[derive(Deserialize)] +pub struct FaceGroupsQuery { + #[serde(default)] + pub include_trace_ids: Option, +} + #[derive(Debug, Serialize, Deserialize)] pub struct FaceGroupsResponse { pub success: bool, @@ -1180,6 +1187,7 @@ pub async fn get_face_groups_handler( State(state): State, Extension(_auth): Extension, Path(file_uuid): Path, + Query(q): Query, ) -> Result, StatusCode> { let tkg_table = schema::table_name("tkg_nodes"); let tp_table = schema::table_name("trace_profiles"); @@ -1213,7 +1221,21 @@ pub async fn get_face_groups_handler( } } - let face_groups: Vec = groups + // Filter out single-trace "groups" — these are unclustered traces + // Move them to unassigned instead of showing as separate groups + let mut filtered_groups: HashMap> = HashMap::new(); + for (name, trace_ids) in groups { + if trace_ids.len() > 1 { + filtered_groups.insert(name, trace_ids); + } else { + // Single-trace "group" → treat as unassigned + unassigned.extend(trace_ids); + } + } + + let include_trace_ids = q.include_trace_ids.unwrap_or(true); + + let face_groups: Vec = filtered_groups .into_iter() .enumerate() .map(|(idx, (name, trace_ids))| FaceGroup { @@ -1221,7 +1243,7 @@ pub async fn get_face_groups_handler( name, trace_count: trace_ids.len() as i32, representative_trace: trace_ids.first().copied().unwrap_or(0), - trace_ids, + trace_ids: if include_trace_ids { Some(trace_ids) } else { None }, editable: true, }) .collect(); @@ -1309,9 +1331,18 @@ pub async fn get_cluster_results_handler( } } + // Filter out single-trace "groups" — these are unclustered traces + let mut filtered_groups: HashMap> = HashMap::new(); + for (name, trace_ids) in groups { + if trace_ids.len() > 1 { + filtered_groups.insert(name, trace_ids); + } + // Single-trace groups are silently dropped (not real clusters) + } + // trace_id -> cluster_id mapping for frame faces let mut trace_to_cluster: HashMap = HashMap::new(); - let face_groups: Vec = groups + let face_groups: Vec = filtered_groups .into_iter() .enumerate() .map(|(idx, (name, trace_ids))| {