From 60add901bde01f5804f191a5d747896fb1bfd2f6 Mon Sep 17 00:00:00 2001
From: G2-Games <ke0bhogsg@gmail.com>
Date: Wed, 1 Nov 2023 10:57:15 -0500
Subject: [PATCH] Finished CUE file support

---
 src/music_storage/music_db.rs | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/src/music_storage/music_db.rs b/src/music_storage/music_db.rs
index 3135b34..7eae5b5 100644
--- a/src/music_storage/music_db.rs
+++ b/src/music_storage/music_db.rs
@@ -493,9 +493,19 @@ impl MusicLibrary {
             let _ = self.remove_uri(&URI::Local(audio_location.clone()));
 
             // Get the track timing information
-            let start = Duration::from_micros((track.get_start() as f32 * 13333.333333).round() as u64);
+            let pregap = match track.get_zero_pre() {
+                Some(pregap) => Duration::from_micros((pregap as f32 * 13333.333333) as u64),
+                None => Duration::from_secs(0)
+            };
+            let postgap = match track.get_zero_post() {
+                Some(postgap) => Duration::from_micros((postgap as f32 * 13333.333333) as u64),
+                None => Duration::from_secs(0)
+            };
+            let mut start = Duration::from_micros((track.get_start() as f32 * 13333.333333) as u64);
+            start -= pregap;
+
             let duration = match track.get_length() {
-                Some(len) =>  Duration::from_micros((len as f32 * 13333.333333).round() as u64),
+                Some(len) =>  Duration::from_micros((len as f32 * 13333.333333) as u64),
                 None => {
                     let tagged_file = match lofty::read_from_path(&audio_location) {
                         Ok(tagged_file) => tagged_file,
@@ -510,7 +520,7 @@ impl MusicLibrary {
                     tagged_file.properties().duration() - start
                 }
             };
-            let end = start + duration;
+            let end = start + duration + postgap;
 
             // Get the format as a string
             let format: Option<FileFormat> = match FileFormat::from_file(&audio_location) {
@@ -563,7 +573,7 @@ impl MusicLibrary {
             match self.add_song(new_song) {
                 Ok(_) => tracks_added += 1,
                 Err(_error) => {
-                    //println!("{}", error);
+                    println!("{}", _error);
                     continue
                 },
             };
@@ -579,6 +589,17 @@ impl MusicLibrary {
             }
             None => (),
         }
+        match self.query_path(&new_song.location.path()) {
+            Some(songs) => {
+                for song in songs {
+                    match &song.location {
+                        URI::Local(location) => return Err(format!("Cuesheet exists: {:?}", new_song.location).into()),
+                        _ => ()
+                    }
+                }
+            }
+            None => (),
+        }
 
         self.library.push(new_song);