Skip to content

Commit

Permalink
Merge pull request #212 from axodotdev/axoasset-compression-levels
Browse files Browse the repository at this point in the history
Allow overriding compression levels with environment variables
  • Loading branch information
fasterthanlime authored Oct 30, 2024
2 parents c30b09a + fab2257 commit b41e0ac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ xz2 = { version = "0.1.7", optional = true, features = ["static"] }
zstd = { version = "0.13.0", optional = true }
toml_edit = { version = "0.22.5", optional = true }
walkdir = "2.5.0"
lazy_static = "1.5.0"

[dev-dependencies]
assert_fs = "1"
Expand Down
38 changes: 30 additions & 8 deletions src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ pub(crate) enum CompressionImpl {
Zstd,
}

lazy_static::lazy_static! {
static ref DEFAULT_GZ_LEVEL: u32 = {
std::env::var("AXOASSET_GZ_LEVEL")
.ok()
.and_then(|val| val.parse().ok())
.unwrap_or(6)
};
static ref DEFAULT_XZ_LEVEL: u32 = {
std::env::var("AXOASSET_XZ_LEVEL")
.ok()
.and_then(|val| val.parse().ok())
.unwrap_or(9)
};
static ref DEFAULT_ZSTD_LEVEL: i32 = {
std::env::var("AXOASSET_ZSTD_LEVEL")
.ok()
.and_then(|val| val.parse().ok())
.unwrap_or(3)
};
}

#[cfg(feature = "compression-tar")]
pub(crate) fn tar_dir(
src_path: &Utf8Path,
Expand Down Expand Up @@ -52,7 +73,7 @@ pub(crate) fn tar_dir(
// Wrap our file in compression
let zip_output = GzBuilder::new()
.filename(zip_contents_name)
.write(final_zip_file, Compression::default());
.write(final_zip_file, Compression::new(*DEFAULT_GZ_LEVEL));

// Write the tar to the compression stream
let mut tar = tar::Builder::new(zip_output);
Expand Down Expand Up @@ -87,7 +108,7 @@ pub(crate) fn tar_dir(
// Drop the file to close it
}
CompressionImpl::Xzip => {
let zip_output = XzEncoder::new(final_zip_file, 9);
let zip_output = XzEncoder::new(final_zip_file, *DEFAULT_XZ_LEVEL);
// Write the tar to the compression stream
let mut tar = tar::Builder::new(zip_output);

Expand Down Expand Up @@ -122,12 +143,13 @@ pub(crate) fn tar_dir(
}
CompressionImpl::Zstd => {
// Wrap our file in compression
let zip_output = ZstdEncoder::new(final_zip_file, 0).map_err(|details| {
AxoassetError::Compression {
reason: "failed to create zstd encoder".to_string(),
details,
}
})?;
let zip_output =
ZstdEncoder::new(final_zip_file, *DEFAULT_ZSTD_LEVEL).map_err(|details| {
AxoassetError::Compression {
reason: "failed to create zstd encoder".to_string(),
details,
}
})?;

// Write the tar to the compression stream
let mut tar = tar::Builder::new(zip_output);
Expand Down

0 comments on commit b41e0ac

Please sign in to comment.