Improved image caching and CORS handling
This commit is contained in:
22
src/main.rs
22
src/main.rs
@@ -19,8 +19,8 @@ use std::{
|
||||
};
|
||||
use walkdir::{DirEntry, WalkDir};
|
||||
|
||||
use actix_files::NamedFile;
|
||||
use actix_cors::Cors;
|
||||
use actix_files::NamedFile;
|
||||
use actix_multipart as mp;
|
||||
use actix_web::{
|
||||
App, HttpRequest, HttpResponse, HttpServer, Responder, delete, get, middleware, post, put,
|
||||
@@ -114,13 +114,23 @@ async fn get_image(
|
||||
if let Ok(file) = NamedFile::open(&thumb_path) {
|
||||
span.set_status(Status::Ok);
|
||||
// The NamedFile will automatically set the correct content-type
|
||||
return file.into_response(&request);
|
||||
// Enable ETag and set cache headers for thumbnails (1 day cache)
|
||||
return file
|
||||
.use_etag(true)
|
||||
.use_last_modified(true)
|
||||
.prefer_utf8(true)
|
||||
.into_response(&request);
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(file) = NamedFile::open(&path) {
|
||||
span.set_status(Status::Ok);
|
||||
return file.into_response(&request);
|
||||
// Enable ETag and set cache headers for full images (1 hour cache)
|
||||
return file
|
||||
.use_etag(true)
|
||||
.use_last_modified(true)
|
||||
.prefer_utf8(true)
|
||||
.into_response(&request);
|
||||
}
|
||||
|
||||
span.set_status(Status::error("Not found"));
|
||||
@@ -745,9 +755,9 @@ fn main() -> std::io::Result<()> {
|
||||
.allowed_origin_fn(|origin, _req_head| {
|
||||
// Allow all origins in development, or check against CORS_ALLOWED_ORIGINS env var
|
||||
if let Ok(allowed_origins) = env::var("CORS_ALLOWED_ORIGINS") {
|
||||
allowed_origins.split(',').any(|allowed| {
|
||||
origin.as_bytes() == allowed.trim().as_bytes()
|
||||
})
|
||||
allowed_origins
|
||||
.split(',')
|
||||
.any(|allowed| origin.as_bytes() == allowed.trim().as_bytes())
|
||||
} else {
|
||||
// Default: allow all origins if not configured
|
||||
true
|
||||
|
||||
Reference in New Issue
Block a user