Automatically remove orphan files
This commit is contained in:
parent
6f18aafc33
commit
7c04acaf4b
@ -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,
|
||||
|
@ -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()?)?;
|
||||
|
Loading…
x
Reference in New Issue
Block a user