-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move route related logics to routes dir
- Loading branch information
1 parent
47ce809
commit 8cf4b0c
Showing
5 changed files
with
117 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use axum::{routing::get, Router}; | ||
|
||
use crate::env::state::AppState; | ||
|
||
pub fn app() -> Router<AppState> { | ||
Router::new() | ||
.route("/files/*path", get(super::files::get)) | ||
.route("/images/*path", get(super::images::get)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use axum::{ | ||
extract::{Path, State}, | ||
response::IntoResponse, | ||
}; | ||
use reqwest::StatusCode; | ||
use std::path::PathBuf; | ||
use tracing::error; | ||
|
||
use crate::{ | ||
constants::CDN_ROOT, | ||
env::state::AppState, | ||
utils::{ | ||
fetch::fetch_and_cache, | ||
http::{response_error, response_file}, | ||
}, | ||
}; | ||
|
||
pub async fn get(State(state): State<AppState>, Path(path): Path<String>) -> impl IntoResponse { | ||
let file_path = PathBuf::from(format!("{}/files/{}", CDN_ROOT, path)); | ||
|
||
if file_path.exists() { | ||
error!("File exists but respond with Rust: {:?}", file_path); | ||
return response_file(&file_path).await; | ||
} | ||
|
||
if let Err(_) = fetch_and_cache(state.host, &file_path, &path).await { | ||
return response_error(StatusCode::NOT_FOUND); | ||
} | ||
|
||
response_file(&file_path).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use axum::{ | ||
extract::{Path, State}, | ||
response::IntoResponse, | ||
}; | ||
use reqwest::StatusCode; | ||
use std::path::PathBuf; | ||
use tracing::error; | ||
|
||
use crate::{ | ||
constants::CDN_ROOT, | ||
env::state::AppState, | ||
utils::{ | ||
fetch::fetch_and_cache, | ||
http::{response_error, response_file}, | ||
img::{save_image_to_webp, save_resized_image}, | ||
path::{get_original_path, get_resize_width_from_path}, | ||
}, | ||
}; | ||
|
||
pub async fn get(State(state): State<AppState>, Path(path): Path<String>) -> impl IntoResponse { | ||
let file_path = PathBuf::from(format!("{}/images/{}", CDN_ROOT, path)); | ||
|
||
if file_path.exists() { | ||
error!("File exists but respond with Rust: {:?}", file_path); | ||
return response_file(&file_path).await; | ||
} | ||
|
||
let resize_width = get_resize_width_from_path(&path); | ||
let convert_to_webp = path.ends_with(".webp"); | ||
let original_path = get_original_path(&path, resize_width.is_some()); | ||
let original_file_path = PathBuf::from(format!("{}/images/{}", CDN_ROOT, original_path)); | ||
|
||
if !original_file_path.exists() { | ||
if let Err(_) = fetch_and_cache(state.host, &original_file_path, &original_path).await { | ||
return response_error(StatusCode::NOT_FOUND); | ||
} | ||
} | ||
|
||
if resize_width.is_none() && !convert_to_webp { | ||
return response_file(&file_path).await; | ||
} | ||
|
||
let image = match image::open(&original_file_path) { | ||
Ok(image) => image, | ||
Err(_) => { | ||
return response_error(StatusCode::INTERNAL_SERVER_ERROR); | ||
} | ||
}; | ||
|
||
if !convert_to_webp { | ||
return save_resized_image(image, resize_width, &original_file_path, &file_path).await; | ||
} | ||
|
||
let path_with_webp = format!("{}.webp", original_path); | ||
let file_path_with_webp = PathBuf::from(format!("{}/images/{}", CDN_ROOT, path_with_webp)); | ||
|
||
if let Err(_) = save_image_to_webp(&image, &file_path_with_webp) { | ||
return response_error(StatusCode::INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
let image_webp = match image::open(&file_path_with_webp) { | ||
Ok(image) => image, | ||
Err(_) => { | ||
return response_error(StatusCode::INTERNAL_SERVER_ERROR); | ||
} | ||
}; | ||
|
||
save_resized_image(image_webp, resize_width, &file_path_with_webp, &file_path).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod app; | ||
mod files; | ||
mod images; |