Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(src_tauri:fs_track): use WalkDir instead of globwalk #88

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ tauri-build = { version = "1.3", features = [] }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.4", features = [ "shell-open", "devtools", "dialog-all", "global-shortcut-all", "os-all", "path-all", "protocol-all", "window-all"] }
globwalk = "0.8.1"
reqwest = { version = "0.11.12", features = ["json"] }
lofty = "0.19.2"
anyhow = "1.0.71"
Expand All @@ -31,6 +30,7 @@ data-encoding = "2.4.0"
kira = "0.8.7"
symphonia = { version = "0.5.2", features = ["all"] }
regex = "1.10.4"
walkdir = "2"

[features]
# by default Tauri runs in production mode
Expand Down
48 changes: 34 additions & 14 deletions src-tauri/src/fs_track.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use globwalk::{glob, DirEntry};
use lofty::error::LoftyError;
use lofty::file::AudioFile;
use lofty::file::TaggedFileExt;
Expand All @@ -15,6 +14,7 @@ use thiserror::Error;
use std::time::Instant;
use crate::db;
use tauri::Manager;
use walkdir::{WalkDir, DirEntry};

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct FsTrack {
Expand Down Expand Up @@ -210,19 +210,25 @@ pub fn load_tracks_from_directories(directories: &Vec<String>, conn: &mut Connec
let mut files_scanned: usize = 0;
for directory in directories.iter() {
let mut entry_batch: Vec<DirEntry> = vec![];
let globwalker = glob(format!("{}/**/*.{{mp3,m4a,flac,ogg,opus,wav,MP3,M4A,FLAC,OGG,OPUS,WAV}}", directory))?;
for item in globwalker {
let entry = item?;
entry_batch.push(entry);
if entry_batch.len() == 100 {
let tracks = load_tracks_from_entry_batch(&entry_batch)?;

db::add_tracks(&tracks, conn)?;
files_scanned += entry_batch.len();
app_handle.emit_all("initialize-progress", ScanProgress { progress: None, files_scanned, files_count: Some(files_count) }).unwrap();
entry_batch.clear();
for entry in WalkDir::new(directory).follow_links(true).into_iter().filter_map(|e| e.ok()) {
if let Some(extension) = entry.path().extension() {
match extension.to_str().unwrap_or("").to_lowercase().as_str() {
"mp3" | "m4a" | "flac" | "ogg" | "opus" | "wav" => {
entry_batch.push(entry);
},
_ => {}
}
}
if entry_batch.len() == 100 {
let tracks = load_tracks_from_entry_batch(&entry_batch)?;

db::add_tracks(&tracks, conn)?;
files_scanned += entry_batch.len();
app_handle.emit_all("initialize-progress", ScanProgress { progress: None, files_scanned, files_count: Some(files_count) }).unwrap();
entry_batch.clear();
}
}

let tracks = load_tracks_from_entry_batch(&entry_batch)?;
db::add_tracks(&tracks, conn)?;
files_scanned += entry_batch.len();
Expand All @@ -233,11 +239,25 @@ pub fn load_tracks_from_directories(directories: &Vec<String>, conn: &mut Connec
Ok(())
}


pub fn count_files_from_directories(directories: &Vec<String>) -> Result<usize> {
let mut files_count = 0;

for directory in directories.iter() {
let files_in_dir = glob(format!("{}/**/*.{{mp3,m4a,flac,ogg,opus,wav,MP3,M4A,FLAC,OGG,OPUS,WAV}}", directory))?;
files_count += files_in_dir.into_iter().count();
println!("Scanning directory: {}", directory);

for entry in WalkDir::new(directory).follow_links(true).into_iter().filter_map(Result::ok) {
let path = entry.path();

if let Some(extension) = path.extension() {
match extension.to_str().unwrap_or("").to_lowercase().as_str() {
"mp3" | "m4a" | "flac" | "ogg" | "opus" | "wav" => {
files_count += 1;
},
_ => {}
}
}
}
}

Ok(files_count)
Expand Down