@@ -18,7 +18,8 @@ use crate::util::walk_dir;
1818use crate :: Cache ;
1919use crate :: { Config , TaggedFile , TaggedFileCollection } ;
2020use futures:: FutureExt ;
21- use std:: collections:: HashSet ;
21+ use regex:: Regex ;
22+ use std:: collections:: { HashMap , HashSet } ;
2223use std:: path:: PathBuf ;
2324use tokio:: runtime:: { Builder , Runtime } ;
2425use tokio:: sync:: mpsc:: Receiver ;
@@ -137,9 +138,13 @@ impl Drop for Scanner {
137138/// Find track collections in the given path.
138139fn find_track_paths ( input_path : PathBuf ) -> impl Iterator < Item = ( PathBuf , Vec < TaggedFile > ) > {
139140 let supported_extensions = HashSet :: from ( [ "mp3" , "flac" ] ) ;
141+ let disc_pattern = Regex :: new ( r"^(?:CD|Disc)\s*\d+$" ) . unwrap ( ) ;
142+
143+ let mut grouped_tracks: HashMap < PathBuf , Vec < TaggedFile > > = HashMap :: new ( ) ;
144+
140145 walk_dir ( input_path)
141146 . filter_map ( Result :: ok)
142- . filter_map ( move |( path, _dirs, files) | {
147+ . for_each ( |( path, _dirs, files) | {
143148 let tagged_files: Vec < TaggedFile > = files
144149 . iter ( )
145150 . filter ( |path| {
@@ -162,13 +167,33 @@ fn find_track_paths(input_path: PathBuf) -> impl Iterator<Item = (PathBuf, Vec<T
162167 . collect ( ) ;
163168
164169 if tagged_files. is_empty ( ) {
165- return None ;
170+ return ;
166171 }
167172
173+ // Check if the leaf directory matches CD/Disc pattern
174+ let group_path = if let Some ( file_name) = path. file_name ( ) {
175+ if let Some ( name_str) = file_name. to_str ( ) {
176+ if disc_pattern. is_match ( name_str) {
177+ path. parent ( ) . unwrap_or ( & path) . to_path_buf ( )
178+ } else {
179+ path. clone ( )
180+ }
181+ } else {
182+ path. clone ( )
183+ }
184+ } else {
185+ path. clone ( )
186+ } ;
187+
168188 log:: info!( "Found {} tracks in {}" , tagged_files. len( ) , path. display( ) ) ;
169189
170- Some ( ( path, tagged_files) )
171- } )
190+ grouped_tracks
191+ . entry ( group_path)
192+ . or_default ( )
193+ . extend ( tagged_files) ;
194+ } ) ;
195+
196+ grouped_tracks. into_iter ( )
172197}
173198
174199/// Analyze a file and assign the analysis results to it.
0 commit comments