From c687058a06043c7bdc7244123906e4417e99ce34 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 26 Oct 2022 17:29:32 +0200 Subject: [PATCH] Added error handling for download functions to avoid race conditions --- backend/src/routes/fs/routes.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/backend/src/routes/fs/routes.rs b/backend/src/routes/fs/routes.rs index 88f837d..006feda 100644 --- a/backend/src/routes/fs/routes.rs +++ b/backend/src/routes/fs/routes.rs @@ -461,14 +461,15 @@ pub fn download(span: &Span, req: &mut Request, db: &mut DBConnection) -> Result AppError::BadRequest("Unknown node").err() } else { let file_name = format!("./temp/{}", entry.temp_id); - let resp = Response::from_file(File::open(std::path::Path::new(&file_name)).unwrap()) - .with_header(header("content-type", "application/zip")) - .with_header(header( - "content-disposition", - &("attachment; filename=".to_owned() + &node.name + ".zip") - )) - .boxed(); - Ok(resp) + 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-disposition", + &("attachment; filename=".to_owned() + &node.name + ".zip") + )) + .boxed()) } } } @@ -527,7 +528,9 @@ pub fn download_preview( get_reply(&dto::responses::DownloadBase64 { statusCode: 200, 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 { AppError::BadRequest("No preview").err()