Remove file-based audio streaming

This commit is contained in:
jude 2023-12-20 16:51:44 +00:00
parent e632f55b4e
commit 5364e41560
2 changed files with 7 additions and 33 deletions

View File

@ -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?;
}

View File

@ -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<u8> {
pub(crate) async fn src(&self, db_pool: impl Executor<'_, Database = Database>) -> Vec<u8> {
struct Src {
src: Vec<u8>,
}
@ -420,31 +418,11 @@ impl Sound {
record.src
}
pub async fn store_sound_source(
&self,
db_pool: impl Executor<'_, Database = Database>,
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
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<Input, Box<dyn std::error::Error + Send + Sync>> {
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<U: Into<u64>>(