fixed bugs
This commit is contained in:
parent
ce3eaac9c2
commit
6fa7ed44af
95
src/music.rs
95
src/music.rs
@ -1,7 +1,6 @@
|
|||||||
use crate::json_parsing::{self, add_song};
|
use crate::json_parsing::{self, add_song};
|
||||||
|
|
||||||
use audiotags::Tag;
|
use audiotags::Tag;
|
||||||
use std::collections::HashSet;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
@ -101,41 +100,62 @@ pub fn get_song_file(path: &Path) -> Result<(), MusicError> {
|
|||||||
Ok(())
|
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> {
|
pub fn get_all_music_files(directory: &Path, watch: bool) -> Result<(), MusicError> {
|
||||||
if !directory.is_dir() {
|
if !directory.is_dir() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let absolute = fs::canonicalize(directory).unwrap();
|
let files = if watch {
|
||||||
|
get_new_files(directory)?
|
||||||
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)?
|
|
||||||
} else {
|
} else {
|
||||||
let mut all_files = Vec::new();
|
let mut all = Vec::new();
|
||||||
for entry in fs::read_dir(&absolute)? {
|
collect_all_files(directory, &mut all)?;
|
||||||
let entry = entry?;
|
all
|
||||||
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
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Process files
|
for path in 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(e) = get_song_file(&path) {
|
if let Err(e) = get_song_file(&path) {
|
||||||
eprintln!("Skipping file {}: {}", path.display(), e);
|
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> {
|
pub fn get_song_length(path: &Path) -> Result<Duration, MusicError> {
|
||||||
mp3_duration::from_path(path).map_err(|_| MusicError::DurationRead(path.display().to_string()))
|
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