Skip to content

Commit

Permalink
Fix logics for removing size part from path
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallku committed Feb 9, 2024
1 parent 747611b commit e7755d5
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,20 @@ async fn handle_image_request(Path(path): Path<String>) -> impl IntoResponse {
return body.into_response();
}

let resize_width = path
.split('.')
.find(|s| s.starts_with("w"))
.and_then(|s| s.strip_prefix("w"))
.and_then(|s| s.parse::<u32>().ok());

let extension = file_path.extension().and_then(|s| s.to_str()).unwrap_or("");
let original_path = path.split('.').next().unwrap().to_string() + "." + extension;
let original_path = if resize_width.is_none() {
path.clone()
} else {
path.split('.').collect::<Vec<&str>>()[..path.split('.').count() - 2].join(".")
+ "."
+ extension
};
let original_file_path = PathBuf::from(format!("cdn_root/images/{}", original_path));

if !original_file_path.exists() {
Expand All @@ -67,12 +79,6 @@ async fn handle_image_request(Path(path): Path<String>) -> impl IntoResponse {
}
}

let resize_width = path
.split('.')
.find(|s| s.starts_with("w"))
.and_then(|s| s.strip_prefix("w"))
.and_then(|s| s.parse::<u32>().ok());

if resize_width.is_none() {
let file = match tokio::fs::File::open(file_path).await {
Ok(file) => file,
Expand Down

0 comments on commit e7755d5

Please sign in to comment.