feat: add new -n flag to write imported file path to m3u file

This commit is contained in:
2026-10-28 16:42:44 +01:00
parent 5e3bc38a11
commit b4941442cd
2 changed files with 28 additions and 9 deletions
+23 -7
View File
@@ -5,7 +5,7 @@ use lofty::tag::Accessor;
use lofty::tag::Tag;
use std::env::var;
use std::fs::OpenOptions;
use std::fs::{copy, create_dir, create_dir_all, remove_dir, rename, File};
use std::fs::{copy, create_dir, create_dir_all, remove_dir, rename};
use std::io::Write;
use std::path::PathBuf;
@@ -15,6 +15,7 @@ pub fn move_files(
extra_files: [String; 9],
should_copy: bool,
has_parent: bool,
add_new_playlist: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let target_dir = PathBuf::from("/home/user/Music/Tracks");
@@ -54,9 +55,13 @@ pub fn move_files(
};
if should_copy {
copy(&file_path, target.join(file_name))?;
copy(&file_path, target.join(&file_name))?;
} else {
rename(&file_path, target.join(file_name))?;
rename(&file_path, target.join(&file_name))?;
}
if add_new_playlist {
new_playlist(target.join(file_name))?;
}
for ext in &extra_files {
@@ -98,6 +103,16 @@ fn fix_filename(name: &str) -> String {
.collect()
}
fn new_playlist(path: PathBuf) -> std::result::Result<(), std::io::Error> {
let new_playlist_path = PathBuf::from("/home/user/Music/Playlists/active - new.m3u");
let mut new_playlist = OpenOptions::new()
.append(true)
.open(new_playlist_path)
.unwrap();
println!("{:?}", path);
writeln!(new_playlist, "{}", path.display())
}
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 data_dir = home_dir.join(".local").join("share").join("music-sort");
@@ -106,11 +121,12 @@ fn write_log(file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
if !data_dir.exists() {
create_dir(&data_dir)?;
}
if !log_file_path.exists() {
File::create(&log_file_path)?;
}
let mut log_file = OpenOptions::new().append(true).open(log_file_path).unwrap();
let mut log_file = OpenOptions::new()
.append(true)
.create(true)
.open(log_file_path)
.unwrap();
if let Err(e) = writeln!(
log_file,