ran cargo fmt over code. added kill_on_drop to ffmpeg to stop producing zombies

This commit is contained in:
jude 2020-10-16 15:47:06 +01:00
parent 9c6d6a4db3
commit 697d8aa9f1
3 changed files with 671 additions and 368 deletions

View File

@ -17,17 +17,16 @@ impl GuildData {
SELECT id, name, prefix, volume, allow_greets SELECT id, name, prefix, volume, allow_greets
FROM servers FROM servers
WHERE id = ? WHERE id = ?
", guild.id.as_u64() ",
guild.id.as_u64()
) )
.fetch_one(&db_pool) .fetch_one(&db_pool)
.await; .await;
match guild_data { match guild_data {
Ok(g) => Some(g), Ok(g) => Some(g),
Err(sqlx::Error::RowNotFound) => { Err(sqlx::Error::RowNotFound) => Self::create_from_guild(guild, db_pool).await.ok(),
Self::create_from_guild(guild, db_pool).await.ok()
}
Err(e) => { Err(e) => {
println!("{:?}", e); println!("{:?}", e);
@ -37,36 +36,45 @@ SELECT id, name, prefix, volume, allow_greets
} }
} }
pub async fn create_from_guild(guild: Guild, db_pool: MySqlPool) -> Result<GuildData, Box<dyn std::error::Error + Send + Sync>> { pub async fn create_from_guild(
guild: Guild,
db_pool: MySqlPool,
) -> Result<GuildData, Box<dyn std::error::Error + Send + Sync>> {
sqlx::query!( sqlx::query!(
" "
INSERT INTO servers (id, name) INSERT INTO servers (id, name)
VALUES (?, ?) VALUES (?, ?)
", guild.id.as_u64(), guild.name ",
guild.id.as_u64(),
guild.name
) )
.execute(&db_pool) .execute(&db_pool)
.await?; .await?;
sqlx::query!( sqlx::query!(
" "
INSERT IGNORE INTO roles (guild_id, role) INSERT IGNORE INTO roles (guild_id, role)
VALUES (?, ?) VALUES (?, ?)
", ",
guild.id.as_u64(), guild.id.as_u64() guild.id.as_u64(),
guild.id.as_u64()
) )
.execute(&db_pool) .execute(&db_pool)
.await?; .await?;
Ok(GuildData { Ok(GuildData {
id: *guild.id.as_u64(), id: *guild.id.as_u64(),
name: Some(guild.name.clone()), name: Some(guild.name.clone()),
prefix: String::from("?"), prefix: String::from("?"),
volume: 100, volume: 100,
allow_greets: true allow_greets: true,
}) })
} }
pub async fn commit(&self, db_pool: MySqlPool) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { pub async fn commit(
&self,
db_pool: MySqlPool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
sqlx::query!( sqlx::query!(
" "
UPDATE servers UPDATE servers
@ -78,10 +86,14 @@ SET
WHERE WHERE
id = ? id = ?
", ",
self.name, self.prefix, self.volume, self.allow_greets, self.id self.name,
self.prefix,
self.volume,
self.allow_greets,
self.id
) )
.execute(&db_pool) .execute(&db_pool)
.await?; .await?;
Ok(()) Ok(())
} }

File diff suppressed because it is too large Load Diff

View File

