begin implementing tmp file struct for better file management

This commit is contained in:
mykola2312 2024-03-30 04:39:56 +02:00
parent 1c356eb899
commit 82897a8e36
4 changed files with 52 additions and 4 deletions

View file

@ -1,12 +1,14 @@
use anyhow; use anyhow;
use rust_i18n::t; use rust_i18n::t;
use url::Url;
use std::str::{self, FromStr}; use std::str::{self, FromStr};
use std::time::Duration; use std::time::Duration;
use teloxide::dispatching::{dialogue, dialogue::InMemStorage, UpdateHandler}; use teloxide::dispatching::{dialogue, dialogue::InMemStorage, UpdateHandler};
use teloxide::types::{InputFile, InputMediaVideo, Me, MessageKind, MessageNewChatMembers, UpdateKind}; use teloxide::types::{
InputFile, InputMediaVideo, Me, MessageKind, MessageNewChatMembers, UpdateKind,
};
use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands}; use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands};
use tracing::{event, Level}; use tracing::{event, Level};
use url::Url;
use super::start::handle_new_chat_member; use super::start::handle_new_chat_member;
use super::types::*; use super::types::*;
@ -25,8 +27,8 @@ use super::start::{cmd_start, handle_my_chat_member};
pub async fn bot_main(db: DbPool) -> anyhow::Result<()> { pub async fn bot_main(db: DbPool) -> anyhow::Result<()> {
event!(Level::INFO, "start"); event!(Level::INFO, "start");
let bot = Bot::new(unwrap_env("BOT_TOKEN")) let bot =
.set_api_url(Url::from_str(&unwrap_env("BOT_API_URL"))?); Bot::new(unwrap_env("BOT_TOKEN")).set_api_url(Url::from_str(&unwrap_env("BOT_API_URL"))?);
let listener = Polling::builder(bot.clone()) let listener = Polling::builder(bot.clone())
.timeout(Duration::from_secs(parse_env("POLLING_TIMEOUT"))) .timeout(Duration::from_secs(parse_env("POLLING_TIMEOUT")))

View file

@ -9,6 +9,7 @@ use self::yt_dlp::{YtDlp, YtDlpError, YtDlpFormat, YtDlpInfo};
pub mod ffmpeg; pub mod ffmpeg;
mod spawn; mod spawn;
mod tmpfile;
pub mod yt_dlp; pub mod yt_dlp;
pub enum DownloadError { pub enum DownloadError {

44
src/dl/tmpfile.rs Normal file
View file

@ -0,0 +1,44 @@
use std::fs;
use tracing::{event, Level};
pub enum TmpFileError {
MakePathError
}
pub struct TmpFile {
pub path: String,
}
impl TmpFile {
pub fn new(filename: String) -> Result<Self, TmpFileError> {
let path = std::env::temp_dir()
.join(filename)
.into_os_string()
.into_string()
.map_err(|_| TmpFileError::MakePathError)?;
Ok(Self { path })
}
pub fn exists(&self) -> bool {
match fs::metadata(&self.path) {
Ok(_) => true,
Err(_) => false
}
}
fn delete_if_exists(&self) {
if self.exists() {
if let Err(e) = fs::remove_file(&self.path) {
event!(Level::ERROR, "{}", e);
}
}
event!(Level::INFO, "deleted {}", self.path);
}
}
impl Drop for TmpFile {
fn drop(&mut self) {
self.delete_if_exists();
}
}

View file

@ -1,4 +1,5 @@
use super::spawn::{spawn, SpawnError}; use super::spawn::{spawn, SpawnError};
use super::tmpfile::{TmpFile, TmpFileError};
use core::fmt; use core::fmt;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use serde::Deserialize; use serde::Deserialize;