diff --git a/src/api/core/mod.rs b/src/api/core/mod.rs index 4f0c19c9..f0b449c4 100644 --- a/src/api/core/mod.rs +++ b/src/api/core/mod.rs @@ -5,6 +5,8 @@ mod organizations; pub mod two_factor; mod sends; +pub use sends::start_send_deletion_scheduler; + pub fn routes() -> Vec { let mut mod_routes = routes![ clear_device_token, diff --git a/src/api/core/sends.rs b/src/api/core/sends.rs index 4680fa56..2b1c4334 100644 --- a/src/api/core/sends.rs +++ b/src/api/core/sends.rs @@ -25,6 +25,23 @@ pub fn routes() -> Vec { ] } +pub fn start_send_deletion_scheduler(pool: crate::db::DbPool) { + std::thread::spawn(move || { + loop { + if let Ok(conn) = pool.get() { + info!("Initiating send deletion"); + for send in Send::find_all(&conn) { + if chrono::Utc::now().naive_utc() >= send.deletion_date { + send.delete(&conn).ok(); + } + } + } + + std::thread::sleep(std::time::Duration::from_secs(3600)); + } + }); +} + #[derive(Deserialize)] #[allow(non_snake_case)] pub struct SendData { @@ -370,10 +387,6 @@ fn delete_send(id: String, headers: Headers, conn: DbConn, nt: Notify) -> EmptyR err!("Send is not owned by user") } - if send.atype == SendType::File as i32 { - std::fs::remove_dir_all(Path::new(&CONFIG.sends_folder()).join(&send.uuid)).ok(); - } - send.delete(&conn)?; nt.send_user_update(UpdateType::SyncSendDelete, &headers.user); diff --git a/src/api/mod.rs b/src/api/mod.rs index 6962e38c..840c65ff 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -11,6 +11,7 @@ use serde_json::Value; pub use crate::api::{ admin::routes as admin_routes, core::routes as core_routes, + core::start_send_deletion_scheduler, icons::routes as icons_routes, identity::routes as identity_routes, notifications::routes as notifications_routes, diff --git a/src/db/mod.rs b/src/db/mod.rs index f9dc2885..35d9bf78 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -37,6 +37,7 @@ macro_rules! generate_connections { pub enum DbConn { $( #[cfg($name)] $name(PooledConnection>), )+ } #[allow(non_camel_case_types)] + #[derive(Clone)] pub enum DbPool { $( #[cfg($name)] $name(Pool>), )+ } impl DbPool { diff --git a/src/db/models/send.rs b/src/db/models/send.rs index 5ab7cd01..0356d818 100644 --- a/src/db/models/send.rs +++ b/src/db/models/send.rs @@ -194,6 +194,10 @@ impl Send { pub fn delete(&self, conn: &DbConn) -> EmptyResult { self.update_users_revision(conn); + if self.atype == SendType::File as i32 { + std::fs::remove_dir_all(std::path::Path::new(&crate::CONFIG.sends_folder()).join(&self.uuid)).ok(); + } + db_run! { conn: { diesel::delete(sends::table.filter(sends::uuid.eq(&self.uuid))) .execute(conn) @@ -202,7 +206,7 @@ impl Send { } pub fn update_users_revision(&self, conn: &DbConn) { - match self.user_uuid { + match &self.user_uuid { Some(user_uuid) => { User::update_uuid_revision(&user_uuid, conn); } @@ -219,6 +223,12 @@ impl Send { Ok(()) } + pub fn find_all(conn: &DbConn) -> Vec { + db_run! {conn: { + sends::table.load::(conn).expect("Error loading sends").from_db() + }} + } + pub fn find_by_access_id(access_id: &str, conn: &DbConn) -> Option { use data_encoding::BASE64URL_NOPAD; use uuid::Uuid; diff --git a/src/main.rs b/src/main.rs index d792dba5..15b18b3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -313,6 +313,8 @@ fn launch_rocket(extra_debug: bool) { } }; + api::start_send_deletion_scheduler(pool.clone()); + let basepath = &CONFIG.domain_path(); // If adding more paths here, consider also adding them to