diff --git a/moneymgr_backend/src/controllers/files_controller.rs b/moneymgr_backend/src/controllers/files_controller.rs index 8b6a4b0..d572029 100644 --- a/moneymgr_backend/src/controllers/files_controller.rs +++ b/moneymgr_backend/src/controllers/files_controller.rs @@ -58,3 +58,15 @@ pub async fn serve_file(req: HttpRequest, file: &File) -> HttpResult { )) .body(files_service::get_file_content(file).await?)) } + +/// Delete an uploaded file +pub async fn delete(file_extractor: FileIdExtractor) -> HttpResult { + match files_service::delete_file_if_unused(file_extractor.as_ref().id()).await { + Ok(true) => Ok(HttpResponse::Accepted().finish()), + Ok(false) => Ok(HttpResponse::Conflict().finish()), + Err(e) => { + log::error!("Failed to delete file: {e}"); + Ok(HttpResponse::InternalServerError().finish()) + } + } +} diff --git a/moneymgr_backend/src/main.rs b/moneymgr_backend/src/main.rs index 91de894..1cc3964 100644 --- a/moneymgr_backend/src/main.rs +++ b/moneymgr_backend/src/main.rs @@ -125,7 +125,10 @@ async fn main() -> std::io::Result<()> { "/api/file/{file_id}", web::get().to(files_controller::download), ) - // TODO Delete file + .route( + "/api/file/{file_id}", + web::delete().to(files_controller::delete), + ) // Static assets .route("/", web::get().to(static_controller::root_index)) .route(