From 1acd3058bb26c6372bb9f0be4f9430acf85fa68e Mon Sep 17 00:00:00 2001 From: aarbit Date: Fri, 7 Jun 2024 22:48:02 -0500 Subject: [PATCH] Refactors track controller endpoints into album controller. Removes outmoded folder controller and service. --- .../controller/FolderController.kt | 15 ------ .../controller/TrackController.kt | 27 ---------- .../prettyplayerapi/service/AlbumService.kt | 51 ++++++++++++++++++- .../prettyplayerapi/service/FolderService.kt | 10 ---- 4 files changed, 50 insertions(+), 53 deletions(-) delete mode 100644 src/main/kotlin/net/halfbinary/prettyplayerapi/controller/FolderController.kt delete mode 100644 src/main/kotlin/net/halfbinary/prettyplayerapi/controller/TrackController.kt delete mode 100644 src/main/kotlin/net/halfbinary/prettyplayerapi/service/FolderService.kt diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/FolderController.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/FolderController.kt deleted file mode 100644 index fbbcceb..0000000 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/FolderController.kt +++ /dev/null @@ -1,15 +0,0 @@ -package net.halfbinary.prettyplayerapi.controller - -import net.halfbinary.prettyplayerapi.service.FolderService -import org.springframework.web.bind.annotation.CrossOrigin -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.RestController - -@RestController -@CrossOrigin -class FolderController(private val folderService: FolderService) { - @GetMapping("folders") - fun listFolders(): List { - return folderService.getFolderList() - } -} \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/TrackController.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/TrackController.kt deleted file mode 100644 index f2504d1..0000000 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/TrackController.kt +++ /dev/null @@ -1,27 +0,0 @@ -package net.halfbinary.prettyplayerapi.controller - -import com.mpatric.mp3agic.Mp3File -import io.github.oshai.kotlinlogging.KotlinLogging -import net.halfbinary.prettyplayerapi.model.TrackInfo -import net.halfbinary.prettyplayerapi.service.MusicService -import org.springframework.web.bind.annotation.CrossOrigin -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PathVariable -import org.springframework.web.bind.annotation.RestController - -private val logger = KotlinLogging.logger {} - -@RestController -@CrossOrigin -class TrackController(private val musicService: MusicService) { - @GetMapping("track/{albumHash}/{trackNumber}") - fun getTrackInfo(@PathVariable albumHash: String, @PathVariable trackNumber: Int): TrackInfo { - val file = musicService.getMusic(albumHash, trackNumber) - val trackFile = Mp3File(file) - trackFile.id3v2Tag.artist - trackFile.id3v2Tag.apply { - logger.debug { "Getting track $trackNumber. $title by $artist on $album" } - return TrackInfo(album, artist, title) - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumService.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumService.kt index bf681fc..40ae801 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumService.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumService.kt @@ -1,11 +1,60 @@ package net.halfbinary.prettyplayerapi.service +import com.mpatric.mp3agic.Mp3File +import io.github.oshai.kotlinlogging.KotlinLogging +import net.halfbinary.prettyplayerapi.exception.AlbumFolderNotFoundException +import net.halfbinary.prettyplayerapi.exception.AlbumHashNotFoundException import net.halfbinary.prettyplayerapi.model.AlbumInfo +import net.halfbinary.prettyplayerapi.model.AlbumMetadata +import net.halfbinary.prettyplayerapi.model.AlbumTrackInfo +import net.halfbinary.prettyplayerapi.model.TrackInfo import org.springframework.stereotype.Service +import java.io.File + +private val logger = KotlinLogging.logger {} @Service class AlbumService(private val albumCacheService: AlbumCacheService) { - fun getAlbumList(): List { + fun getAlbumList(): List { return albumCacheService.getAlbumCache().map { (_,v) -> v } } + + fun getAlbumInfo(albumHash: String): AlbumInfo { + val albumInfo = albumCacheService.getAlbumCache()[albumHash] + val trackFileList = if(albumInfo != null) { + File(albumInfo.albumFolder).listFiles() + ?.filter { it.name.endsWith(".mp3") } + ?.sortedBy { it.name } + ?: throw AlbumFolderNotFoundException(albumInfo.albumFolder) + + } else { + throw AlbumHashNotFoundException(albumHash) + } + val trackList = trackFileList.map { + val id3 = Mp3File(it).id3v2Tag + id3.run { + AlbumTrackInfo(track.toInt(), artist, title) + } + } + val id3 = Mp3File(trackFileList[0]).id3v2Tag + return AlbumInfo(id3.album, id3.albumArtist, trackList) + } + + fun getTrackInfo(albumHash: String, trackNumber: Int): TrackInfo { + val albumInfo = albumCacheService.getAlbumCache()[albumHash] + val file = if(albumInfo != null) { + File(albumInfo.albumFolder).listFiles() + ?.filter { it.name.endsWith(".mp3") } + ?.sortedBy { it.name } + ?.get(trackNumber) + } else { + throw AlbumHashNotFoundException(albumHash) + } + val trackFile = Mp3File(file) + trackFile.id3v2Tag.artist + trackFile.id3v2Tag.apply { + logger.debug { "Getting track $trackNumber: $track. $title by $artist on $album" } + return TrackInfo(album, artist, title) + } + } } \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/FolderService.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/FolderService.kt deleted file mode 100644 index e147427..0000000 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/FolderService.kt +++ /dev/null @@ -1,10 +0,0 @@ -package net.halfbinary.prettyplayerapi.service - -import org.springframework.stereotype.Service - -@Service -class FolderService(private val albumCacheService: AlbumCacheService) { - fun getFolderList(): List { - return albumCacheService.getAlbumCache().map { (_,v) -> v.albumFolder } - } -} \ No newline at end of file