use crate::{min::{Minifier, only_recompress}, errors::FileIgnoreError};
pub(crate) const REPACKED: &str = "_repack";
pub enum FileType {
Other,
Original,
Repacked
}
impl FileType {
#[must_use]
pub fn by_name(s: &str) -> Self {
match s.rsplit_once('.') {
Some((n, "jar" | "zip")) => if n.ends_with(REPACKED) { Self::Repacked } else { Self::Original }
_ => Self::Other
}
}
}
pub enum FileOp {
Recompress(u32),
Minify(Minifier),
Ignore(FileIgnoreError),
}
impl FileOp {
pub(crate) fn by_name(fname: &str, use_blacklist: bool) -> Self {
if fname.starts_with(".cache/") { return Self::Ignore(FileIgnoreError::Blacklisted) }
if let Some(sub) = fname.strip_prefix("META-INF/") {
match sub {
"MANIFEST.MF" => {return Self::Recompress(64) }
"SIGNFILE.SF" | "SIGNFILE.DSA" => { return Self::Ignore(FileIgnoreError::Signfile) }
x if x.starts_with("SIG-") || [".DSA", ".RSA", ".SF"].into_iter().any(|e| x.ends_with(e)) => {
return Self::Ignore(FileIgnoreError::Signfile)
}
x if x.starts_with("services/") => { return Self::Recompress(64) }
_ => {}
}
}
let Some((_, ftype)) = fname.rsplit_once('.') else {
return Self::Recompress(2)
};
if ftype == "class" {
return Self::Recompress(64)
}
if only_recompress(ftype) {
return Self::Recompress(4)
}
if let Some(x) = Minifier::by_extension(ftype) {
return Self::Minify(x)
}
if use_blacklist && can_ignore_type(ftype) { Self::Ignore(FileIgnoreError::Blacklisted) } else { Self::Recompress(2) }
}
}
fn can_ignore_type(s: &str) -> bool {
matches!(s, "blend" | "blend1" | "psd")
}