Automatically remove orphan files
This commit is contained in:
		@@ -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;
 | 
			
		||||
 
 | 
			
		||||
@@ -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?;
 | 
			
		||||
 
 | 
			
		||||
@@ -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<Vec<u8>> {
 | 
			
		||||
pub async fn delete_file_if_unused(id: FileID) -> anyhow::Result<bool> {
 | 
			
		||||
    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()?);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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()?)?;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user