From 8d039e9404129414e446137eb5c5ec172bfdae8e Mon Sep 17 00:00:00 2001 From: chaos Date: Sat, 7 Oct 2023 18:33:40 +0100 Subject: [PATCH] add deploy zip --- .gitignore | 3 ++- Cargo.lock | 22 ++++++++++++++++++++++ Cargo.toml | 1 + src/cmd_deploy.rs | 10 ++++++++++ src/deploy_zip.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 6 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/deploy_zip.rs diff --git a/.gitignore b/.gitignore index 7179f6c..91033ca 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ dist build result -.direnv \ No newline at end of file +.direnv +*.zip \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 4eda5b2..a408a41 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -209,6 +209,15 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-utils" +version = "0.8.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" +dependencies = [ + "cfg-if", +] + [[package]] name = "crypto-common" version = "0.1.6" @@ -1214,6 +1223,7 @@ dependencies = [ "tokio", "tracing", "tracing-subscriber", + "zip", ] [[package]] @@ -1749,3 +1759,15 @@ checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d" dependencies = [ "winapi", ] + +[[package]] +name = "zip" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "760394e246e4c28189f19d488c058bf16f564016aefac5d32bb1f3b51d5e9261" +dependencies = [ + "byteorder", + "crc32fast", + "crossbeam-utils", + "flate2", +] diff --git a/Cargo.toml b/Cargo.toml index 9a1aa57..1011911 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ serde = { version = "1", features = ["derive"] } serde_json = "1.0" serde_yaml = "0.9.9" serde_with = "1.3.1" +zip = { version = "0.6.6", default-features = false, features = ["deflate"] } serenity = { version = "0.11" } tracing = "0.1.37" tracing-subscriber = "0.3.17" diff --git a/src/cmd_deploy.rs b/src/cmd_deploy.rs index bf0372a..74e1841 100644 --- a/src/cmd_deploy.rs +++ b/src/cmd_deploy.rs @@ -1,6 +1,7 @@ use crate::deploy_discord::deploy_discord; use crate::deploy_mastodon::deploy_mastodon; use crate::deploy_telegram::deploy_telegram; +use crate::deploy_zip::deploy_zip; use crate::sticker_config::StickerConfig; use std::collections::HashMap; @@ -46,6 +47,15 @@ pub async fn deploy(args: crate::args::DeployArgs) { ) .await; } + "zip" => { + println!("deploying {} to zip", &deploy_id); + deploy_zip( + deploy_id, + sticker_config, + args.folder.clone(), + ) + .await; + } "telegram" => { println!("deploying {} to telegram", &deploy_id); deploy_telegram( diff --git a/src/deploy_zip.rs b/src/deploy_zip.rs new file mode 100644 index 0000000..d48e61f --- /dev/null +++ b/src/deploy_zip.rs @@ -0,0 +1,44 @@ +use std::{collections::HashMap, io::{Read, Write}, path::{PathBuf, Path}, fs::File}; + +use crate::sticker_config::{Sticker, StickerConfig}; + +pub async fn deploy_zip( + deploy_id: String, + sticker_config: StickerConfig, + base_stickerdb_path: String, +) { + let deploy_where = sticker_config.deploy_where.get(&deploy_id).unwrap(); + + let pack_contents = sticker_config + .sticker_sets + .get(&deploy_where.pack_id) + .unwrap(); + + let pack_stickers: HashMap = pack_contents + .iter() + .map(|sticker_name| { + return ( + sticker_name.clone(), + sticker_config.stickers.get(sticker_name).unwrap().clone(), + ); + }) + .collect(); + + let zip_path = format!("{}.zip", &deploy_id).to_string(); + let path = Path::new(&zip_path); + let file = File::create(path).unwrap(); + + let mut zip = zip::ZipWriter::new(file); + 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(); + zip.write_all(&buffer).unwrap(); + buffer.clear(); + } + zip.finish().unwrap(); +} diff --git a/src/main.rs b/src/main.rs index 816fb50..32a8f94 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ pub mod cmd_deploy; pub mod deploy_discord; pub mod deploy_mastodon; pub mod deploy_telegram; +pub mod deploy_zip; pub mod mastodon_api; pub mod sticker_config; pub mod tg_api;