@ -2,21 +2,11 @@ use super::error::ErrorTypes;
use sqlx::mysql::MySqlPool; use sqlx::mysql::MySqlPool;
use serenity::voice::{ use serenity::voice::{ffmpeg, AudioSource};
AudioSource,
ffmpeg,
};
use tokio::{ use tokio::{fs::File, process::Command};
fs::File,
process::Command,
};
use std::{
env,
path::Path,
};
use std::{env, path::Path};
pub struct Sound { pub struct Sound {
pub name: String, pub name: String,
@ -28,20 +18,23 @@ pub struct Sound {
} }
impl Sound { impl Sound {
pub async fn search_for_sound(query: &str, guild_id: u64, user_id: u64, db_pool: MySqlPool, strict: bool) -> Result<Vec<Sound>, sqlx::Error> { pub async fn search_for_sound(
query: &str,
guild_id: u64,
user_id: u64,
db_pool: MySqlPool,
strict: bool,
) -> Result<Vec<Sound>, sqlx::Error> {
fn extract_id(s: &str) -> Option<u32> { fn extract_id(s: &str) -> Option<u32> {
if s.len() > 3 && s.to_lowercase().starts_with("id:") { if s.len() > 3 && s.to_lowercase().starts_with("id:") {
match s[3..].parse::<u32>() { match s[3..].parse::<u32>() {
Ok(id) => Some(id), Ok(id) => Some(id),
Err(_) => None Err(_) => None,
} }
} } else if let Ok(id) = s.parse::<u32>() {
else if let Ok(id) = s.parse::<u32>() {
Some(id) Some(id)
} } else {
else {
None None
} }
} }
@ -58,14 +51,15 @@ SELECT name, id, plays, public, server_id, uploader_id
server_id = ? server_id = ?
) )
", ",
id, user_id, guild_id id,
user_id,
guild_id
) )
.fetch_all(&db_pool) .fetch_all(&db_pool)
.await?; .await?;
Ok(sound) Ok(sound)
} } else {
else {
let name = query; let name = query;
let sound; let sound;
@ -82,13 +76,15 @@ SELECT name, id, plays, public, server_id, uploader_id
) )
ORDER BY uploader_id = ? DESC, server_id = ? DESC, public = 1 DESC, rand() ORDER BY uploader_id = ? DESC, server_id = ? DESC, public = 1 DESC, rand()
", ",
name, user_id, guild_id, user_id, guild_id name,
user_id,
guild_id,
user_id,
guild_id
) )
.fetch_all(&db_pool) .fetch_all(&db_pool)
.await?; .await?;
} else {
}
else {
sound = sqlx::query_as_unchecked!( sound = sqlx::query_as_unchecked!(
Self, Self,
" "
@ -101,10 +97,14 @@ SELECT name, id, plays, public, server_id, uploader_id
) )
ORDER BY uploader_id = ? DESC, server_id = ? DESC, public = 1 DESC, rand() ORDER BY uploader_id = ? DESC, server_id = ? DESC, public = 1 DESC, rand()
", ",
name, user_id, guild_id, user_id, guild_id name,
user_id,
guild_id,
user_id,
guild_id
) )
.fetch_all(&db_pool) .fetch_all(&db_pool)
.await?; .await?;
} }
Ok(sound) Ok(sound)
@ -113,7 +113,7 @@ SELECT name, id, plays, public, server_id, uploader_id
async fn get_self_src(&self, db_pool: MySqlPool) -> Vec<u8> { async fn get_self_src(&self, db_pool: MySqlPool) -> Vec<u8> {
struct Src { struct Src {
src: Vec<u8> src: Vec<u8>,
} }
let record = sqlx::query_as_unchecked!( let record = sqlx::query_as_unchecked!(
@ -126,14 +126,17 @@ SELECT src
", ",
self.id self.id
) )
.fetch_one(&db_pool) .fetch_one(&db_pool)
.await.unwrap(); .await
.unwrap();
return record.src return record.src;
} }
pub async fn store_sound_source(&self, db_pool: MySqlPool) -> Result<Box<dyn AudioSource>, Box<dyn std::error::Error + Send + Sync>> { pub async fn store_sound_source(
&self,
db_pool: MySqlPool,
) -> Result<Box<dyn AudioSource>, Box<dyn std::error::Error + Send + Sync>> {
let caching_location = env::var("CACHING_LOCATION").unwrap_or(String::from("/tmp")); let caching_location = env::var("CACHING_LOCATION").unwrap_or(String::from("/tmp"));
let path_name = format!("{}/sound-{}", caching_location, self.id); let path_name = format!("{}/sound-{}", caching_location, self.id);
@ -150,39 +153,53 @@ SELECT src
Ok(ffmpeg(path_name).await?) Ok(ffmpeg(path_name).await?)
} }
pub async fn count_user_sounds(user_id: u64, db_pool: MySqlPool) -> Result<u32, sqlx::error::Error> { pub async fn count_user_sounds(
user_id: u64,
db_pool: MySqlPool,
) -> Result<u32, sqlx::error::Error> {
let c = sqlx::query!( let c = sqlx::query!(
" "
SELECT COUNT(1) as count SELECT COUNT(1) as count
FROM sounds FROM sounds
WHERE uploader_id = ? WHERE uploader_id = ?
", ",
user_id user_id
) )
.fetch_one(&db_pool) .fetch_one(&db_pool)
.await?.count; .await?
.count;
Ok(c as u32) Ok(c as u32)
} }
pub async fn count_named_user_sounds(user_id: u64, name: &String, db_pool: MySqlPool) -> Result<u32, sqlx::error::Error> { pub async fn count_named_user_sounds(
user_id: u64,
name: &String,
db_pool: MySqlPool,
) -> Result<u32, sqlx::error::Error> {
let c = sqlx::query!( let c = sqlx::query!(
" "
SELECT COUNT(1) as count SELECT COUNT(1) as count
FROM sounds FROM sounds
WHERE WHERE
uploader_id = ? AND uploader_id = ? AND
name = ? name = ?
", ",
user_id, name user_id,
name
) )
.fetch_one(&db_pool) .fetch_one(&db_pool)
.await?.count; .await?
.count;
Ok(c as u32) Ok(c as u32)
} }
pub async fn set_as_greet(&self, user_id: u64, db_pool: MySqlPool) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { pub async fn set_as_greet(
&self,
user_id: u64,
db_pool: MySqlPool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
sqlx::query!( sqlx::query!(
" "
UPDATE users UPDATE users
@ -191,15 +208,19 @@ SET
WHERE WHERE
user = ? user = ?
", ",
self.id, user_id self.id,
user_id
) )
.execute(&db_pool) .execute(&db_pool)
.await?; .await?;
Ok(()) Ok(())
} }
pub async fn commit(&self, db_pool: MySqlPool) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { pub async fn commit(
&self,
db_pool: MySqlPool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
sqlx::query!( sqlx::query!(
" "
UPDATE sounds UPDATE sounds
@ -209,15 +230,20 @@ SET
WHERE WHERE
id = ? id = ?
", ",
self.plays, self.public, self.id self.plays,
self.public,
self.id
) )
.execute(&db_pool) .execute(&db_pool)
.await?; .await?;
Ok(()) Ok(())
} }
pub async fn delete(&self, db_pool: MySqlPool) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { pub async fn delete(
&self,
db_pool: MySqlPool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
sqlx::query!( sqlx::query!(
" "
DELETE DELETE
@ -226,15 +252,22 @@ DELETE
", ",
self.id self.id
) )
.execute(&db_pool) .execute(&db_pool)
.await?; .await?;
Ok(()) Ok(())
} }
pub async fn create_anon(name: &str, src_url: &str, server_id: u64, user_id: u64, db_pool: MySqlPool) -> Result<u64, Box<dyn std::error::Error + Send + Sync + Send>> { pub async fn create_anon(
name: &str,
src_url: &str,
server_id: u64,
user_id: u64,
db_pool: MySqlPool,
) -> Result<u64, Box<dyn std::error::Error + Send + Sync + Send>> {
async fn process_src(src_url: &str) -> Option<Vec<u8>> { async fn process_src(src_url: &str) -> Option<Vec<u8>> {
let future = Command::new("ffmpeg") let output = Command::new("ffmpeg")
.kill_on_drop(true)
.arg("-i") .arg("-i")
.arg(src_url) .arg(src_url)
.arg("-loglevel") .arg("-loglevel")
@ -246,16 +279,14 @@ DELETE
.arg("-fs") .arg("-fs")
.arg("1048576") .arg("1048576")
.arg("pipe:1") .arg("pipe:1")
.output(); .output()
.await;
let output = future.await;
match output { match output {
Ok(out) => { Ok(out) => {
if out.status.success() { if out.status.success() {
Some(out.stdout) Some(out.stdout)
} } else {
else {
None None
} }
} }
@ -269,25 +300,32 @@ DELETE
match source { match source {
Some(data) => { Some(data) => {
match sqlx::query!( match sqlx::query!(
" "
INSERT INTO sounds (name, server_id, uploader_id, public, src) INSERT INTO sounds (name, server_id, uploader_id, public, src)
VALUES (?, ?, ?, 1, ?) VALUES (?, ?, ?, 1, ?)
", ",
name, server_id, user_id, data name,
server_id,
user_id,
data
) )
.execute(&db_pool) .execute(&db_pool)
.await { .await
{
Ok(u) => Ok(u), Ok(u) => Ok(u),
Err(e) => Err(Box::new(e)) Err(e) => Err(Box::new(e)),
} }
} }
None => Err(Box::new(ErrorTypes::InvalidFile)) None => Err(Box::new(ErrorTypes::InvalidFile)),
} }
} }
pub async fn get_user_sounds(user_id: u64, db_pool: MySqlPool) -> Result<Vec<Sound>, Box<dyn std::error::Error + Send + Sync>> { pub async fn get_user_sounds(
user_id: u64,
db_pool: MySqlPool,
) -> Result<Vec<Sound>, Box<dyn std::error::Error + Send + Sync>> {
let sounds = sqlx::query_as_unchecked!( let sounds = sqlx::query_as_unchecked!(
Sound, Sound,
" "
@ -296,12 +334,17 @@ SELECT name, id, plays, public, server_id, uploader_id
WHERE uploader_id = ? WHERE uploader_id = ?
", ",
user_id user_id
).fetch_all(&db_pool).await?; )
.fetch_all(&db_pool)
.await?;
Ok(sounds) Ok(sounds)
} }
pub async fn get_guild_sounds(guild_id: u64, db_pool: MySqlPool) -> Result<Vec<Sound>, Box<dyn std::error::Error + Send + Sync>> { pub async fn get_guild_sounds(
guild_id: u64,
db_pool: MySqlPool,
) -> Result<Vec<Sound>, Box<dyn std::error::Error + Send + Sync>> {
let sounds = sqlx::query_as_unchecked!( let sounds = sqlx::query_as_unchecked!(
Sound, Sound,
" "
@ -310,7 +353,9 @@ SELECT name, id, plays, public, server_id, uploader_id
WHERE server_id = ? WHERE server_id = ?
", ",
guild_id guild_id
).fetch_all(&db_pool).await?; )
.fetch_all(&db_pool)
.await?;
Ok(sounds) Ok(sounds)
} }