Skip to content

Commit

Permalink
Merge pull request #3 from 5GameMaker/master
Browse files Browse the repository at this point in the history
Windows support
  • Loading branch information
5GameMaker authored Aug 25, 2024
2 parents dbb5393 + b6c1e5c commit 5dcbed7
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 61 deletions.
6 changes: 6 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[target.x86_64-pc-windows-msvc]
linker = "lld-link"

[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=/usr/bin/mold"]
5 changes: 4 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ env:

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: awalsh128/cache-apt-pkgs-action@v1
with:
packages: mold
version: 1.0
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
Expand Down
17 changes: 16 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ jobs:
- uses: ningenMe/[email protected]
- uses: awalsh128/cache-apt-pkgs-action@v1
with:
packages: musl-tools gcc-multilib
packages: musl-tools mold gcc-mingw-w64
version: 1.0
- run: cargo install cargo-xwin --locked

- name: Create release dir
run: rm -fr target/github-release && mkdir -p target/github-release
Expand Down Expand Up @@ -52,6 +53,20 @@ jobs:
run: cargo build --verbose --release --target i686-unknown-linux-gnu
- name: Copy i686-unknown-linux-gnu artifact
run: cp target/i686-unknown-linux-gnu/release/discord-backup-util target/github-release/discord-backup-util.i686-unknown-linux-gnu

- name: Install x86_64-pc-windows-gnu toolchain
run: rustup target add x86_64-pc-windows-gnu
- name: Build for x86_64-pc-windows-gnu
run: cargo build --verbose --release --target x86_64-pc-windows-gnu
- name: Copy x86_64-pc-windows-gnu artifact
run: cp target/x86_64-pc-windows-gnu/release/discord-backup-util.exe target/github-release/discord-backup-util.x86_64-pc-windows-gnu.exe

- name: Install x86_64-pc-windows-msvc toolchain
run: rustup target add x86_64-pc-windows-msvc
- name: Build for x86_64-pc-windows-msvc
run: cargo xwin build --verbose --release --target x86_64-pc-windows-msvc
- name: Copy x86_64-pc-windows-msvc artifact
run: cp target/x86_64-pc-windows-msvc/release/discord-backup-util.exe target/github-release/discord-backup-util.x86_64-pc-windows-msvc.exe

- uses: colathro/[email protected]
id: crate-version
Expand Down
55 changes: 8 additions & 47 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "discord-backup-util"
version = "0.2.0"
version = "0.2.1"
authors = ["buj"]
license = "AGPL3-or-later"
description = "A tiny tool to backup stuff to Discord"
Expand All @@ -19,7 +19,6 @@ ureq = ["dep:ureq"]
[dependencies]
minreq = { version = "2.12.0", features = ["https-bundled-probe"], optional = true }
rand = "0.8.5"
serde = { version = "1.0.208", features = ["derive"] }
serde_json = "1.0.125"
tinyjson = "2.5.1"
ureq = { version = "2.10.1", optional = true }
zip = { version = "2.2.0", features = ["aes", "aes-crypto", "deflate", "deflate-zlib", "deflate64"], default-features = false }
40 changes: 32 additions & 8 deletions src/hook.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{num::NonZeroU64, ops::Add, time::Duration};

use rand::Rng;
use serde::Deserialize;
use tinyjson::JsonValue;

use crate::log::Logger;

Expand Down Expand Up @@ -75,7 +75,6 @@ impl Message {
}
}

#[derive(Deserialize)]
struct ApiMessage {
id: String,
}
Expand Down Expand Up @@ -174,19 +173,44 @@ impl Webhook {
}
} {
Ok(x) => {
let parsed: ApiMessage = match serde_json::from_reader({
let parsed: ApiMessage = match {
#[cfg(feature = "ureq")]
{
x.into_reader()
x.into_string()
}
#[cfg(feature = "minreq")]
{
std::io::Cursor::new(x.as_bytes())
x.as_str()
}
}) {
Ok(x) => x,
} {
Ok(x) => match x.parse::<JsonValue>() {
Ok(x) => match x {
JsonValue::Object(x) => match x.get("id") {
Some(JsonValue::String(x)) => ApiMessage { id: x.to_owned() },
x => {
logger.error(&format!("Received invalid json, retrying in 5 minutes\n\nExpected string, found {x:?}"));
std::thread::sleep(Duration::from_secs(300));
continue;
}
},
x => {
logger.error(&format!("Received invalid json, retrying in 5 minutes\n\nExpected object, found {x:?}"));
std::thread::sleep(Duration::from_secs(300));
continue;
}
},
Err(why) => {
logger.error(&format!(
"Failed to parse json, retrying in 5 minutes...\n\n{why}"
));
std::thread::sleep(Duration::from_secs(300));
continue;
}
},
Err(why) => {
println!("Failed to parse message, retrying in 5 minutes...\n\n{why}");
logger.error(&format!(
"Failed to parse message, retrying in 5 minutes...\n\n{why}"
));
std::thread::sleep(Duration::from_secs(300));
continue;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use upload::upload;

#[cfg(not(any(feature = "ureq", feature = "minreq")))]
compile_error!("Either 'ureq' or 'minreq' feature must be enabled");
#[cfg(all(feature = "ureq", feature = "minreq"))]
compile_error!("Cannot enable both 'ureq' and 'minreq' features");

mod config;
mod hook;
Expand Down
23 changes: 22 additions & 1 deletion src/temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,33 @@ use std::{path::PathBuf, str::FromStr};

use rand::Rng;

#[cfg(windows)]
pub fn temp_path() -> PathBuf {
let Ok(root) = std::env::var("TEMP") else {
panic!("'TEMP' is not set");
};

let name: String = rand::thread_rng()
.sample_iter(rand::distributions::Alphanumeric)
.map(|x| x as char)
.take(32)
.collect();

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

#[cfg(unix)]
pub fn temp_path() -> PathBuf {
let temp = match std::env::var("TMPDIR") {
Ok(x) => x,
Err(_) => "/var/tmp".to_string(),
};

let name: String = rand::thread_rng()
.sample_iter(rand::distributions::Alphanumeric)
.map(|x| x as char)
.take(32)
.collect();

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

0 comments on commit 5dcbed7

Please sign in to comment.