refactor: apply formatting to image.rs

This commit is contained in:
xfy 2026-06-08 15:13:07 +08:00
parent d584ef6e00
commit 8b1b949bf8

View File

@ -1,16 +1,15 @@
#[cfg(feature = "server")] #[cfg(feature = "server")]
use axum::{ use axum::{
extract::{Path, Query}, extract::{Path, Query},
http::{HeaderValue, StatusCode, header}, http::{header, HeaderValue, StatusCode},
response::{IntoResponse, Response}, response::{IntoResponse, Response},
}; };
#[cfg(feature = "server")] #[cfg(feature = "server")]
use moka::future::Cache;
#[cfg(feature = "server")]
use serde::Deserialize; use serde::Deserialize;
#[cfg(feature = "server")] #[cfg(feature = "server")]
use std::sync::LazyLock; use std::sync::LazyLock;
#[cfg(feature = "server")]
use moka::future::Cache;
#[cfg(feature = "server")] #[cfg(feature = "server")]
const MAX_IMAGE_DIMENSION: u32 = 4096; const MAX_IMAGE_DIMENSION: u32 = 4096;
@ -225,10 +224,7 @@ fn is_path_safe(path: &str) -> bool {
} }
#[cfg(feature = "server")] #[cfg(feature = "server")]
pub async fn serve_image( pub async fn serve_image(Path(path): Path<String>, Query(params): Query<ImageParams>) -> Response {
Path(path): Path<String>,
Query(params): Query<ImageParams>,
) -> Response {
// Path traversal protection // Path traversal protection
if !is_path_safe(&path) { if !is_path_safe(&path) {
return StatusCode::FORBIDDEN.into_response(); return StatusCode::FORBIDDEN.into_response();
@ -255,7 +251,11 @@ pub async fn serve_image(
// Check cache // Check cache
let cache_key = params.cache_key(&path); let cache_key = params.cache_key(&path);
if let Some(cached) = IMAGE_CACHE.get(&cache_key).await { if let Some(cached) = IMAGE_CACHE.get(&cache_key).await {
return (StatusCode::OK, [(header::CONTENT_TYPE, cached.content_type)], cached.data) return (
StatusCode::OK,
[(header::CONTENT_TYPE, cached.content_type)],
cached.data,
)
.into_response(); .into_response();
} }
@ -289,7 +289,10 @@ pub async fn serve_image(
}; };
let _ = IMAGE_CACHE.insert(cache_key, cached).await; let _ = IMAGE_CACHE.insert(cache_key, cached).await;
(StatusCode::OK, [(header::CONTENT_TYPE, content_type)], processed).into_response() (
StatusCode::OK,
[(header::CONTENT_TYPE, content_type)],
processed,
)
.into_response()
} }