Skip to content

Commit

Permalink
Added compression level + fixed zipper
Browse files Browse the repository at this point in the history
  • Loading branch information
buj committed Aug 24, 2024
1 parent 86f4add commit 0a1fa7f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "discord-backup-util"
version = "0.1.0"
version = "0.1.1"
authors = ["buj"]
license = "AGPL3-or-later"
description = "A tiny tool to backup stuff to Discord"
Expand Down
19 changes: 19 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Config {
pub shell: Vec<String>,
pub delay: Duration,
pub password: Option<String>,
pub compression_level: i64,
}

struct TimeColumn {
Expand Down Expand Up @@ -91,6 +92,7 @@ pub fn parse_args() -> Config {
let mut webhook = None;
let mut delay = None;
let mut password = None;
let mut compression = None;

while let Some(x) = lines.peek() {
let x = x.trim();
Expand All @@ -116,6 +118,22 @@ pub fn parse_args() -> Config {
continue;
}

if x.starts_with("compression ") {
if let Ok(value) = x.split_once(' ').unwrap().1.parse::<i64>() {
if compression
.replace(value)
.is_some()
{
eprintln!("{exe}: cannot set multiple compression levels");
exit(-1);
}
continue;
} else {
eprintln!("{exe}: invalid compression value");
exit(-1);
}
}

if x.starts_with("webhook ") {
if webhook
.replace(Webhook::new(x.split_once(' ').unwrap().1.to_string()))
Expand Down Expand Up @@ -219,6 +237,7 @@ pub fn parse_args() -> Config {
exit(-1);
}
},
compression_level: compression.unwrap_or(10),
shell,
script,
password,
Expand Down
12 changes: 1 addition & 11 deletions src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ use serde::Deserialize;
#[derive(Default)]
pub struct MessageBuilder(Message);
impl MessageBuilder {
pub fn reply(mut self, to: NonZeroU64) -> Self {
self.0.reply.replace(to);
self
}
pub fn reply_maybe(mut self, to: Option<NonZeroU64>) -> Self {
self.0.reply = to;
self
}

pub fn content(mut self, content: impl Into<String>) -> Self {
self.0.content.replace(content.into());
self
Expand All @@ -33,7 +24,6 @@ impl MessageBuilder {
#[derive(Default)]
pub struct Message {
pub id: Option<NonZeroU64>,
pub reply: Option<NonZeroU64>,
pub content: Option<String>,
pub files: Vec<(String, Vec<u8>)>,
}
Expand Down Expand Up @@ -65,7 +55,7 @@ impl Message {
hook: &Webhook,
message: impl Fn(MessageBuilder) -> MessageBuilder,
) -> Message {
hook.send(|x| message(x.reply(self.id.expect("Replying to a message that was never sent"))))
hook.send(message)
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,10 @@ fn main() {
}
}
}

println!("Added file {}", format!("{name}/{}", x.file_name().into_string().unwrap())
.trim_start_matches('/'));
} else {
if let Err(why) = zip.add_directory(
name.clone(),
SimpleFileOptions::default().compression_level(Some(10)),
) {
println!("Failed to add directory: {why}");
return;
}
walk(
x.path(),
format!("{name}/{}", x.file_name().into_string().unwrap())
Expand All @@ -212,7 +208,7 @@ fn main() {
}
walk(dir.clone(), String::new(), &mut zip, {
let options = SimpleFileOptions::default()
.compression_level(Some(10))
.compression_level(Some(config.compression_level))
.compression_method(zip::CompressionMethod::Deflated);

if let Some(x) = &config.password {
Expand Down
2 changes: 1 addition & 1 deletion src/temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ pub fn temp_path() -> PathBuf {
.take(32)
.collect();

PathBuf::from_str(&format!("/tmp/{name}")).unwrap()
PathBuf::from_str(&format!("/tmp/discord-backup-util.{name}")).unwrap()
}

0 comments on commit 0a1fa7f

Please sign in to comment.