Improved image caching and CORS handling
This commit is contained in:
16
Cargo.lock
generated
16
Cargo.lock
generated
@@ -44,6 +44,21 @@ dependencies = [
|
|||||||
"tracing",
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "actix-cors"
|
||||||
|
version = "0.7.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "daa239b93927be1ff123eebada5a3ff23e89f0124ccb8609234e5103d5a5ae6d"
|
||||||
|
dependencies = [
|
||||||
|
"actix-utils",
|
||||||
|
"actix-web",
|
||||||
|
"derive_more 2.0.1",
|
||||||
|
"futures-util",
|
||||||
|
"log",
|
||||||
|
"once_cell",
|
||||||
|
"smallvec",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "actix-files"
|
name = "actix-files"
|
||||||
version = "0.6.7"
|
version = "0.6.7"
|
||||||
@@ -1537,6 +1552,7 @@ name = "image-api"
|
|||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix",
|
"actix",
|
||||||
|
"actix-cors",
|
||||||
"actix-files",
|
"actix-files",
|
||||||
"actix-multipart",
|
"actix-multipart",
|
||||||
"actix-rt",
|
"actix-rt",
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ actix-web = "4"
|
|||||||
actix-rt = "2.6"
|
actix-rt = "2.6"
|
||||||
tokio = { version = "1.42.0", features = ["default", "process", "sync"] }
|
tokio = { version = "1.42.0", features = ["default", "process", "sync"] }
|
||||||
actix-files = "0.6"
|
actix-files = "0.6"
|
||||||
|
actix-cors = "0.7"
|
||||||
actix-multipart = "0.7.2"
|
actix-multipart = "0.7.2"
|
||||||
futures = "0.3.5"
|
futures = "0.3.5"
|
||||||
jsonwebtoken = "9.3.0"
|
jsonwebtoken = "9.3.0"
|
||||||
|
|||||||
22
src/main.rs
22
src/main.rs
@@ -19,8 +19,8 @@ use std::{
|
|||||||
};
|
};
|
||||||
use walkdir::{DirEntry, WalkDir};
|
use walkdir::{DirEntry, WalkDir};
|
||||||
|
|
||||||
use actix_files::NamedFile;
|
|
||||||
use actix_cors::Cors;
|
use actix_cors::Cors;
|
||||||
|
use actix_files::NamedFile;
|
||||||
use actix_multipart as mp;
|
use actix_multipart as mp;
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
App, HttpRequest, HttpResponse, HttpServer, Responder, delete, get, middleware, post, put,
|
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) {
|
if let Ok(file) = NamedFile::open(&thumb_path) {
|
||||||
span.set_status(Status::Ok);
|
span.set_status(Status::Ok);
|
||||||
// The NamedFile will automatically set the correct content-type
|
// 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) {
|
if let Ok(file) = NamedFile::open(&path) {
|
||||||
span.set_status(Status::Ok);
|
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"));
|
span.set_status(Status::error("Not found"));
|
||||||
@@ -745,9 +755,9 @@ fn main() -> std::io::Result<()> {
|
|||||||
.allowed_origin_fn(|origin, _req_head| {
|
.allowed_origin_fn(|origin, _req_head| {
|
||||||
// Allow all origins in development, or check against CORS_ALLOWED_ORIGINS env var
|
// Allow all origins in development, or check against CORS_ALLOWED_ORIGINS env var
|
||||||
if let Ok(allowed_origins) = env::var("CORS_ALLOWED_ORIGINS") {
|
if let Ok(allowed_origins) = env::var("CORS_ALLOWED_ORIGINS") {
|
||||||
allowed_origins.split(',').any(|allowed| {
|
allowed_origins
|
||||||
origin.as_bytes() == allowed.trim().as_bytes()
|
.split(',')
|
||||||
})
|
.any(|allowed| origin.as_bytes() == allowed.trim().as_bytes())
|
||||||
} else {
|
} else {
|
||||||
// Default: allow all origins if not configured
|
// Default: allow all origins if not configured
|
||||||
true
|
true
|
||||||
|
|||||||
Reference in New Issue
Block a user