diff --git a/src/cmds/manage.rs b/src/cmds/manage.rs index cceefa1..44a1617 100644 --- a/src/cmds/manage.rs +++ b/src/cmds/manage.rs @@ -2,7 +2,6 @@ use poise::{ serenity_prelude::{Attachment, CreateAttachment, GuildId, RoleId}, CreateReply, }; -use tokio::fs::File; #[cfg(feature = "metrics")] use crate::metrics::{DELETE_COUNTER, UPLOAD_COUNTER}; @@ -227,15 +226,12 @@ pub async fn download_file( match sound.first() { Some(sound) => { - let source = sound.store_sound_source(&ctx.data().database).await?; - - let file = File::open(&source).await?; let name = format!("{}-{}.opus", sound.id, sound.name); - ctx.send( - CreateReply::default() - .attachment(CreateAttachment::file(&file, name.as_str()).await?), - ) + ctx.send(CreateReply::default().attachment(CreateAttachment::bytes( + sound.src(&ctx.data().database).await, + name.as_str(), + ))) .await?; } diff --git a/src/models/sound.rs b/src/models/sound.rs index 2f2cb23..c9469a7 100644 --- a/src/models/sound.rs +++ b/src/models/sound.rs @@ -1,9 +1,7 @@ -use std::{env, path::Path}; - use poise::serenity_prelude::async_trait; use songbird::input::Input; use sqlx::Executor; -use tokio::{fs::File, io::AsyncWriteExt, process::Command}; +use tokio::process::Command; use crate::{consts::UPLOAD_MAX_SIZE, error::ErrorTypes, Data, Database}; @@ -399,7 +397,7 @@ impl SoundCtx for Data { } impl Sound { - async fn src(&self, db_pool: impl Executor<'_, Database = Database>) -> Vec { + pub(crate) async fn src(&self, db_pool: impl Executor<'_, Database = Database>) -> Vec { struct Src { src: Vec, } @@ -420,31 +418,11 @@ impl Sound { record.src } - pub async fn store_sound_source( - &self, - db_pool: impl Executor<'_, Database = Database>, - ) -> Result> { - let caching_location = env::var("CACHING_LOCATION").unwrap_or(String::from("/tmp")); - - let path_name = format!("{}/sound-{}.opus", caching_location, self.id); - let path = Path::new(&path_name); - - if !path.exists() { - let mut file = File::create(&path).await?; - - file.write_all(&self.src(db_pool).await).await?; - } - - Ok(path_name) - } - pub async fn playable( &self, db_pool: impl Executor<'_, Database = Database>, ) -> Result> { - let path_name = self.store_sound_source(db_pool).await?; - - Ok(Input::from(path_name)) + Ok(Input::from(self.src(db_pool).await)) } pub async fn count_user_sounds>(