vw_small

Hardened fork of Vaultwarden (https://github.com/dani-garcia/vaultwarden) with fewer features.
git clone https://git.philomathiclife.com/repos/vw_small
Log | Files | Refs | README

commit 1fc6c30652b59a9dd7495393075df2a22246fa02
parent 46a1a013cd471c473964a7113dc2fab497732cc4
Author: Daniel GarcĂ­a <dani-garcia@users.noreply.github.com>
Date:   Mon, 22 Mar 2021 19:57:35 +0100

Send deletion thread and updated users revision

Diffstat:
Msrc/api/core/mod.rs | 2++
Msrc/api/core/sends.rs | 21+++++++++++++++++----
Msrc/api/mod.rs | 1+
Msrc/db/mod.rs | 1+
Msrc/db/models/send.rs | 12+++++++++++-
Msrc/main.rs | 2++
6 files changed, 34 insertions(+), 5 deletions(-)

diff --git 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<Route> { let mut mod_routes = routes![ clear_device_token, diff --git a/src/api/core/sends.rs b/src/api/core/sends.rs @@ -25,6 +25,23 @@ pub fn routes() -> Vec<rocket::Route> { ] } +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 @@ -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 @@ -37,6 +37,7 @@ macro_rules! generate_connections { pub enum DbConn { $( #[cfg($name)] $name(PooledConnection<ConnectionManager< $ty >>), )+ } #[allow(non_camel_case_types)] + #[derive(Clone)] pub enum DbPool { $( #[cfg($name)] $name(Pool<ConnectionManager< $ty >>), )+ } impl DbPool { diff --git 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<Self> { + db_run! {conn: { + sends::table.load::<SendDb>(conn).expect("Error loading sends").from_db() + }} + } + pub fn find_by_access_id(access_id: &str, conn: &DbConn) -> Option<Self> { use data_encoding::BASE64URL_NOPAD; use uuid::Uuid; diff --git 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