simplify check_mime_type

This commit is contained in:
João M. Bezerra 2023-02-06 20:56:46 -03:00
parent e78fee0d48
commit 8102260da4
2 changed files with 53 additions and 52 deletions

View File

@ -23,51 +23,50 @@ use crate::{
/// TODO: maybe the name of this should be "magic numbers" or "file signature", /// TODO: maybe the name of this should be "magic numbers" or "file signature",
/// and not MIME. /// and not MIME.
pub fn check_mime_type( pub fn check_mime_type(
files: &[PathBuf], path: &Path,
formats: &mut [Vec<Extension>], formats: &mut Vec<Extension>,
question_policy: QuestionPolicy, question_policy: QuestionPolicy,
) -> Result<ControlFlow<()>> { ) -> Result<ControlFlow<()>> {
for (path, format) in files.iter().zip(formats.iter_mut()) { if formats.is_empty() {
if format.is_empty() { // File with no extension
// File with no extension // Try to detect it automatically and prompt the user about it
// Try to detect it automatically and prompt the user about it if let Some(detected_format) = try_infer_extension(path) {
if let Some(detected_format) = try_infer_extension(path) { // Inferring the file extension can have unpredicted consequences (e.g. the user just
// Inferring the file extension can have unpredicted consequences (e.g. the user just // mistyped, ...) which we should always inform the user about.
// mistyped, ...) which we should always inform the user about. info!(
info!( accessible,
accessible, "Detected file: `{}` extension as `{}`",
"Detected file: `{}` extension as `{}`", path.display(),
path.display(), detected_format
detected_format );
); if user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? {
if user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? { formats.push(detected_format);
format.push(detected_format); } else {
} else { return Ok(ControlFlow::Break(()));
return Ok(ControlFlow::Break(()));
}
} }
} else if let Some(detected_format) = try_infer_extension(path) {
// File ending with extension
// Try to detect the extension and warn the user if it differs from the written one
let outer_ext = format.iter().next_back().unwrap();
if !outer_ext
.compression_formats
.ends_with(detected_format.compression_formats)
{
warning!(
"The file extension: `{}` differ from the detected extension: `{}`",
outer_ext,
detected_format
);
if !user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? {
return Ok(ControlFlow::Break(()));
}
}
} else {
// NOTE: If this actually produces no false positives, we can upgrade it in the future
// to a warning and ask the user if he wants to continue decompressing.
info!(accessible, "Could not detect the extension of `{}`", path.display());
} }
} else if let Some(detected_format) = try_infer_extension(path) {
// File ending with extension
// Try to detect the extension and warn the user if it differs from the written one
let outer_ext = formats.iter().next_back().unwrap();
if !outer_ext
.compression_formats
.ends_with(detected_format.compression_formats)
{
warning!(
"The file extension: `{}` differ from the detected extension: `{}`",
outer_ext,
detected_format
);
if !user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? {
return Ok(ControlFlow::Break(()));
}
}
} else {
// NOTE: If this actually produces no false positives, we can upgrade it in the future
// to a warning and ask the user if he wants to continue decompressing.
info!(accessible, "Could not detect the extension of `{}`", path.display());
} }
Ok(ControlFlow::Continue(())) Ok(ControlFlow::Continue(()))
} }

View File

@ -125,13 +125,14 @@ pub fn run(
} }
} else { } else {
for path in files.iter() { for path in files.iter() {
let (file_output_path, file_formats) = extension::separate_known_extensions_from_name(path); let (path, mut file_formats) = extension::separate_known_extensions_from_name(path);
output_paths.push(file_output_path);
formats.push(file_formats);
}
if let ControlFlow::Break(_) = check::check_mime_type(&files, &mut formats, question_policy)? { if let ControlFlow::Break(_) = check::check_mime_type(path, &mut file_formats, question_policy)? {
return Ok(()); return Ok(());
}
output_paths.push(path);
formats.push(file_formats);
} }
} }
@ -172,12 +173,13 @@ pub fn run(
} }
} else { } else {
for path in files.iter() { for path in files.iter() {
let file_formats = extension::extensions_from_path(path); let mut file_formats = extension::extensions_from_path(path);
formats.push(file_formats);
}
if let ControlFlow::Break(_) = check::check_mime_type(&files, &mut formats, question_policy)? { if let ControlFlow::Break(_) = check::check_mime_type(path, &mut file_formats, question_policy)? {
return Ok(()); return Ok(());
}
formats.push(file_formats);
} }
} }