fix: invalid filenames causing panic when moving (dir separators)
This commit is contained in:
+17
@@ -1,4 +1,5 @@
|
|||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
//use ini::Ini;
|
||||||
use lofty::file::TaggedFileExt;
|
use lofty::file::TaggedFileExt;
|
||||||
use lofty::probe::Probe;
|
use lofty::probe::Probe;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
@@ -34,6 +35,22 @@ enum Commands {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
//let mut conf = Ini::new();
|
||||||
|
//conf.with_section(Some("General"))
|
||||||
|
// .set("target_directory", "");
|
||||||
|
////conf.write_to_file("conf.ini").unwrap();
|
||||||
|
//
|
||||||
|
//let conf = Ini::load_from_file("conf.ini").unwrap();
|
||||||
|
//
|
||||||
|
//let section = conf.section(Some("General")).unwrap();
|
||||||
|
//println!("target_dir={:?}", section.get("target_directory"));
|
||||||
|
//
|
||||||
|
//for (_sec, prop) in &conf {
|
||||||
|
// for (key, value) in prop.iter() {
|
||||||
|
// println!("{:?}:{:?}", key, value);
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let extra_files = ["jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt"].map(str::to_string);
|
let extra_files = ["jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt"].map(str::to_string);
|
||||||
|
|
||||||
|
|||||||
+20
-10
@@ -36,20 +36,21 @@ pub fn move_files(
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
let dir_to_create = target_dir.join(artist_dir).join(album_dir.as_ref());
|
let artist_dir = fix_filename(artist_dir);
|
||||||
create_dir_all(&dir_to_create)?;
|
let album_dir = fix_filename(&album_dir);
|
||||||
let target = target_dir.join(artist_dir).join(album_dir.as_ref());
|
let target = target_dir.join(artist_dir).join(album_dir);
|
||||||
|
create_dir_all(&target)?;
|
||||||
|
|
||||||
let file_name = if let Some(track_number) = track_number {
|
let file_name = if let Some(track_number) = track_number {
|
||||||
let number: u32 = track_number.parse()?;
|
let number: u32 = track_number.parse()?;
|
||||||
PathBuf::from(format!(
|
let artist = fix_filename(&artist.unwrap());
|
||||||
"{:02}. {} - {}.flac",
|
let title = fix_filename(&title.unwrap());
|
||||||
number,
|
|
||||||
artist.unwrap(),
|
PathBuf::from(format!("{:02}. {} - {}.flac", number, artist, title,))
|
||||||
title.unwrap(),
|
|
||||||
))
|
|
||||||
} else {
|
} else {
|
||||||
PathBuf::from(format!("01. {} - {}.flac", artist.unwrap(), title.unwrap(),))
|
let artist = fix_filename(&artist.unwrap());
|
||||||
|
let title = fix_filename(&title.unwrap());
|
||||||
|
PathBuf::from(format!("01. {} - {}.flac", artist, title))
|
||||||
};
|
};
|
||||||
|
|
||||||
if should_copy {
|
if should_copy {
|
||||||
@@ -86,6 +87,15 @@ pub fn move_files(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn fix_filename(name: &str) -> String {
|
||||||
|
name.chars()
|
||||||
|
.map(|c| match c {
|
||||||
|
'/' => '_',
|
||||||
|
c => c,
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
fn write_log(file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
|
fn write_log(file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let home_dir: PathBuf = PathBuf::from(var("HOME").expect("HOME is not set"));
|
let home_dir: PathBuf = PathBuf::from(var("HOME").expect("HOME is not set"));
|
||||||
let data_dir = home_dir.join(".local").join("share").join("music-sort");
|
let data_dir = home_dir.join(".local").join("share").join("music-sort");
|
||||||
|
|||||||
Reference in New Issue
Block a user