mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
simplify check_mime_type
This commit is contained in:
parent
e78fee0d48
commit
8102260da4
81
src/check.rs
81
src/check.rs
@ -23,51 +23,50 @@ use crate::{
|
||||
/// 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(()))
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user