feat: add an identifier before the extension to prevent file conflicts rather than after (#316)

This commit is contained in:
Rick Yao 2023-10-27 14:51:16 +08:00 committed by GitHub
parent 2b731748e4
commit aceec545ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
@ -44,15 +44,31 @@ pub fn expand_url(mut u: 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;
};
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;
while fs::symlink_metadata(&p).await.is_ok() {
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}"));
if !ext.is_empty() {
name.push(&ext);
}
p.set_file_name(name);
}
p