From 6fa7ed44af752c69ec6d72e022d29aec2a666d3b Mon Sep 17 00:00:00 2001 From: shinya Date: Sat, 31 Jan 2026 14:07:22 +0100 Subject: [PATCH] fixed bugs --- src/music.rs | 95 ++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/src/music.rs b/src/music.rs index 82b90e7..5cf4f53 100644 --- a/src/music.rs +++ b/src/music.rs @@ -1,7 +1,6 @@ use crate::json_parsing::{self, add_song}; use audiotags::Tag; -use std::collections::HashSet; use std::fs; use std::io; use std::path::{Path, PathBuf}; @@ -101,41 +100,62 @@ pub fn get_song_file(path: &Path) -> Result<(), MusicError> { Ok(()) } +pub fn get_new_files(dir: &Path) -> io::Result> { + let mut new_files = Vec::new(); + collect_new_files(dir, &mut new_files)?; + Ok(new_files) +} + +fn collect_new_files(dir: &Path, out: &mut Vec) -> io::Result<()> { + let absolute = fs::canonicalize(dir)?; + let processed = json_parsing::return_all_processed_files(&absolute).unwrap_or_default(); + + for entry in fs::read_dir(&absolute)? { + let entry = entry?; + let path = entry.path(); + + if entry.file_type()?.is_dir() { + collect_new_files(&path, out)?; + } else { + let file_name = entry.file_name().to_string_lossy().to_string(); + + if !processed.contains(&file_name) { + out.push(path); + } + } + } + + Ok(()) +} + +fn collect_all_files(dir: &Path, out: &mut Vec) -> std::io::Result<()> { + for entry in std::fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + + if entry.file_type()?.is_dir() { + collect_all_files(&path, out)?; + } else { + out.push(path); + } + } + Ok(()) +} + pub fn get_all_music_files(directory: &Path, watch: bool) -> Result<(), MusicError> { if !directory.is_dir() { return Ok(()); } - let absolute = fs::canonicalize(directory).unwrap(); - - let already_processed_files: HashSet = - json_parsing::return_all_processed_files(&absolute).unwrap(); - - let files_to_process: Vec = if watch { - get_new_files(&absolute)? + let files = if watch { + get_new_files(directory)? } else { - let mut all_files = Vec::new(); - for entry in fs::read_dir(&absolute)? { - let entry = entry?; - let path = entry.path(); - if entry.file_type()?.is_dir() { - get_all_music_files(&path, watch)?; // recursive call for subdirectories - } else { - all_files.push(path); - } - } - all_files + let mut all = Vec::new(); + collect_all_files(directory, &mut all)?; + all }; - // Process files - for path in files_to_process { - let path_string = path.file_name().unwrap().to_string_lossy().to_string(); - - // Skip already processed files in full-run mode - if !watch && already_processed_files.contains(&path_string) { - continue; - } - + for path in files { if let Err(e) = get_song_file(&path) { eprintln!("Skipping file {}: {}", path.display(), e); } @@ -168,22 +188,3 @@ pub fn get_song_info(path: &Path) -> Result { pub fn get_song_length(path: &Path) -> Result { mp3_duration::from_path(path).map_err(|_| MusicError::DurationRead(path.display().to_string())) } - -pub fn get_new_files(path: &Path) -> io::Result> { - // Get the already processed files - let processed: HashSet = json_parsing::return_all_processed_files(path)?; - - // Read all files in the directory - let mut new_files = Vec::new(); - for entry in fs::read_dir(path)? { - let entry = entry?; - let file_name = entry.file_name().to_string_lossy().to_string(); - - // Only include files not in processed set - if !processed.contains(&file_name) && entry.path().is_file() { - new_files.push(entry.path()); - } - } - - Ok(new_files) -}