From c167d5142f851e5d0ef54ee5910134c9b4f9639d Mon Sep 17 00:00:00 2001 From: aarbit Date: Fri, 7 Jun 2024 22:53:17 -0500 Subject: [PATCH] Simplifies music track retrieval. --- .../prettyplayerapi/service/MusicService.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/MusicService.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/MusicService.kt index de868b1..b194b93 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/MusicService.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/MusicService.kt @@ -1,5 +1,7 @@ package net.halfbinary.prettyplayerapi.service +import com.mpatric.mp3agic.Mp3File +import net.halfbinary.prettyplayerapi.exception.AlbumFolderNotFoundException import net.halfbinary.prettyplayerapi.exception.AlbumHashNotFoundException import org.springframework.stereotype.Service import java.io.File @@ -9,9 +11,14 @@ class MusicService(private val albumCacheService: AlbumCacheService) { fun getMusic(albumHash: String, trackNumber: Int): File { val albumInfo = albumCacheService.getAlbumCache()[albumHash] return if(albumInfo != null) { - File(albumInfo.albumFolder).listFiles().sortedBy { it.name }[trackNumber-1] + File(albumInfo.albumFolder) + .listFiles() + ?.filter { it.name.endsWith(".mp3") } + ?.sortedBy { it.name } + ?.get(trackNumber) + ?: throw AlbumFolderNotFoundException(albumInfo.albumFolder) } else { - throw AlbumHashNotFoundException("Album hash $albumHash not found") + throw AlbumHashNotFoundException(albumHash) } } } \ No newline at end of file