Merge pull request #362 from ouch-org/simplify-check_mime_type

simplify `check_mime_type`
This commit is contained in:
figsoda 2023-02-06 21:51:03 -05:00 committed by GitHub
commit 557f42af92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 53 deletions

View File

@ -16,58 +16,57 @@ use crate::{
warning, QuestionAction, QuestionPolicy, Result,
};
/// Check, for each file, if the mime type matches the detected extensions.
/// Check if the mime type matches the detected extensions.
///
/// In case the file doesn't has any extensions, try to infer the format.
///
/// TODO: maybe the name of this should be "magic numbers" or "file signature",
/// and not MIME.
pub fn check_mime_type(
files: &[PathBuf],
formats: &mut [Vec<Extension>],
path: &Path,
formats: &mut Vec<Extension>,
question_policy: QuestionPolicy,
) -> Result<ControlFlow<()>> {
for (path, format) in files.iter().zip(formats.iter_mut()) {
if format.is_empty() {
// File with no extension
// Try to detect it automatically and prompt the user about it
if let Some(detected_format) = try_infer_extension(path) {
// Inferring the file extension can have unpredicted consequences (e.g. the user just
// mistyped, ...) which we should always inform the user about.
info!(
accessible,
"Detected file: `{}` extension as `{}`",
path.display(),
detected_format
);
if user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? {
format.push(detected_format);
} else {
return Ok(ControlFlow::Break(()));
}
if formats.is_empty() {
// File with no extension
// Try to detect it automatically and prompt the user about it
if let Some(detected_format) = try_infer_extension(path) {
// Inferring the file extension can have unpredicted consequences (e.g. the user just
// mistyped, ...) which we should always inform the user about.
info!(
accessible,
"Detected file: `{}` extension as `{}`",
path.display(),
detected_format
);
if user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? {
formats.push(detected_format);
} else {
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(()))
}

View File

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