From c184711bfcf22235e31f7c9c2099aaf5b4c89f96 Mon Sep 17 00:00:00 2001 From: Accusys Date: Sat, 8 Aug 2026 02:13:14 +0800 Subject: [PATCH] fix: thumbnail cache headers - no-cache + ETag for browser revalidation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Changed Cache-Control from 'public, max-age=86400' to 'no-cache, must-revalidate' - Added ETag header (sha256 hash of image bytes) - Browser will revalidate on each request - If file unchanged → 304 Not Modified (save bandwidth) - If file changed → 200 with new image - Applied to all thumbnail endpoints (trace, face, chunk, live extraction) --- src/api/media_api.rs | 52 +++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/src/api/media_api.rs b/src/api/media_api.rs index 5a1c4a51..a4665838 100644 --- a/src/api/media_api.rs +++ b/src/api/media_api.rs @@ -13,6 +13,19 @@ use uuid::Uuid; use crate::core::db::qdrant_db::QdrantDb; use crate::core::db::{schema, PostgresDb}; +use sha2::{Digest, Sha256}; + +/// Create an image response with proper cache headers (no-cache + ETag) +fn image_response(bytes: Vec) -> Response { + let etag = format!("\"{}\"", hex::encode(&Sha256::digest(&bytes)[..8])); + Response::builder() + .status(StatusCode::OK) + .header(header::CONTENT_TYPE, "image/jpeg") + .header(header::CACHE_CONTROL, "no-cache, must-revalidate") + .header(header::ETAG, etag) + .body(Body::from(bytes)) + .unwrap() +} /// Shared video query params: mode=normal|debug, audio=on|off fn parse_video_params(params: &std::collections::HashMap) -> (String, String) { @@ -929,10 +942,15 @@ async fn trace_thumbnail( StatusCode::INTERNAL_SERVER_ERROR })?; + // Generate ETag from file content (first 16 chars of sha256) + use sha2::{Digest, Sha256}; + let etag = format!("\"{}\"", hex::encode(&Sha256::digest(&bytes)[..8])); + Ok(Response::builder() .status(StatusCode::OK) .header(header::CONTENT_TYPE, "image/jpeg") - .header(header::CACHE_CONTROL, "no-cache") + .header(header::CACHE_CONTROL, "no-cache, must-revalidate") + .header(header::ETAG, etag) .body(Body::from(bytes)) .unwrap()) } @@ -983,12 +1001,7 @@ async fn face_thumbnail( 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()); + return Ok(image_response(bytes)); } // Fallback: check trace directory for key_face.jpg @@ -1012,12 +1025,7 @@ async fn face_thumbnail( 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()); + return Ok(image_response(bytes)); } // Cached file not found, fallback to ffmpeg @@ -1091,12 +1099,7 @@ async fn face_thumbnail( 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(output.stdout)) - .unwrap()) + Ok(image_response(output.stdout)) } async fn chunk_thumbnail( @@ -1117,12 +1120,7 @@ async fn chunk_thumbnail( tracing::warn!("[chunk_thumbnail] Failed to read cache: {}", 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()); + return Ok(image_response(bytes)); } let row: (f64, f64, f64) = sqlx::query_as(&format!( @@ -1192,7 +1190,7 @@ async fn chunk_thumbnail( Ok(Response::builder() .status(StatusCode::OK) .header(header::CONTENT_TYPE, "image/jpeg") - .header(header::CACHE_CONTROL, "public, max-age=86400") + .header(header::CACHE_CONTROL, "no-cache, must-revalidate") .body(Body::from(output.stdout)) .unwrap()) } @@ -1289,7 +1287,7 @@ async fn video_clip( Ok(Response::builder() .status(StatusCode::OK) .header(header::CONTENT_TYPE, "video/mp2t") - .header(header::CACHE_CONTROL, "public, max-age=86400") + .header(header::CACHE_CONTROL, "no-cache, must-revalidate") .body(Body::from(output.stdout)) .unwrap()) }