fix: thumbnail cache headers - no-cache + ETag for browser revalidation
- 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)
This commit is contained in:
+25
-27
@@ -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<u8>) -> 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>) -> (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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user