2021-04-04 01:38:29 +00:00
|
|
|
use async_std::{
|
|
|
|
channel::Receiver,
|
|
|
|
fs,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
task,
|
|
|
|
};
|
2020-07-09 17:27:24 +00:00
|
|
|
use chrono::{prelude::*, Duration};
|
2021-04-04 01:38:29 +00:00
|
|
|
use futures::{future::FutureExt, TryStreamExt};
|
2021-03-09 18:59:10 +00:00
|
|
|
use sqlx::{postgres::PgPool, Row};
|
2020-07-09 17:27:24 +00:00
|
|
|
|
2020-07-11 21:27:15 +00:00
|
|
|
pub(crate) async fn delete_old_files(receiver: Receiver<()>, db: PgPool, files_dir: PathBuf) {
|
2020-07-09 17:27:24 +00:00
|
|
|
loop {
|
|
|
|
wait_for_file_expiry(&receiver, &db).await;
|
2020-07-13 13:29:40 +00:00
|
|
|
|
|
|
|
let now = Local::now().naive_local();
|
2021-03-09 18:59:10 +00:00
|
|
|
let mut rows = sqlx::query("SELECT file_id FROM files WHERE files.valid_till < $1")
|
2020-07-13 13:29:40 +00:00
|
|
|
.bind(now)
|
2020-07-12 00:26:11 +00:00
|
|
|
.fetch(&db);
|
2021-03-09 18:59:10 +00:00
|
|
|
while let Some(row) = rows.try_next().await.expect("could not load expired files") {
|
|
|
|
let file_id: String = row.try_get("file_id").expect("we selected this column");
|
2021-04-04 01:38:29 +00:00
|
|
|
delete_content(&file_id, &files_dir)
|
|
|
|
.await
|
|
|
|
.expect("could not delete file");
|
2020-07-09 17:27:24 +00:00
|
|
|
}
|
2020-07-13 13:29:40 +00:00
|
|
|
|
2020-07-12 00:26:11 +00:00
|
|
|
sqlx::query("DELETE FROM files WHERE valid_till < $1")
|
2020-07-13 13:29:40 +00:00
|
|
|
.bind(now)
|
2020-07-09 17:27:24 +00:00
|
|
|
.execute(&db)
|
|
|
|
.await
|
|
|
|
.expect("could not delete expired files from database");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 01:38:29 +00:00
|
|
|
pub(crate) async fn delete_by_id(
|
|
|
|
db: &PgPool,
|
|
|
|
file_id: &str,
|
|
|
|
files_dir: &Path,
|
|
|
|
) -> Result<(), sqlx::Error> {
|
2021-09-11 00:08:47 +00:00
|
|
|
delete_content(file_id, files_dir).await?;
|
2021-04-04 01:38:29 +00:00
|
|
|
sqlx::query("DELETE FROM files WHERE file_id = $1")
|
|
|
|
.bind(file_id)
|
|
|
|
.execute(db)
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn delete_content(file_id: &str, files_dir: &Path) -> Result<(), std::io::Error> {
|
|
|
|
let path = files_dir.join(file_id);
|
|
|
|
if path.exists().await {
|
|
|
|
log::info!("delete file {}", file_id);
|
|
|
|
fs::remove_file(&path).await?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-09 17:27:24 +00:00
|
|
|
async fn wait_for_file_expiry(receiver: &Receiver<()>, db: &PgPool) {
|
2021-03-09 18:59:10 +00:00
|
|
|
let mut rows = sqlx::query("SELECT MIN(valid_till) as min from files").fetch(db);
|
|
|
|
let row = rows
|
|
|
|
.try_next()
|
2020-07-09 17:27:24 +00:00
|
|
|
.await
|
2020-07-12 00:26:11 +00:00
|
|
|
.expect("could not fetch expiring files from database")
|
|
|
|
.expect("postgres min did not return any row");
|
|
|
|
let valid_till: Option<NaiveDateTime> = row.get("min");
|
|
|
|
let next_timeout = match valid_till {
|
|
|
|
Some(valid_till) => valid_till.signed_duration_since(Local::now().naive_local()),
|
2020-07-09 17:27:24 +00:00
|
|
|
None => Duration::days(1),
|
|
|
|
};
|
|
|
|
let positive_timeout = next_timeout
|
|
|
|
.to_std()
|
|
|
|
.unwrap_or_else(|_| std::time::Duration::from_secs(0));
|
|
|
|
futures::select! {
|
|
|
|
_ = task::sleep(positive_timeout).fuse() => {}
|
|
|
|
_ = receiver.recv().fuse() => {}
|
|
|
|
}
|
|
|
|
}
|