diff --git a/moneymgr_backend/migrations/2025-03-17-173101_initial_structure/up.sql b/moneymgr_backend/migrations/2025-03-17-173101_initial_structure/up.sql index 9184d5e..ee2e8e7 100644 --- a/moneymgr_backend/migrations/2025-03-17-173101_initial_structure/up.sql +++ b/moneymgr_backend/migrations/2025-03-17-173101_initial_structure/up.sql @@ -52,7 +52,7 @@ CREATE TABLE movements account_id INTEGER NOT NULL REFERENCES accounts ON DELETE CASCADE, time BIGINT NOT NULL, label VARCHAR(200) NOT NULL, - file_id INT REFERENCES files ON DELETE SET NULL, + file_id INT REFERENCES files ON DELETE RESTRICT, amount REAL NOT NULL, checked BOOLEAN NOT NULL DEFAULT false, time_create BIGINT NOT NULL, @@ -62,7 +62,7 @@ CREATE TABLE movements CREATE TABLE inbox ( id SERIAL PRIMARY KEY, - file_id INTEGER NOT NULL REFERENCES files ON DELETE CASCADE, + file_id INTEGER NOT NULL REFERENCES files ON DELETE RESTRICT, user_id INTEGER NOT NULL REFERENCES users ON DELETE CASCADE, account_id INTEGER REFERENCES accounts ON DELETE CASCADE, time_create BIGINT NOT NULL, diff --git a/moneymgr_backend/src/constants.rs b/moneymgr_backend/src/constants.rs index 0c57108..8f7845e 100644 --- a/moneymgr_backend/src/constants.rs +++ b/moneymgr_backend/src/constants.rs @@ -21,3 +21,6 @@ pub mod sessions { /// Maximum uploaded file size (15MB) pub const MAX_UPLOAD_FILE_SIZE: usize = 15 * 1024 * 1024; + +/// Minimum elapsed time before a file can be deleted by garbage collector (2 hours) +pub const MIN_FILE_LIFETIME: u64 = 3600 * 2; diff --git a/moneymgr_backend/src/routines.rs b/moneymgr_backend/src/routines.rs index cb9a4ad..10745e8 100644 --- a/moneymgr_backend/src/routines.rs +++ b/moneymgr_backend/src/routines.rs @@ -1,5 +1,5 @@ use crate::app_config::AppConfig; -use crate::services::tokens_service; +use crate::services::{files_service, tokens_service}; use std::time::Duration; /// The "cron" of the project @@ -20,7 +20,8 @@ pub async fn main_routine() { } async fn exec_routine() -> anyhow::Result<()> { - // TODO : remove orphan attachment + // Remove orphan attachments + files_service::run_garbage_collector().await?; // Remove expired tokens tokens_service::cleanup().await?; diff --git a/moneymgr_backend/src/services/files_service.rs b/moneymgr_backend/src/services/files_service.rs index 08db222..505fa23 100644 --- a/moneymgr_backend/src/services/files_service.rs +++ b/moneymgr_backend/src/services/files_service.rs @@ -1,5 +1,6 @@ use crate::connections::db_connection::db; use crate::connections::s3_connection; +use crate::constants; use crate::models::files::{File, FileID, NewFile}; use crate::models::users::UserID; use crate::schema::files; @@ -86,6 +87,11 @@ pub async fn get_file_content(file: &File) -> anyhow::Result> { pub async fn delete_file_if_unused(id: FileID) -> anyhow::Result { let file = get_file_with_id(id)?; + // Check if the file is old enough + if file.time_create as u64 + constants::MIN_FILE_LIFETIME > time() { + return Ok(false); + } + let res = diesel::delete(files::dsl::files.filter(files::dsl::id.eq(file.id().0))) .execute(&mut db()?); diff --git a/moneymgr_backend/src/services/tokens_service.rs b/moneymgr_backend/src/services/tokens_service.rs index d0d702f..c39f26e 100644 --- a/moneymgr_backend/src/services/tokens_service.rs +++ b/moneymgr_backend/src/services/tokens_service.rs @@ -100,7 +100,7 @@ pub async fn delete(user_id: UserID, token_id: TokenID) -> anyhow::Result<()> { /// Remove outdated token pub async fn cleanup() -> anyhow::Result<()> { let query = format!( - "DELETE from token where time_used + max_inactivity < {};", + "DELETE from tokens where time_used + max_inactivity < {};", time() ); diesel::sql_query(query).execute(&mut db()?)?;