diff options
| -rw-r--r-- | src/config.rs | 2 | ||||
| -rw-r--r-- | src/files.rs | 19 | ||||
| -rw-r--r-- | src/main.rs | 6 |
3 files changed, 25 insertions, 2 deletions
diff --git a/src/config.rs b/src/config.rs index fd85d91..02ae63d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,4 +3,4 @@ pub static PAGE_LIMIT: i64 = 20; pub const MAX_UPLOAD_FILE_SIZE: u64 = 1024 * 1024; // 1 MB pub const SAVE_FILE_BASE_PATH: &str = "./uploads"; -pub const UPLOADS_ENDPOINT: &str = "http://localhost:3000/uploads"; +pub const UPLOADS_ENDPOINT: &str = "/uploads"; diff --git a/src/files.rs b/src/files.rs index bcda6ab..00d16b2 100644 --- a/src/files.rs +++ b/src/files.rs @@ -1,6 +1,10 @@ use crate::config::{SAVE_FILE_BASE_PATH, UPLOADS_ENDPOINT}; use crate::errors::AppError; -use axum::extract::Multipart; +use axum::{ + extract::{Multipart, Path}, + http::header::HeaderMap, +}; +use std::fs::read; use rand::random; @@ -45,3 +49,16 @@ pub async fn upload( "File extension not supported".to_string(), )) } + +/// Axum endpoint which shows uploaded file +pub async fn show_uploads(Path(id): Path<String>) -> (HeaderMap, Vec<u8>) { + // let index = id.find(".").map(|i| i).unwrap_or(usize::max_value()); + + // let mut ext_name = "xxx"; + // if index != usize::max_value() { + // ext_name = &id[index + 1..]; + // } + let headers = HeaderMap::new(); + let file_name = format!("{}/{}", SAVE_FILE_BASE_PATH, id); + (headers, read(&file_name).unwrap()) +} diff --git a/src/main.rs b/src/main.rs index f0d7661..fd58313 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,9 +7,11 @@ mod models; mod pagination; mod routes; +use crate::config::UPLOADS_ENDPOINT; use axum::{ handler::Handler, http::{header, Request}, + routing::get, Router, }; use std::time::Duration; @@ -43,6 +45,10 @@ async fn create_app() -> Router { .nest("/models", routes::model::create_route()); Router::new() + .route( + &format!("{}/:id", UPLOADS_ENDPOINT).to_owned(), + get(crate::files::show_uploads), + ) // Map all routes to `/v1/*` namespace .nest("/v1", api_routes) .fallback(crate::routes::page_404.into_service()) |
