hanime thumbnail fix

This commit is contained in:
Simon
2026-03-15 23:47:32 +00:00
parent 0137313c6e
commit 448efeff1e

View File

@@ -6,12 +6,24 @@ use ntex::{
use crate::util::requester::Requester; use crate::util::requester::Requester;
fn normalize_image_url(endpoint: &str) -> String {
let endpoint = endpoint.trim_start_matches('/');
if endpoint.starts_with("http://") || endpoint.starts_with("https://") {
endpoint.to_string()
} else if endpoint.starts_with("hanime-cdn.com/") || endpoint == "hanime-cdn.com" {
format!("https://{endpoint}")
} else {
format!("https://hanime-cdn.com/{endpoint}")
}
}
pub async fn get_image( pub async fn get_image(
req: HttpRequest, req: HttpRequest,
requester: web::types::State<Requester>, requester: web::types::State<Requester>,
) -> Result<impl web::Responder, web::Error> { ) -> Result<impl web::Responder, web::Error> {
let endpoint = req.match_info().query("endpoint").to_string(); let endpoint = req.match_info().query("endpoint").to_string();
let image_url = format!("https://hanime-cdn.com/{}", endpoint); let image_url = normalize_image_url(&endpoint);
let upstream = match requester let upstream = match requester
.get_ref() .get_ref()
@@ -52,3 +64,24 @@ pub async fn get_image(
// ...or simple & compatible: // ...or simple & compatible:
Ok(resp.body(bytes.to_vec())) Ok(resp.body(bytes.to_vec()))
} }
#[cfg(test)]
mod tests {
use super::normalize_image_url;
#[test]
fn keeps_full_hanime_cdn_host_path_without_duplication() {
assert_eq!(
normalize_image_url("hanime-cdn.com/images/covers/natsu-zuma-2-cv1.png"),
"https://hanime-cdn.com/images/covers/natsu-zuma-2-cv1.png"
);
}
#[test]
fn prefixes_relative_paths_with_hanime_cdn_host() {
assert_eq!(
normalize_image_url("/images/covers/natsu-zuma-2-cv1.png"),
"https://hanime-cdn.com/images/covers/natsu-zuma-2-cv1.png"
);
}
}