refactor: prepare for automatic compression of embedded files in krilla

This commit is contained in:
Tobias Schmitz 2025-05-10 14:28:34 +02:00
parent 0cef699f3e
commit cd659bf460
No known key found for this signature in database

View File

@ -34,7 +34,8 @@ pub(crate) fn embed_files(
}, },
}; };
let data: Arc<dyn AsRef<[u8]> + Send + Sync> = Arc::new(embed.data.clone()); let data: Arc<dyn AsRef<[u8]> + Send + Sync> = Arc::new(embed.data.clone());
let compress = should_compress(&embed.data); // TODO: update when new krilla version lands (https://github.com/LaurenzV/krilla/pull/203)
let compress = should_compress(&embed.data).unwrap_or(true);
let file = EmbeddedFile { let file = EmbeddedFile {
path, path,
@ -54,12 +55,10 @@ pub(crate) fn embed_files(
Ok(()) Ok(())
} }
fn should_compress(data: &[u8]) -> bool { fn should_compress(data: &[u8]) -> Option<bool> {
let Some(ty) = infer::get(data) else { let ty = infer::get(data)?;
return true;
};
match ty.matcher_type() { match ty.matcher_type() {
infer::MatcherType::App => true, infer::MatcherType::App => None,
infer::MatcherType::Archive => match ty.mime_type() { infer::MatcherType::Archive => match ty.mime_type() {
#[rustfmt::skip] #[rustfmt::skip]
"application/zip" "application/zip"
@ -76,8 +75,8 @@ fn should_compress(data: &[u8]) -> bool {
| "application/x-rpm" | "application/x-rpm"
| "application/zstd" | "application/zstd"
| "application/x-lz4" | "application/x-lz4"
| "application/x-ole-storage" => false, | "application/x-ole-storage" => Some(false),
_ => true, _ => None,
}, },
infer::MatcherType::Audio => match ty.mime_type() { infer::MatcherType::Audio => match ty.mime_type() {
#[rustfmt::skip] #[rustfmt::skip]
@ -88,12 +87,12 @@ fn should_compress(data: &[u8]) -> bool {
| "audio/x-flac" | "audio/x-flac"
| "audio/amr" | "audio/amr"
| "audio/aac" | "audio/aac"
| "audio/x-ape" => false, | "audio/x-ape" => Some(false),
_ => true, _ => None,
}, },
infer::MatcherType::Book => true, infer::MatcherType::Book => None,
infer::MatcherType::Doc => true, infer::MatcherType::Doc => None,
infer::MatcherType::Font => true, infer::MatcherType::Font => None,
infer::MatcherType::Image => match ty.mime_type() { infer::MatcherType::Image => match ty.mime_type() {
#[rustfmt::skip] #[rustfmt::skip]
"image/jpeg" "image/jpeg"
@ -104,10 +103,10 @@ fn should_compress(data: &[u8]) -> bool {
| "image/heif" | "image/heif"
| "image/avif" | "image/avif"
| "image/jxl" | "image/jxl"
| "image/vnd.djvu" => false, | "image/vnd.djvu" => None,
_ => true, _ => None,
}, },
infer::MatcherType::Text => true, infer::MatcherType::Text => None,
infer::MatcherType::Video => match ty.mime_type() { infer::MatcherType::Video => match ty.mime_type() {
#[rustfmt::skip] #[rustfmt::skip]
"video/mp4" "video/mp4"
@ -115,9 +114,9 @@ fn should_compress(data: &[u8]) -> bool {
| "video/x-matroska" | "video/x-matroska"
| "video/webm" | "video/webm"
| "video/quicktime" | "video/quicktime"
| "video/x-flv" => false, | "video/x-flv" => Some(false),
_ => true, _ => None,
}, },
infer::MatcherType::Custom => true, infer::MatcherType::Custom => None,
} }
} }