Skip to content

Commit

Permalink
Update sounds.bin on upload or delete command
Browse files Browse the repository at this point in the history
reiyw committed Mar 18, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent f1ac57f commit a04d65f
Showing 2 changed files with 37 additions and 4 deletions.
6 changes: 3 additions & 3 deletions ssspam-bot/src/command.rs
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ use tracing::{info, warn};
use crate::{
core::{process_from_string, ChannelUserManager},
interpret_rhai,
web::update_data_json,
web::update_sounds_bin,
ChannelManager, Configs, GuildBroadcast, OpsMessage, SayCommands, SaySoundCache, SoundStorage,
};

@@ -559,7 +559,7 @@ async fn upload_impl(ctx: &Context, msg: &Message) -> anyhow::Result<u32> {
}
}

tokio::spawn(update_data_json(storage.read().dir.clone()));
tokio::spawn(update_sounds_bin(storage.read().dir.clone()));

storage.write().reload();

@@ -627,7 +627,7 @@ async fn delete_impl(ctx: &Context, mut args: Args) -> anyhow::Result<Vec<String
}
}

tokio::spawn(update_data_json(storage.read().dir.clone()));
tokio::spawn(update_sounds_bin(storage.read().dir.clone()));

clean_cache_impl(ctx).await?;

35 changes: 34 additions & 1 deletion ssspam-bot/src/web.rs
Original file line number Diff line number Diff line change
@@ -7,10 +7,11 @@ use std::{
};

use chrono::{DateTime, Utc};
use prost::Message;
use serde::Serialize;
use tempfile::tempdir;

use crate::SoundStorage;
use crate::{sound::ToSoundsProto, SoundStorage};

#[derive(Debug, Serialize)]
struct Data {
@@ -45,6 +46,17 @@ pub fn gen_data_json_from_sound_dir<P: AsRef<Path>, Q: AsRef<Path>>(
Ok(())
}

pub fn gen_sounds_bin_from_sound_dir<P: AsRef<Path>, Q: AsRef<Path>>(
sound_dir: P,
out_file: Q,
) -> anyhow::Result<()> {
let storage = SoundStorage::load(sound_dir);
let mut buf = Vec::new();
storage.files().cloned().to_sounds().encode(&mut buf)?;
fs::write(out_file, buf)?;
Ok(())
}

#[allow(clippy::future_not_send)]
pub async fn update_data_json<P: AsRef<Path>>(sound_dir: P) -> anyhow::Result<()> {
let temp_dir = tempdir()?;
@@ -60,3 +72,24 @@ pub async fn update_data_json<P: AsRef<Path>>(sound_dir: P) -> anyhow::Result<()
.await?;
Ok(())
}

#[allow(clippy::future_not_send)]
pub async fn update_sounds_bin<P: AsRef<Path>>(sound_dir: P) -> anyhow::Result<()> {
let temp_dir = tempdir()?;
let out_file = temp_dir.path().join("sounds.bin");

gen_sounds_bin_from_sound_dir(sound_dir, &out_file)?;

let data = fs::read(&out_file)?;
let client = cloud_storage::Client::default();
client
.object()
.create(
"surfpvparena",
data,
"dist/sounds.bin",
"application/octet-stream",
)
.await?;
Ok(())
}

0 comments on commit a04d65f

Please sign in to comment.