diff --git a/src/api/media_api.rs b/src/api/media_api.rs index a2b084af..2fa47754 100644 --- a/src/api/media_api.rs +++ b/src/api/media_api.rs @@ -52,6 +52,7 @@ fn ffmpeg_cmd() -> std::process::Command { pub fn bbox_routes() -> Router { Router::new() .route("/api/v1/face-thumbnail", get(face_thumbnail_compat)) + .route("/api/v1/trace-thumb/:file_uuid/:trace_id", get(trace_thumbnail)) .route( "/api/v1/file/:file_uuid/video/bbox", get(bbox_overlay_video), @@ -869,6 +870,40 @@ async fn face_thumbnail_compat( face_thumbnail(State(state), Path(file_uuid), Query(q)).await } +/// Direct access to trace key_face.jpg — bypasses ffmpeg, reads pre-cached file. +/// Used by Studio for fast face group thumbnail loading. +async fn trace_thumbnail( + State(_state): State, + Path((file_uuid, trace_id)): Path<(String, i32)>, +) -> Result { + let output_dir = crate::core::config::OUTPUT_DIR.as_str(); + let key_face_path = std::path::PathBuf::from(output_dir) + .join(&file_uuid) + .join(format!("trace_{}", trace_id)) + .join("key_face.jpg"); + + if !key_face_path.exists() { + return Err(StatusCode::NOT_FOUND); + } + + let bytes = tokio::fs::read(&key_face_path).await.map_err(|e| { + tracing::warn!("[trace-thumb] Failed to read {}: {}", key_face_path.display(), e); + StatusCode::INTERNAL_SERVER_ERROR + })?; + + crate::core::thumbnail::validator::validate_jpeg(&bytes).map_err(|e| { + tracing::warn!("[trace-thumb] JPEG validation failed: {}", e); + StatusCode::INTERNAL_SERVER_ERROR + })?; + + 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()) +} + async fn face_thumbnail( State(state): State, Path(file_uuid): Path,