add cause in errors, filesize to 50kb

This commit is contained in:
əlemi 2022-03-23 02:43:56 +01:00
parent 497879b839
commit 35960af854
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E

View file

@ -15,20 +15,22 @@ struct FileRequest {
}
#[derive(Display, Debug, Error)]
struct FileError {}
struct FileError {
cause: String,
}
impl actix_web::error::ResponseError for FileError {}
/// upload memo db
async fn put(req: web::Json<FileRequest>) -> Result<String, FileError> {
if req.payload.len() < 20480 {
let fname = req.file.chars().filter(|c| c.is_alphanumeric()).collect::<String>();
if req.payload.len() < 51200 {
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();
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 _res = web::block(move || f.write(base64::decode(&req.payload).unwrap().as_slice())).await.unwrap();
return Ok("OK".to_string());
} 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()),
}
}
return Err(FileError{});
return Err(FileError{cause:"not found".to_string()});
}
#[actix_web::main]