Skip to content

Commit

Permalink
Infer extenion for list command as well
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmaSd committed Nov 3, 2021
1 parent bdee0a7 commit e45d303
Showing 1 changed file with 42 additions and 29 deletions.
71 changes: 42 additions & 29 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use std::{
io::{self, BufReader, BufWriter, Read, Write},
ops::ControlFlow,
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -185,36 +186,9 @@ pub fn run(args: Opts, question_policy: QuestionPolicy) -> crate::Result<()> {
output_paths.push(file_output_path);
formats.push(file_formats);
}
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(path) {
info!("Detected file: `{}` extension as `{}`", path.display(), detected_format);
if user_wants_to_continue_decompressing(path, question_policy)? {
format.push(detected_format);
} else {
return Ok(());
}
}
} else if let Some(detected_format) = try_infer(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().unwrap();
if outer_ext != &detected_format {
info!(
"The file extension: `{}` differ from the detected extension: `{}`",
outer_ext, detected_format
);

if !user_wants_to_continue_decompressing(path, question_policy)? {
return Ok(());
}
}
} else {
// Could not detect the extension automatically
// Should we tell the user about it?
}
if let ControlFlow::Break(_) = check_mime_type(&files, &mut formats, question_policy)? {
return Ok(());
}

let files_missing_format: Vec<PathBuf> = files
Expand Down Expand Up @@ -255,6 +229,10 @@ pub fn run(args: Opts, question_policy: QuestionPolicy) -> crate::Result<()> {
formats.push(file_formats);
}

if let ControlFlow::Break(_) = check_mime_type(&files, &mut formats, question_policy)? {
return Ok(());
}

let not_archives: Vec<PathBuf> = files
.iter()
.zip(&formats)
Expand Down Expand Up @@ -520,3 +498,38 @@ fn list_archive_contents(
list::list_files(archive_path, files, list_options);
Ok(())
}

fn check_mime_type(
files: &[PathBuf],
formats: &mut Vec<Vec<Extension>>,
question_policy: QuestionPolicy,
) -> crate::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(path) {
info!("Detected file: `{}` extension as `{}`", path.display(), detected_format);
if user_wants_to_continue_decompressing(path, question_policy)? {
format.push(detected_format);
} else {
return Ok(ControlFlow::Break(()));
}
}
} else if let Some(detected_format) = try_infer(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().unwrap();
if outer_ext != &detected_format {
info!("The file extension: `{}` differ from the detected extension: `{}`", outer_ext, detected_format);
if !user_wants_to_continue_decompressing(path, question_policy)? {
return Ok(ControlFlow::Break(()));
}
}
} else {
// Could not detect the extension automatically
// Should we tell the user about it?
}
}
Ok(ControlFlow::Continue(()))
}

0 comments on commit e45d303

Please sign in to comment.