Compare commits
2 Commits
0137313c6e
...
1f99eec5a3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1f99eec5a3 | ||
|
|
448efeff1e |
@@ -6,12 +6,24 @@ use ntex::{
|
||||
|
||||
use crate::util::requester::Requester;
|
||||
|
||||
fn normalize_image_url(endpoint: &str) -> String {
|
||||
let endpoint = endpoint.trim_start_matches('/');
|
||||
println!("Normalizing image URL: {endpoint}");
|
||||
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://{endpoint}")
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_image(
|
||||
req: HttpRequest,
|
||||
requester: web::types::State<Requester>,
|
||||
) -> Result<impl web::Responder, web::Error> {
|
||||
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
|
||||
.get_ref()
|
||||
@@ -52,3 +64,24 @@ pub async fn get_image(
|
||||
// ...or simple & compatible:
|
||||
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"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user