mirror of
https://git.alemi.dev/memo-cli.git
synced 2024-11-10 04:09:22 +01:00
add cause in errors, filesize to 50kb
This commit is contained in:
parent
497879b839
commit
35960af854
1 changed files with 8 additions and 6 deletions
|
@ -15,20 +15,22 @@ struct FileRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Display, Debug, Error)]
|
#[derive(Display, Debug, Error)]
|
||||||
struct FileError {}
|
struct FileError {
|
||||||
|
cause: String,
|
||||||
|
}
|
||||||
impl actix_web::error::ResponseError for FileError {}
|
impl actix_web::error::ResponseError for FileError {}
|
||||||
|
|
||||||
/// upload memo db
|
/// upload memo db
|
||||||
async fn put(req: web::Json<FileRequest>) -> Result<String, FileError> {
|
async fn put(req: web::Json<FileRequest>) -> Result<String, FileError> {
|
||||||
if req.payload.len() < 20480 {
|
if req.payload.len() < 51200 {
|
||||||
let fname = req.file.chars().filter(|c| c.is_alphanumeric()).collect::<String>();
|
let fname = req.file.chars().filter(|c| c.is_alphanumeric()).collect::<String>(); // TODO base64 uses some symbols!
|
||||||
let f_res = web::block(||fs::File::create(fname)).await.unwrap();
|
let f_res = web::block(||fs::File::create(fname)).await.unwrap();
|
||||||
if f_res.is_err() { return Err(FileError{}); }
|
if f_res.is_err() { return Err(FileError{cause:"could not create file".to_string()}); }
|
||||||
let mut f = f_res.unwrap();
|
let mut f = f_res.unwrap();
|
||||||
let _res = web::block(move || f.write(base64::decode(&req.payload).unwrap().as_slice())).await.unwrap();
|
let _res = web::block(move || f.write(base64::decode(&req.payload).unwrap().as_slice())).await.unwrap();
|
||||||
return Ok("OK".to_string());
|
return Ok("OK".to_string());
|
||||||
} else {
|
} else {
|
||||||
return Err(FileError{});
|
return Err(FileError{cause:"size too big".to_string()});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +50,7 @@ async fn get(req: web::Json<FileRequest>) -> Result<NamedFile, FileError> {
|
||||||
Err(e) => println!("[!] could not list dirs : {}", e.to_string()),
|
Err(e) => println!("[!] could not list dirs : {}", e.to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Err(FileError{});
|
return Err(FileError{cause:"not found".to_string()});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
|
|
Loading…
Reference in a new issue