1
0
Fork 0

add convert png for zip

This commit is contained in:
chaos 2023-10-07 18:43:05 +01:00
parent 8d039e9404
commit 73293f47ba
No known key found for this signature in database
5 changed files with 66 additions and 31 deletions

View file

@ -1,33 +1,10 @@
use std::{collections::HashMap, io::Read, path::PathBuf, process::Command}; use std::{collections::HashMap, io::Read, path::PathBuf};
use crate::{ use crate::{
mastodon_api::{AdminEmoji, MastodonAPI}, mastodon_api::{AdminEmoji, MastodonAPI},
sticker_config::{Sticker, StickerConfig}, sticker_config::{Sticker, StickerConfig}, utils::convert_to_png,
}; };
fn convert_to_png(path: PathBuf) -> Vec<u8> {
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( pub async fn deploy_mastodon(
deploy_id: String, deploy_id: String,
sticker_config: StickerConfig, sticker_config: StickerConfig,

View file

@ -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( pub async fn deploy_zip(
deploy_id: String, deploy_id: String,
@ -9,6 +17,8 @@ pub async fn deploy_zip(
) { ) {
let deploy_where = sticker_config.deploy_where.get(&deploy_id).unwrap(); 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 let pack_contents = sticker_config
.sticker_sets .sticker_sets
.get(&deploy_where.pack_id) .get(&deploy_where.pack_id)
@ -32,11 +42,28 @@ pub async fn deploy_zip(
let mut buffer = Vec::new(); let mut buffer = Vec::new();
for (name, sticker) in pack_stickers.into_iter() { for (name, sticker) in pack_stickers.into_iter() {
let sticker_path = PathBuf::from(&base_stickerdb_path).join(sticker.file); 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(); let sticker_extension = match should_convert_png {
zip.start_file(format!("{}.{}", name, sticker_extension), Default::default()).unwrap(); false => (&sticker_path)
sticker_file.read_to_end(&mut buffer).unwrap(); .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(); zip.write_all(&buffer).unwrap();
buffer.clear(); buffer.clear();
} }

View file

@ -9,6 +9,7 @@ pub mod deploy_zip;
pub mod mastodon_api; pub mod mastodon_api;
pub mod sticker_config; pub mod sticker_config;
pub mod tg_api; pub mod tg_api;
pub mod utils;
use clap::Parser; use clap::Parser;

View file

@ -16,6 +16,7 @@ pub struct DeployWhere {
pub discord: Option<Vec<DiscordDeployLocation>>, pub discord: Option<Vec<DiscordDeployLocation>>,
pub telegram: Option<TelegramDeployLocation>, pub telegram: Option<TelegramDeployLocation>,
pub mastodon: Option<MastodonDeployLocation>, pub mastodon: Option<MastodonDeployLocation>,
pub zip: Option<ZipDeployLocation>,
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
@ -43,6 +44,11 @@ fn mastodon_credential_name_default() -> String {
"mastodon".to_string() "mastodon".to_string()
} }
#[derive(Debug, Clone, Deserialize)]
pub struct ZipDeployLocation {
pub convert_png: bool,
}
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct DiscordDeployLocation { pub struct DiscordDeployLocation {
pub deploy_name: String, pub deploy_name: String,

24
src/utils.rs Normal file
View file

@ -0,0 +1,24 @@
use std::{path::PathBuf, process::Command};
pub fn convert_to_png(path: PathBuf) -> Vec<u8> {
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
}