[+] Add mime to json return
This commit is contained in:
+20
-1
@@ -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,10 +79,12 @@ 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) {
|
||||||
Ok(json) => { json.resp(200) }
|
Ok(json) => { json.resp(200) }
|
||||||
Err(e) => { e.to_string().resp(500) }
|
Err(e) => { e.to_string().resp(500) }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user