fix: Strip more file names in extractor

Closes: #698

I ended up fixing a few Clippy lints while tracking down this problem.
It turns out that the issue was way simpler than I assumed: ".tar.xz"
needed to be added to a slice of extensions to strip.
This commit is contained in:
Josh Megnauth 2025-01-02 23:57:39 -05:00 committed by Jeremy Soller
parent 87fa4ac478
commit 0f5163acc6
2 changed files with 26 additions and 18 deletions

View file

@ -2053,14 +2053,15 @@ impl Application for App {
}
Message::ExtractHere(entity_opt) => {
let paths = self.selected_paths(entity_opt);
if let Some(current_path) = paths.get(0) {
if let Some(destination) = current_path.parent().zip(current_path.file_stem()) {
let destination_path = destination.0.to_path_buf();
self.operation(Operation::Extract {
paths,
to: destination_path,
});
}
if let Some(destination) = paths
.first()
.and_then(|first| first.parent())
.map(|parent| parent.to_path_buf())
{
self.operation(Operation::Extract {
paths,
to: destination,
});
}
}
Message::Key(modifiers, key) => {

View file

@ -67,11 +67,20 @@ fn handle_replace(
}
fn get_directory_name(file_name: &str) -> &str {
const SUPPORTED_EXTENSIONS: [&str; 4] = [".tar.gz", ".tgz", ".tar", ".zip"];
// TODO: Chain with COMPOUND_EXTENSIONS once more formats are supported
const SUPPORTED_EXTENSIONS: &[&str] = &[
".tar.bz2",
".tar.gz",
".tar.lzma",
".tar.xz",
".tgz",
".tar",
".zip",
];
for ext in &SUPPORTED_EXTENSIONS {
if file_name.ends_with(ext) {
return &file_name[..file_name.len() - ext.len()];
for ext in SUPPORTED_EXTENSIONS {
if let Some(stripped) = file_name.strip_suffix(ext) {
return stripped;
}
}
file_name
@ -245,7 +254,7 @@ async fn copy_or_move(
if matches!(from.parent(), Some(parent) if parent == to) && !moving {
// `from`'s parent is equal to `to` which means we're copying to the same
// directory (duplicating files)
let to = copy_unique_path(&from, &to);
let to = copy_unique_path(&from, to);
Some((from, to))
} else if let Some(name) = from.file_name() {
let to = to.join(name);
@ -361,12 +370,12 @@ fn copy_unique_path(from: &Path, to: &Path) -> PathBuf {
to
}
fn file_name<'a>(path: &'a Path) -> Cow<'a, str> {
fn file_name(path: &Path) -> Cow<'_, str> {
path.file_name()
.map_or_else(|| fl!("unknown-folder").into(), |x| x.to_string_lossy())
}
fn parent_name<'a>(path: &'a Path) -> Cow<'a, str> {
fn parent_name(path: &Path) -> Cow<'_, str> {
let Some(parent) = path.parent() else {
return fl!("unknown-folder").into();
};
@ -374,7 +383,7 @@ fn parent_name<'a>(path: &'a Path) -> Cow<'a, str> {
file_name(parent)
}
fn paths_parent_name<'a>(paths: &'a Vec<PathBuf>) -> Cow<'a, str> {
fn paths_parent_name(paths: &[PathBuf]) -> Cow<'_, str> {
let Some(first_path) = paths.first() else {
return fl!("unknown-folder").into();
};
@ -777,8 +786,6 @@ impl Operation {
controller.set_progress((i as f32) / total_paths as f32);
let to = to.to_owned();
if let Some(file_name) = path.file_name().and_then(|f| f.to_str()) {
let dir_name = get_directory_name(file_name);
let mut new_dir = to.join(dir_name);