fix: file deletion when database can't be reached
This commit is contained in:
parent
2ab33b82d0
commit
6e5892a209
|
@ -9,7 +9,7 @@ use actix_web::http::header::LOCATION;
|
||||||
use actix_web::{error, web, Error, HttpRequest, HttpResponse};
|
use actix_web::{error, web, Error, HttpRequest, HttpResponse};
|
||||||
use rand::{distributions::Slice, Rng};
|
use rand::{distributions::Slice, Rng};
|
||||||
use sqlx::postgres::PgPool;
|
use sqlx::postgres::PgPool;
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
use tokio::fs::{self, OpenOptions};
|
use tokio::fs::{self, OpenOptions};
|
||||||
use tokio::sync::mpsc::Sender;
|
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!(
|
log::info!(
|
||||||
"{} create new file {} (valid_till: {}, content_type: {}, delete_on_download: {})",
|
"{} create new file {} (valid_till: {}, content_type: {}, delete_on_download: {})",
|
||||||
|
@ -69,6 +69,7 @@ pub async fn upload(
|
||||||
async fn insert_file_metadata(
|
async fn insert_file_metadata(
|
||||||
file_id: &String,
|
file_id: &String,
|
||||||
file_name: String,
|
file_name: String,
|
||||||
|
file_path: &Path,
|
||||||
upload_config: &UploadConfig,
|
upload_config: &UploadConfig,
|
||||||
db: web::Data<sqlx::Pool<sqlx::Postgres>>,
|
db: web::Data<sqlx::Pool<sqlx::Postgres>>,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
|
@ -85,7 +86,8 @@ async fn insert_file_metadata(
|
||||||
.await;
|
.await;
|
||||||
if let Err(db_err) = db_insert {
|
if let Err(db_err) = db_insert {
|
||||||
log::error!("could not insert into datebase {:?}", db_err);
|
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);
|
log::error!("could not remove file {:?}", file_err);
|
||||||
}
|
}
|
||||||
return Err(error::ErrorInternalServerError(
|
return Err(error::ErrorInternalServerError(
|
||||||
|
|
Loading…
Reference in New Issue