From 6e5892a2090c77232b5a1ccfb50c6ce0eceb0e08 Mon Sep 17 00:00:00 2001 From: neri Date: Wed, 24 May 2023 09:55:47 +0200 Subject: [PATCH] fix: file deletion when database can't be reached --- src/upload.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/upload.rs b/src/upload.rs index dcf086a..704aa3f 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -9,7 +9,7 @@ use actix_web::http::header::LOCATION; use actix_web::{error, web, Error, HttpRequest, HttpResponse}; use rand::{distributions::Slice, Rng}; use sqlx::postgres::PgPool; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use tokio::fs::{self, OpenOptions}; use tokio::sync::mpsc::Sender; @@ -46,7 +46,7 @@ pub async fn upload( ) }); - insert_file_metadata(&file_id, file_name, &upload_config, db).await?; + insert_file_metadata(&file_id, file_name, &file_path, &upload_config, db).await?; log::info!( "{} create new file {} (valid_till: {}, content_type: {}, delete_on_download: {})", @@ -69,6 +69,7 @@ pub async fn upload( async fn insert_file_metadata( file_id: &String, file_name: String, + file_path: &Path, upload_config: &UploadConfig, db: web::Data>, ) -> Result<(), Error> { @@ -85,7 +86,8 @@ async fn insert_file_metadata( .await; if let Err(db_err) = db_insert { log::error!("could not insert into datebase {:?}", db_err); - if let Err(file_err) = fs::remove_file(file_name).await { + + if let Err(file_err) = fs::remove_file(file_path).await { log::error!("could not remove file {:?}", file_err); } return Err(error::ErrorInternalServerError(