11use  crate :: data_js:: get_data; 
22use  crate :: get_now_timestamp; 
3+ use  crate :: library:: Paths ; 
34use  crate :: library_types:: { 
45	CountObject ,  Folder ,  Library ,  Playlist ,  Track ,  TrackList ,  new_item_ids_from_track_ids, 
56} ; 
@@ -11,7 +12,7 @@ use napi::Env;
1112use  serde:: { Deserialize ,  Serialize } ; 
1213use  std:: collections:: HashMap ; 
1314use  std:: fs; 
14- use  std:: path:: { Path ,   PathBuf } ; 
15+ use  std:: path:: PathBuf ; 
1516use  std:: sync:: Mutex ; 
1617use  time:: OffsetDateTime ; 
1718use  time:: serde:: iso8601; 
@@ -319,11 +320,7 @@ fn parse_file_url(value: &str) -> Result<PathBuf> {
319320} 
320321
321322/// Parses track but does not move it to `tracks_dir` 
322- fn  parse_track ( 
323- 	xml_track :  XmlTrack , 
324- 	start_time :  i64 , 
325- 	tracks_dir :  & Path , 
326- )  -> Result < ( PathBuf ,  Track ) >  { 
323+ fn  parse_track ( xml_track :  XmlTrack ,  start_time :  i64 ,  paths :  & Paths )  -> Result < ( PathBuf ,  Track ) >  { 
327324	let  xml_location = xml_track. location . context ( "Missing track location" ) ?; 
328325	if  xml_track. track_type  != Some ( "File" . to_string ( ) )  { 
329326		bail ! ( 
@@ -358,7 +355,7 @@ fn parse_track(
358355
359356	let  name = xml_track. name . unwrap_or_default ( ) ; 
360357	let  artist = xml_track. artist . unwrap_or_default ( ) ; 
361- 	let  filename = generate_filename ( tracks_dir ,  & artist,  & name,  file_type. file_extension ( ) ) ; 
358+ 	let  filename = generate_filename ( & paths ,  & artist,  & name,  file_type. file_extension ( ) ) ; 
362359
363360	let  track = Track  { 
364361		size :  file_md. len ( )  as  i64 , 
@@ -584,6 +581,7 @@ pub struct ItunesImport {
584581	new_library :  Mutex < Option < Library > > , 
585582	/// iTunes path -> Ferrum file 
586583 	itunes_track_paths :  Mutex < HashMap < PathBuf ,  String > > , 
584+ 	paths :  Paths , 
587585} 
588586#[ napi]  
589587impl  ItunesImport  { 
@@ -593,18 +591,19 @@ impl ItunesImport {
593591		Ok ( Self  { 
594592			new_library :  Some ( data. library . clone ( ) ) . into ( ) , 
595593			itunes_track_paths :  HashMap :: new ( ) . into ( ) , 
594+ 			paths :  data. paths . clone ( ) , 
596595		} ) 
597596	} 
598597	#[ napi]  
599- 	pub  async  fn  start ( & self ,  path :  String ,   tracks_dir :   String )  -> napi:: Result < ImportStatus >  { 
600- 		Ok ( import_itunes ( self ,  path,  tracks_dir ) . await ?) 
598+ 	pub  async  fn  start ( & self ,  path :  String )  -> napi:: Result < ImportStatus >  { 
599+ 		Ok ( import_itunes ( self ,  path) . await ?) 
601600	} 
602601	#[ napi]  
603602	pub  fn  finish ( & mut  self ,  env :  Env )  -> napi:: Result < ( ) >  { 
604603		let  data = get_data ( & env) ; 
605604		let  itunes_track_paths = & mut  * self . itunes_track_paths . lock ( ) . unwrap ( ) ; 
606605		for  ( itunes_path,  ferrum_file)  in  itunes_track_paths { 
607- 			let  new_path = data. paths . tracks_dir . join ( ferrum_file) ; 
606+ 			let  new_path = data. paths . get_track_file_path ( ferrum_file) ; 
608607			fs:: copy ( itunes_path,  new_path) . context ( "Error copying file" ) ?; 
609608		} 
610609		let  new_library = & mut  self . new_library . lock ( ) . unwrap ( ) ; 
@@ -613,11 +612,7 @@ impl ItunesImport {
613612	} 
614613} 
615614
616- pub  async  fn  import_itunes ( 
617- 	itunes_import :  & ItunesImport , 
618- 	path :  String , 
619- 	tracks_dir :  String , 
620- )  -> Result < ImportStatus >  { 
615+ async  fn  import_itunes ( itunes_import :  & ItunesImport ,  path :  String )  -> Result < ImportStatus >  { 
621616	let  new_library_lock = & mut  * itunes_import. new_library . lock ( ) . unwrap ( ) ; 
622617	let  mut  library = match  new_library_lock { 
623618		Some ( library)  => library, 
@@ -663,7 +658,7 @@ pub async fn import_itunes(
663658			errors. push ( format ! ( "Missing track artist: {artist_title}" ) ) ; 
664659		} 
665660
666- 		match  parse_track ( xml_track,  start_time,  Path :: new ( & tracks_dir ) )  { 
661+ 		match  parse_track ( xml_track,  start_time,  & itunes_import . paths )  { 
667662			Ok ( ( xml_track_path,  track) )  => { 
668663				let  generated_id = library. generate_id ( ) ; 
669664				// immediately insert into library so new generated ids are unique 
0 commit comments