[+] Add mime to json return

This commit is contained in:
Azalea Gui
2023-02-23 14:18:07 -05:00
parent 891647fbdb
commit 68dd001075
+20 -1
View File
@@ -42,7 +42,24 @@ async fn main() {
} }
} }
async fn hello_world(_req: Request<Body>) -> http::Result<Response<Body>> { #[derive(Serialize, Deserialize)]
pub struct ReturnPath {
name: String,
file_type: String,
mtime: i64,
mime: Option<String>
}
struct MyApp {
generator: Generator
}
impl MyApp {
fn new(base: &Path) -> Result<MyApp> {
Ok(MyApp { generator: Generator::new(base.into())? })
}
async fn hello_world(&self, _req: Request<Body>) -> http::Result<Response<Body>> {
let path = format!(".{}", clean(_req.uri().path())); let path = format!(".{}", clean(_req.uri().path()));
println!("Raw path: {} | Sanitized path: {path}", _req.uri().path()); println!("Raw path: {} | Sanitized path: {path}", _req.uri().path());
@@ -62,6 +79,7 @@ async fn hello_world(_req: Request<Body>) -> http::Result<Response<Body>> {
name: x.file_name().to_str()?.to_string(), name: x.file_name().to_str()?.to_string(),
file_type: x.path().file_type().to_string(), file_type: x.path().file_type().to_string(),
mtime: x.metadata().ok()?.mtime(), mtime: x.metadata().ok()?.mtime(),
mime: if x.path().is_file() { self.generator.get_mime(&x.path()).ok() } else { None }
})).collect(); })).collect();
match serde_json::to_string(&paths) { match serde_json::to_string(&paths) {
@@ -69,3 +87,4 @@ async fn hello_world(_req: Request<Body>) -> http::Result<Response<Body>> {
Err(e) => { e.to_string().resp(500) } Err(e) => { e.to_string().resp(500) }
} }
} }
}