Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6fa7ed44af |
95
src/music.rs
95
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<Vec<PathBuf>> {
|
||||
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<PathBuf>) -> 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::path::PathBuf>) -> 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<String> =
|
||||
json_parsing::return_all_processed_files(&absolute).unwrap();
|
||||
|
||||
let files_to_process: Vec<PathBuf> = 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<TrackInfo, MusicError> {
|
||||
pub fn get_song_length(path: &Path) -> Result<Duration, MusicError> {
|
||||
mp3_duration::from_path(path).map_err(|_| MusicError::DurationRead(path.display().to_string()))
|
||||
}
|
||||
|
||||
pub fn get_new_files(path: &Path) -> io::Result<Vec<PathBuf>> {
|
||||
// Get the already processed files
|
||||
let processed: HashSet<String> = 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)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user