decompress: match mime type correctly

pull/11/head
phiresky 5 years ago
parent 21f5178d15
commit 8400a4a2bb

Binary file not shown.

@ -45,22 +45,33 @@ impl GetMetadata for DecompressAdapter {
}
}
fn decompress_any<'a, R>(filename: &Path, inp: &'a mut R) -> Fallible<Box<dyn Read + 'a>>
fn decompress_any<'a, R>(reason: &SlowMatcher, inp: &'a mut R) -> Fallible<Box<dyn Read + 'a>>
where
R: Read,
{
let extension = filename.extension().map(|e| e.to_string_lossy().to_owned());
use FastMatcher::*;
use SlowMatcher::*;
let gz = |inp: &'a mut R| Box::new(flate2::read::MultiGzDecoder::new(inp));
let bz2 = |inp: &'a mut R| Box::new(bzip2::read::BzDecoder::new(inp));
let xz = |inp: &'a mut R| Box::new(xz2::read::XzDecoder::new_multi_decoder(inp));
let zst = |inp: &'a mut R| zstd::stream::read::Decoder::new(inp); // returns result
match extension {
Some(e) => Ok(match e.to_owned().as_ref() {
"tgz" | "gz" => Box::new(flate2::read::MultiGzDecoder::new(inp)),
"tbz" | "tbz2" | "bz2" => Box::new(bzip2::read::BzDecoder::new(inp)),
"xz" => Box::new(xz2::read::XzDecoder::new_multi_decoder(inp)),
"zst" => Box::new(zstd::stream::read::Decoder::new(inp)?),
Ok(match reason {
Fast(FileExtension(ext)) => match ext.as_ref() {
"tgz" | "gz" => gz(inp),
"tbz" | "tbz2" | "bz2" => bz2(inp),
"xz" => xz(inp),
"zst" => Box::new(zst(inp)?),
ext => Err(format_err!("don't know how to decompress {}", ext))?,
}),
None => Err(format_err!("no extension")),
}
},
MimeType(mime) => match mime.as_ref() {
"application/gzip" => gz(inp),
"application/x-bzip" => bz2(inp),
"application/x-xz" => xz(inp),
"application/zstd" => Box::new(zst(inp)?),
mime => Err(format_err!("don't know how to decompress mime {}", mime))?,
},
})
}
fn get_inner_filename(filename: &Path) -> PathBuf {
let extension = filename
@ -90,7 +101,7 @@ impl FileAdapter for DecompressAdapter {
..
} = ai;
let mut decompress = decompress_any(filepath_hint, &mut inp)?;
let mut decompress = decompress_any(detection_reason, &mut inp)?;
let ai2: AdaptInfo = AdaptInfo {
filepath_hint: &get_inner_filename(filepath_hint),
is_real_file: false,

@ -59,10 +59,14 @@ pub fn adapter_matcher<T: AsRef<str>>(
use SlowMatcher::*;
for matcher in metadata.get_matchers(slow) {
match matcher.as_ref() {
f @ MimeType(re) => mime_regexes.push((re.clone(), adapter.clone(), f)),
f @ Fast(FastMatcher::FileExtension(re)) => {
fname_regexes.push((extension_to_regex(re), adapter.clone(), f))
MimeType(re) => {
mime_regexes.push((re.clone(), adapter.clone(), MimeType(re.clone())))
}
Fast(FastMatcher::FileExtension(re)) => fname_regexes.push((
extension_to_regex(re),
adapter.clone(),
Fast(FastMatcher::FileExtension(re.clone())),
)),
};
}
}
@ -112,11 +116,11 @@ pub fn adapter_matcher<T: AsRef<str>>(
if fname_matches.is_empty() {
None
} else {
let (_, adapter, matcher) = fname_regexes[fname_matches[0]];
let (_, adapter, matcher) = &fname_regexes[fname_matches[0]];
Some((adapter.clone(), matcher.clone()))
}
} else {
let (_, adapter, matcher) = mime_regexes[mime_matches[0]];
let (_, adapter, matcher) = &mime_regexes[mime_matches[0]];
Some((adapter.clone(), matcher.clone()))
}
})

@ -101,7 +101,7 @@ pub fn rga_preproc(ai: AdaptInfo) -> Result<(), Error> {
archive_recursion_depth,
config: PreprocConfig { cache: None, args },
},
detection_reason,
&detection_reason,
)?;
let compressed = compbuf
.into_inner()
@ -134,7 +134,7 @@ pub fn rga_preproc(ai: AdaptInfo) -> Result<(), Error> {
archive_recursion_depth,
config: PreprocConfig { cache: None, args },
},
detection_reason,
&detection_reason,
)?;
Ok(())
}

Loading…
Cancel
Save