Delete old files

This commit is contained in:
2024-05-10 20:27:08 +02:00
parent bd62f1b076
commit e21273a2b8
2 changed files with 55 additions and 9 deletions

View File

@@ -44,6 +44,7 @@ pub static TEMP_FILES_CACHE: Lazy<Cache<i32, MessageId>> = Lazy::new(|| {
.build() .build()
}); });
pub async fn upload_file( pub async fn upload_file(
file: Bytes, file: Bytes,
filename: String, filename: String,
@@ -69,6 +70,7 @@ pub async fn upload_file(
} }
} }
pub async fn download_file(chat_id: i64, message_id: i32) -> Result<Option<File>, Box<dyn Error>> { pub async fn download_file(chat_id: i64, message_id: i32) -> Result<Option<File>, Box<dyn Error>> {
let bot = ROUND_ROBIN_BOT.get_bot(); let bot = ROUND_ROBIN_BOT.get_bot();
@@ -106,4 +108,31 @@ pub async fn download_file(chat_id: i64, message_id: i32) -> Result<Option<File>
}; };
Ok(Some(File::open(path).await?)) Ok(Some(File::open(path).await?))
} }
pub async fn clean_files() -> Result<(), Box<dyn Error>> {
let bots_folder = "/var/lib/telegram-bot-api/";
let documents_folder_name = "documents";
let mut bots_folder = tokio::fs::read_dir(bots_folder).await.unwrap();
while let Some(entry) = bots_folder.next_entry().await? {
if !entry.metadata().await.unwrap().is_dir() {
continue;
}
let documents_folder_path = entry.path().join(documents_folder_name);
let mut document_folder = tokio::fs::read_dir(documents_folder_path).await.unwrap();
while let Some(file) = document_folder.next_entry().await? {
let metadata = file.metadata().await.unwrap();
if metadata.created()?.elapsed().unwrap().as_secs() > 3600 {
let _ = tokio::fs::remove_file(file.path()).await;
}
}
}
Ok(())
}

View File

@@ -1,6 +1,7 @@
mod config; mod config;
mod core; mod core;
use core::file_utils::clean_files;
use std::{net::SocketAddr, str::FromStr}; use std::{net::SocketAddr, str::FromStr};
use sentry::{integrations::debug_images::DebugImagesIntegration, types::Dsn, ClientOptions}; use sentry::{integrations::debug_images::DebugImagesIntegration, types::Dsn, ClientOptions};
use sentry_tracing::EventFilter; use sentry_tracing::EventFilter;
@@ -9,6 +10,29 @@ use tracing_subscriber::{filter, layer::SubscriberExt, util::SubscriberInitExt};
use crate::core::views::get_router; use crate::core::views::get_router;
async fn start_app() {
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
let app = get_router().await;
println!("Start webserver...");
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
println!("Webserver shutdown...");
}
async fn cron_jobs() {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(5 * 60));
loop {
interval.tick().await;
let _ = clean_files().await;
}
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
dotenv::dotenv().ok(); dotenv::dotenv().ok();
@@ -33,12 +57,5 @@ async fn main() {
.with(sentry_layer) .with(sentry_layer)
.init(); .init();
let addr = SocketAddr::from(([0, 0, 0, 0], 8080)); tokio::join![cron_jobs(), start_app()];
let app = get_router().await;
println!("Start webserver...");
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
println!("Webserver shutdown...");
} }