fix: file deletion when database can't be reached

This commit is contained in:
neri 2023-05-24 09:55:47 +02:00
parent 2ab33b82d0
commit 6e5892a209
1 changed files with 5 additions and 3 deletions

View File

@ -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<sqlx::Pool<sqlx::Postgres>>,
) -> 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(