Refactors track controller endpoints into album controller. Removes outmoded folder controller and service.

This commit is contained in:
aarbit 2024-06-07 22:48:02 -05:00
parent 88d07b0fa0
commit 1acd3058bb
4 changed files with 50 additions and 53 deletions

View File

@ -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<String> {
return folderService.getFolderList()
}
}

View File

@ -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)
}
}
}

View File

@ -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<AlbumInfo> {
fun getAlbumList(): List<AlbumMetadata> {
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)
}
}
}

View File

@ -1,10 +0,0 @@
package net.halfbinary.prettyplayerapi.service
import org.springframework.stereotype.Service
@Service
class FolderService(private val albumCacheService: AlbumCacheService) {
fun getFolderList(): List<String> {
return albumCacheService.getAlbumCache().map { (_,v) -> v.albumFolder }
}
}