Added error handling for download functions to avoid race conditions

This commit is contained in:
Mutzi 2022-10-26 17:29:32 +02:00
parent aa2db48a68
commit c687058a06

View File

@ -461,14 +461,15 @@ pub fn download(span: &Span, req: &mut Request, db: &mut DBConnection) -> Result
AppError::BadRequest("Unknown node").err() AppError::BadRequest("Unknown node").err()
} else { } else {
let file_name = format!("./temp/{}", entry.temp_id); let file_name = format!("./temp/{}", entry.temp_id);
let resp = Response::from_file(File::open(std::path::Path::new(&file_name)).unwrap()) Ok(Response::from_file(
File::open(std::path::Path::new(&file_name)).map_err(|_| AppError::BadRequest("Unknown file"))?
)
.with_header(header("content-type", "application/zip")) .with_header(header("content-type", "application/zip"))
.with_header(header( .with_header(header(
"content-disposition", "content-disposition",
&("attachment; filename=".to_owned() + &node.name + ".zip") &("attachment; filename=".to_owned() + &node.name + ".zip")
)) ))
.boxed(); .boxed())
Ok(resp)
} }
} }
} }
@ -527,7 +528,9 @@ pub fn download_preview(
get_reply(&dto::responses::DownloadBase64 { get_reply(&dto::responses::DownloadBase64 {
statusCode: 200, statusCode: 200,
data: "data:image/png;base64,".to_owned() data: "data:image/png;base64,".to_owned()
+ &base64::encode(std::fs::read(std::path::Path::new(&file)).unwrap()) + &base64::encode(
std::fs::read(std::path::Path::new(&file)).map_err(|_| AppError::BadRequest("Unknown file"))?
)
}) })
} else { } else {
AppError::BadRequest("No preview").err() AppError::BadRequest("No preview").err()