diff --git a/src/deploy_mastodon.rs b/src/deploy_mastodon.rs index 985cfd3..803c6f4 100644 --- a/src/deploy_mastodon.rs +++ b/src/deploy_mastodon.rs @@ -1,33 +1,10 @@ -use std::{collections::HashMap, io::Read, path::PathBuf, process::Command}; +use std::{collections::HashMap, io::Read, path::PathBuf}; use crate::{ mastodon_api::{AdminEmoji, MastodonAPI}, - sticker_config::{Sticker, StickerConfig}, + sticker_config::{Sticker, StickerConfig}, utils::convert_to_png, }; -fn convert_to_png(path: PathBuf) -> Vec { - let output = Command::new("convert") - .args([ - "-format", - "png", - "-resize", - "128x128", - path.as_os_str().to_str().unwrap(), - "png:-", - ]) - .output() - .expect("failed to execute process"); - - if !output.status.success() { - panic!( - "failed to run convert, stderr: {}", - String::from_utf8_lossy(&output.stderr) - ); - } - - output.stdout -} - pub async fn deploy_mastodon( deploy_id: String, sticker_config: StickerConfig, diff --git a/src/deploy_zip.rs b/src/deploy_zip.rs index d48e61f..ae4a7e8 100644 --- a/src/deploy_zip.rs +++ b/src/deploy_zip.rs @@ -1,6 +1,14 @@ -use std::{collections::HashMap, io::{Read, Write}, path::{PathBuf, Path}, fs::File}; +use std::{ + collections::HashMap, + fs::File, + io::{Read, Write}, + path::{Path, PathBuf}, +}; -use crate::sticker_config::{Sticker, StickerConfig}; +use crate::{ + sticker_config::{Sticker, StickerConfig}, + utils::convert_to_png, +}; pub async fn deploy_zip( deploy_id: String, @@ -9,6 +17,8 @@ pub async fn deploy_zip( ) { let deploy_where = sticker_config.deploy_where.get(&deploy_id).unwrap(); + let should_convert_png = deploy_where.zip.as_ref().unwrap().convert_png; + let pack_contents = sticker_config .sticker_sets .get(&deploy_where.pack_id) @@ -32,11 +42,28 @@ pub async fn deploy_zip( let mut buffer = Vec::new(); for (name, sticker) in pack_stickers.into_iter() { let sticker_path = PathBuf::from(&base_stickerdb_path).join(sticker.file); - let sticker_extension = (&sticker_path).extension().unwrap().to_string_lossy(); - let mut sticker_file = File::open(&sticker_path).unwrap(); - zip.start_file(format!("{}.{}", name, sticker_extension), Default::default()).unwrap(); - sticker_file.read_to_end(&mut buffer).unwrap(); + let sticker_extension = match should_convert_png { + false => (&sticker_path) + .extension() + .unwrap() + .to_string_lossy() + .to_string(), + true => "png".to_string(), + }; + + zip.start_file( + format!("{}.{}", name, sticker_extension), + Default::default(), + ) + .unwrap(); + if should_convert_png { + let file_data = convert_to_png(sticker_path); + buffer.write_all(&file_data); + } else { + let mut sticker_file = File::open(&sticker_path).unwrap(); + sticker_file.read_to_end(&mut buffer).unwrap(); + } zip.write_all(&buffer).unwrap(); buffer.clear(); } diff --git a/src/main.rs b/src/main.rs index 32a8f94..dc2fc21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ pub mod deploy_zip; pub mod mastodon_api; pub mod sticker_config; pub mod tg_api; +pub mod utils; use clap::Parser; diff --git a/src/sticker_config.rs b/src/sticker_config.rs index b1224d4..a960aad 100644 --- a/src/sticker_config.rs +++ b/src/sticker_config.rs @@ -16,6 +16,7 @@ pub struct DeployWhere { pub discord: Option>, pub telegram: Option, pub mastodon: Option, + pub zip: Option, } #[derive(Debug, Clone, Deserialize)] @@ -43,6 +44,11 @@ fn mastodon_credential_name_default() -> String { "mastodon".to_string() } +#[derive(Debug, Clone, Deserialize)] +pub struct ZipDeployLocation { + pub convert_png: bool, +} + #[derive(Debug, Clone, Deserialize)] pub struct DiscordDeployLocation { pub deploy_name: String, diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..be761dc --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,24 @@ +use std::{path::PathBuf, process::Command}; + +pub fn convert_to_png(path: PathBuf) -> Vec { + let output = Command::new("convert") + .args([ + "-format", + "png", + "-resize", + "128x128", + path.as_os_str().to_str().unwrap(), + "png:-", + ]) + .output() + .expect("failed to execute process"); + + if !output.status.success() { + panic!( + "failed to run convert, stderr: {}", + String::from_utf8_lossy(&output.stderr) + ); + } + + output.stdout +} \ No newline at end of file