mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: add an identifier before the extension to prevent file conflicts rather than after (#316)
This commit is contained in:
parent
2b731748e4
commit
aceec545ae
1 changed files with 19 additions and 3 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{borrow::Cow, env, path::{Component, Path, PathBuf}};
|
use std::{borrow::Cow, env, ffi::OsString, path::{Component, Path, PathBuf}};
|
||||||
|
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
|
|
||||||
|
|
@ -44,15 +44,31 @@ pub fn expand_url(mut u: Url) -> Url {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn unique_path(mut p: Url) -> Url {
|
pub async fn unique_path(mut p: Url) -> Url {
|
||||||
let Some(name) = p.file_name().map(|n| n.to_owned()) else {
|
let Some(stem) = p.file_stem().map(|s| s.to_owned()) else {
|
||||||
return p;
|
return p;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let ext = p
|
||||||
|
.extension()
|
||||||
|
.map(|s| {
|
||||||
|
let mut n = OsString::with_capacity(s.len() + 1);
|
||||||
|
n.push(".");
|
||||||
|
n.push(s);
|
||||||
|
n
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while fs::symlink_metadata(&p).await.is_ok() {
|
while fs::symlink_metadata(&p).await.is_ok() {
|
||||||
i += 1;
|
i += 1;
|
||||||
let mut name = name.clone();
|
|
||||||
|
let mut name = OsString::with_capacity(stem.len() + ext.len() + 5);
|
||||||
|
name.push(&stem);
|
||||||
name.push(format!("_{i}"));
|
name.push(format!("_{i}"));
|
||||||
|
if !ext.is_empty() {
|
||||||
|
name.push(&ext);
|
||||||
|
}
|
||||||
|
|
||||||
p.set_file_name(name);
|
p.set_file_name(name);
|
||||||
}
|
}
|
||||||
p
|
p
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue