Compare commits
10 Commits
88d07b0fa0
...
8802755300
| Author | SHA1 | Date | |
|---|---|---|---|
| 8802755300 | |||
| 846fe20084 | |||
| 139d40f1e0 | |||
| 9ff2de36df | |||
| db994d92cd | |||
| afd9f22861 | |||
| c167d5142f | |||
| 5cdcb1b7c5 | |||
| da8a8f94d2 | |||
| 1acd3058bb |
3
.gitignore
vendored
3
.gitignore
vendored
@ -38,3 +38,6 @@ out/
|
||||
|
||||
### Kotlin ###
|
||||
.kotlin
|
||||
|
||||
### misc ###
|
||||
/data/
|
||||
|
||||
11
README.md
11
README.md
@ -5,4 +5,13 @@ Kotlin Spring Boot based REST service for the Pretty Player
|
||||
# Environment Variables
|
||||
|
||||
* `ALBUM_ROOT_DIR` = Directory where the albums of mp3s can be found
|
||||
* `ALBUM_ART_ROOT_DIR` = Directory where the album art can be cached for later
|
||||
* `ALBUM_ART_ROOT_DIR` = Directory where the album art can be cached for later
|
||||
|
||||
# TODO
|
||||
|
||||
* Add in DB like SQLite where the DB can be written to disk easily, and the DB can run in memory.
|
||||
* Allows for instant ready, while having new albums added async.
|
||||
|
||||
# API Documentation
|
||||
|
||||
* http://localhost:8080/swagger-ui/index.html
|
||||
@ -3,12 +3,13 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
plugins {
|
||||
id("org.springframework.boot") version "3.2.5"
|
||||
id("io.spring.dependency-management") version "1.1.4"
|
||||
kotlin("jvm") version "1.9.23"
|
||||
kotlin("plugin.spring") version "1.9.23"
|
||||
id("org.jetbrains.kotlin.plugin.jpa") version "2.0.0"
|
||||
kotlin("jvm") version "2.0.0"
|
||||
kotlin("plugin.spring") version "2.0.0"
|
||||
}
|
||||
|
||||
group = "net.halfbinary"
|
||||
version = "0.0.11-SNAPSHOT"
|
||||
version = "0.0.13-SNAPSHOT"
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_21
|
||||
@ -33,6 +34,7 @@ val coroutinesVersion = "1.8.1"
|
||||
dependencies {
|
||||
implementation("org.springframework.boot:spring-boot-starter-web")
|
||||
implementation("org.springframework.boot:spring-boot-starter-webflux")
|
||||
implementation("org.springframework.boot:spring-boot-starter-data-jpa:3.3.0")
|
||||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
|
||||
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
|
||||
implementation("org.jetbrains.kotlin:kotlin-reflect")
|
||||
@ -44,6 +46,8 @@ dependencies {
|
||||
implementation("org.slf4j:slf4j-api:$slf4jApiVersion")
|
||||
implementation("ch.qos.logback:logback-core:$logbackVersion")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
|
||||
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0")
|
||||
runtimeOnly("com.h2database:h2")
|
||||
developmentOnly("org.springframework.boot:spring-boot-devtools")
|
||||
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
|
||||
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||
|
||||
@ -0,0 +1,14 @@
|
||||
package net.halfbinary.prettyplayerapi.config
|
||||
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
class WebConfig : WebMvcConfigurer {
|
||||
override fun addCorsMappings(registry: CorsRegistry) {
|
||||
registry.addMapping("/**")
|
||||
}
|
||||
}
|
||||
@ -4,7 +4,10 @@ import net.halfbinary.prettyplayerapi.model.AlbumInfo
|
||||
import net.halfbinary.prettyplayerapi.model.AlbumMetadata
|
||||
import net.halfbinary.prettyplayerapi.model.TrackInfo
|
||||
import net.halfbinary.prettyplayerapi.service.AlbumService
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping("album")
|
||||
|
||||
@ -1,15 +1,17 @@
|
||||
package net.halfbinary.prettyplayerapi.controller
|
||||
|
||||
import net.halfbinary.prettyplayerapi.model.CacheType
|
||||
import net.halfbinary.prettyplayerapi.service.AlbumCacheService
|
||||
import org.springframework.web.bind.annotation.CrossOrigin
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RequestParam
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@CrossOrigin
|
||||
@RequestMapping("cache")
|
||||
class CacheController(private val albumCacheService: AlbumCacheService) {
|
||||
@GetMapping("cache")
|
||||
suspend fun refreshAlbumCache() {
|
||||
albumCacheService.refreshAlbumCache()
|
||||
@GetMapping
|
||||
suspend fun refreshAlbumCache(@RequestParam type: CacheType) {
|
||||
albumCacheService.cacheAlbums(type)
|
||||
}
|
||||
}
|
||||
@ -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()
|
||||
}
|
||||
}
|
||||
@ -2,15 +2,15 @@ package net.halfbinary.prettyplayerapi.controller
|
||||
|
||||
import net.halfbinary.prettyplayerapi.service.ImageService
|
||||
import org.springframework.http.MediaType
|
||||
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.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@CrossOrigin
|
||||
@RequestMapping("image")
|
||||
class ImageController(private val imageService: ImageService) {
|
||||
@GetMapping(value = ["image/{id}"], produces = [MediaType.IMAGE_JPEG_VALUE])
|
||||
@GetMapping(value = ["/{id}"], produces = [MediaType.IMAGE_JPEG_VALUE])
|
||||
fun getImage(@PathVariable("id") id: String): ByteArray {
|
||||
return imageService.getImage(id).readBytes()
|
||||
}
|
||||
|
||||
@ -2,16 +2,21 @@ package net.halfbinary.prettyplayerapi.controller
|
||||
|
||||
import net.halfbinary.prettyplayerapi.service.MusicService
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody
|
||||
|
||||
@RestController
|
||||
@RequestMapping("music")
|
||||
@CrossOrigin
|
||||
class MusicController(private val musicService: MusicService) {
|
||||
|
||||
@GetMapping(value = ["/album/{albumHash}/{trackNumber}"], produces = ["audio/mpeg3"])
|
||||
fun getAlbum(@PathVariable albumHash: String, @PathVariable trackNumber: Int): ResponseEntity<StreamingResponseBody> {
|
||||
@GetMapping(value = ["/album/{albumHash}/track/{trackNumber}"], produces = ["audio/mpeg3"])
|
||||
fun getAlbum(
|
||||
@PathVariable albumHash: String,
|
||||
@PathVariable trackNumber: Int
|
||||
): ResponseEntity<StreamingResponseBody> {
|
||||
val track = musicService.getMusic(albumHash, trackNumber)
|
||||
val responseBody = StreamingResponseBody { stream -> stream.write(track.readBytes()) }
|
||||
return ResponseEntity.ok().body(responseBody)
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package net.halfbinary.prettyplayerapi.exception
|
||||
|
||||
class AlbumFolderNotFoundException(folder: String) :
|
||||
RuntimeException("Album folder $folder does not exist")
|
||||
@ -1,4 +1,3 @@
|
||||
package net.halfbinary.prettyplayerapi.exception
|
||||
|
||||
class AlbumHashNotFoundException(message: String) : RuntimeException(message) {
|
||||
}
|
||||
class AlbumHashNotFoundException(hash: String) : RuntimeException("Album hash $hash not found")
|
||||
@ -1,4 +1,4 @@
|
||||
package net.halfbinary.prettyplayerapi.exception
|
||||
|
||||
class EnvironmentVariableNotFoundException(message: String) : RuntimeException(message) {
|
||||
}
|
||||
class EnvironmentVariableNotFoundException(envVar: String) :
|
||||
RuntimeException("Environment variable $envVar does not exist, please specify")
|
||||
@ -10,21 +10,41 @@ class GlobalExceptionHandler {
|
||||
@ExceptionHandler
|
||||
fun handleEnvironmentVariableNotFoundException(e: EnvironmentVariableNotFoundException): ResponseEntity<ErrorResponse> {
|
||||
return ResponseEntity(
|
||||
ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||
e.message ?:""), HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
ErrorResponse(
|
||||
HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||
e.message ?: ""
|
||||
), HttpStatus.INTERNAL_SERVER_ERROR
|
||||
)
|
||||
}
|
||||
|
||||
@ExceptionHandler
|
||||
fun handleAlbumHashNotFoundException(e: AlbumHashNotFoundException): ResponseEntity<ErrorResponse> {
|
||||
return ResponseEntity(
|
||||
ErrorResponse(HttpStatus.BAD_REQUEST.value(),
|
||||
e.message ?:""), HttpStatus.BAD_REQUEST)
|
||||
ErrorResponse(
|
||||
HttpStatus.BAD_REQUEST.value(),
|
||||
e.message ?: ""
|
||||
), HttpStatus.BAD_REQUEST
|
||||
)
|
||||
}
|
||||
|
||||
@ExceptionHandler
|
||||
fun handleAlbumFolderNotFoundException(e: AlbumFolderNotFoundException): ResponseEntity<ErrorResponse> {
|
||||
return ResponseEntity(
|
||||
ErrorResponse(
|
||||
HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||
e.message ?: ""
|
||||
), HttpStatus.INTERNAL_SERVER_ERROR
|
||||
)
|
||||
}
|
||||
|
||||
@ExceptionHandler
|
||||
fun handleException(e: RuntimeException): ResponseEntity<ErrorResponse> {
|
||||
return ResponseEntity(
|
||||
ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||
e.message?:""), HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
ErrorResponse(
|
||||
HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||
e.message ?: ""
|
||||
), HttpStatus.INTERNAL_SERVER_ERROR
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,7 @@
|
||||
package net.halfbinary.prettyplayerapi.model
|
||||
|
||||
enum class CacheType {
|
||||
ART,
|
||||
NEW,
|
||||
ALL
|
||||
}
|
||||
@ -1,9 +1,21 @@
|
||||
package net.halfbinary.prettyplayerapi.repository
|
||||
|
||||
import net.halfbinary.prettyplayerapi.model.AlbumMetadata
|
||||
import jakarta.persistence.Entity
|
||||
import jakarta.persistence.Id
|
||||
import jakarta.persistence.Table
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
class AlbumRepository {
|
||||
var albumCache: LinkedHashMap<String, AlbumMetadata> = linkedMapOf()
|
||||
}
|
||||
interface AlbumRepository : JpaRepository<AlbumRecord, String>
|
||||
|
||||
@Entity
|
||||
@Table(name = "album")
|
||||
data class AlbumRecord(
|
||||
@Id
|
||||
val id: String,
|
||||
val albumTitle: String,
|
||||
val albumFolder: String,
|
||||
val albumArtist: String,
|
||||
val albumYear: Int
|
||||
)
|
||||
@ -4,8 +4,10 @@ import com.mpatric.mp3agic.Mp3File
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import kotlinx.coroutines.*
|
||||
import net.halfbinary.prettyplayerapi.exception.EnvironmentVariableNotFoundException
|
||||
import net.halfbinary.prettyplayerapi.model.AlbumMetadata
|
||||
import net.halfbinary.prettyplayerapi.model.CacheType
|
||||
import net.halfbinary.prettyplayerapi.repository.AlbumRecord
|
||||
import net.halfbinary.prettyplayerapi.repository.AlbumRepository
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import java.awt.Image
|
||||
import java.awt.image.BufferedImage
|
||||
@ -17,53 +19,81 @@ import kotlin.io.path.name
|
||||
private val logger = KotlinLogging.logger {}
|
||||
|
||||
@Service
|
||||
class AlbumCacheService(private val albumRepository: AlbumRepository,
|
||||
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO) {
|
||||
|
||||
|
||||
suspend fun doAlbumCache() {
|
||||
coroutineScope {
|
||||
launch { cacheAlbums(false) }
|
||||
}
|
||||
}
|
||||
suspend fun refreshAlbumCache() {
|
||||
coroutineScope{
|
||||
launch { cacheAlbums(true)}
|
||||
}
|
||||
}
|
||||
|
||||
fun getAlbumCache(): LinkedHashMap<String, AlbumMetadata> {
|
||||
return albumRepository.albumCache
|
||||
}
|
||||
|
||||
suspend fun cacheAlbums(refresh: Boolean = false) {
|
||||
logger.debug { "Starting the cache process with refresh set to $refresh" }
|
||||
class AlbumCacheService(
|
||||
private val albumRepository: AlbumRepository,
|
||||
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO
|
||||
) {
|
||||
suspend fun cacheAlbums(type: CacheType = CacheType.ART) {
|
||||
logger.debug { "Starting the cache process with type ${type.name}" }
|
||||
createAlbumArtRootDir()
|
||||
val list = getAlbumRootDir()
|
||||
val fileList = getAlbumRootDir()
|
||||
.walkTopDown()
|
||||
.filter {
|
||||
it.isFile && it.extension == "mp3" && it.name.startsWith("01.")
|
||||
}
|
||||
.toList()
|
||||
.map { Pair(it, Mp3File(it)) }
|
||||
.sortedWith(getMusicComparator())
|
||||
.map {
|
||||
val folder = it.first.parentFile.toPath()
|
||||
val folderHash = folder.name.hashCode().toString()
|
||||
coroutineScope {
|
||||
launch {getAndWriteAlbumImageFile(it.first, folderHash, folder, refresh)}
|
||||
}
|
||||
Pair(folderHash, AlbumMetadata(it.second.id3v2Tag.album, it.first.parent, folderHash))
|
||||
}
|
||||
when (type) {
|
||||
CacheType.ART -> cacheArt(fileList)
|
||||
CacheType.NEW -> cacheNew(fileList)
|
||||
CacheType.ALL -> cacheAll(fileList)
|
||||
}
|
||||
}
|
||||
|
||||
albumRepository.albumCache = convertAlbumListToMapForCache(list)
|
||||
//TODO: Write cache to file for later reading...or get a DB going?
|
||||
logger.info { "Albums cached: ${albumRepository.albumCache.size}" }
|
||||
suspend fun cacheAll(fileList: List<File>) {
|
||||
fileList.forEach {
|
||||
val mp3File = Mp3File(it)
|
||||
val folderPath = it.parentFile.toPath()
|
||||
val folderHash = folderPath.name.hashCode().toString()
|
||||
coroutineScope {
|
||||
launch { getAndWriteAlbumImageFile(it, folderHash, folderPath) }
|
||||
}
|
||||
albumRepository.save(
|
||||
AlbumRecord(
|
||||
folderHash,
|
||||
mp3File.id3v2Tag.album,
|
||||
it.parent,
|
||||
mp3File.id3v2Tag.albumArtist,
|
||||
mp3File.id3v2Tag.year.toInt()
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun cacheNew(fileList: List<File>) {
|
||||
fileList.forEach {
|
||||
val folderPath = it.parentFile.toPath()
|
||||
val folderHash = folderPath.name.hashCode().toString()
|
||||
if (albumRepository.findByIdOrNull(folderHash) == null) {
|
||||
coroutineScope {
|
||||
launch { getAndWriteAlbumImageFile(it, folderHash, folderPath) }
|
||||
}
|
||||
val mp3File = Mp3File(it)
|
||||
albumRepository.save(
|
||||
AlbumRecord(
|
||||
folderHash,
|
||||
mp3File.id3v2Tag.album,
|
||||
it.parent,
|
||||
mp3File.id3v2Tag.albumArtist,
|
||||
mp3File.id3v2Tag.year.toInt()
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun cacheArt(fileList: List<File>) {
|
||||
fileList.forEach {
|
||||
val folderPath = it.parentFile.toPath()
|
||||
val folderHash = folderPath.name.hashCode().toString()
|
||||
coroutineScope {
|
||||
launch { getAndWriteAlbumImageFile(it, folderHash, folderPath) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun createAlbumArtRootDir() {
|
||||
val root = File(ALBUM_ART_ROOT_DIR)
|
||||
if(!root.exists()) {
|
||||
if (!root.exists()) {
|
||||
root.mkdirs()
|
||||
}
|
||||
logger.info { "Album Art Directory: $root" }
|
||||
@ -75,20 +105,15 @@ class AlbumCacheService(private val albumRepository: AlbumRepository,
|
||||
return root
|
||||
}
|
||||
|
||||
fun getMusicComparator(): Comparator<Pair<File, Mp3File>> {
|
||||
return compareBy({ it.second.id3v2Tag.albumArtist }, {it.second.id3v2Tag.album}, {it.second.id3v2Tag.year})
|
||||
}
|
||||
|
||||
fun convertAlbumListToMapForCache(list: List<Pair<String, AlbumMetadata>>): java.util.LinkedHashMap<String, AlbumMetadata> {
|
||||
val array = list.toTypedArray()
|
||||
return linkedMapOf(*array)
|
||||
}
|
||||
|
||||
suspend fun getAndWriteAlbumImageFile(file: File, folderHash: String, folder: Path, refresh: Boolean = false) {
|
||||
suspend fun getAndWriteAlbumImageFile(
|
||||
file: File,
|
||||
folderHash: String,
|
||||
folder: Path
|
||||
) {
|
||||
logger.info { "Checking album $folder with hash $folderHash" }
|
||||
val hashedFile = File("$ALBUM_ART_ROOT_DIR/$folderHash.jpg")
|
||||
if(refresh || !hashedFile.exists()) {
|
||||
logger.debug { "No image for $folder, making one..." }
|
||||
if (!hashedFile.exists()) {
|
||||
logger.debug { "Making image for $folder with hash $folderHash..." }
|
||||
val imageByteStream = Mp3File(file).id3v2Tag.albumImage.inputStream()
|
||||
val bufferedImage = withContext(ioDispatcher) {
|
||||
ImageIO.read(imageByteStream)
|
||||
@ -108,6 +133,7 @@ class AlbumCacheService(private val albumRepository: AlbumRepository,
|
||||
return System.getenv(envName)
|
||||
?: throw EnvironmentVariableNotFoundException(envName)
|
||||
}
|
||||
|
||||
val ALBUM_ROOT_DIR = getEnv("ALBUM_ROOT_DIR") //"q:\\CDs"
|
||||
val ALBUM_ART_ROOT_DIR = getEnv("ALBUM_ART_ROOT_DIR") //"src/main/resources/images/"
|
||||
}
|
||||
|
||||
@ -1,11 +1,64 @@
|
||||
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 net.halfbinary.prettyplayerapi.repository.AlbumRepository
|
||||
import org.springframework.data.domain.Sort
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
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> {
|
||||
return albumCacheService.getAlbumCache().map { (_,v) -> v }
|
||||
class AlbumService(private val albumRepository: AlbumRepository) {
|
||||
fun getAlbumList(): List<AlbumMetadata> {
|
||||
return albumRepository
|
||||
.findAll(Sort.by(Sort.Direction.ASC, "albumArtist", "albumYear", "albumTitle"))
|
||||
.map { AlbumMetadata(it.albumTitle, it.albumFolder, it.id) }
|
||||
}
|
||||
|
||||
fun getAlbumInfo(albumHash: String): AlbumInfo {
|
||||
val albumRecordResponse = albumRepository.findByIdOrNull(albumHash)
|
||||
val trackFileList = if (albumRecordResponse != null) {
|
||||
File(albumRecordResponse.albumFolder).listFiles()
|
||||
?.filter { it.name.endsWith(".mp3") }
|
||||
?.sortedBy { it.name }
|
||||
?: throw AlbumFolderNotFoundException(albumRecordResponse.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 = albumRepository.findByIdOrNull(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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 }
|
||||
}
|
||||
}
|
||||
@ -1,17 +1,25 @@
|
||||
package net.halfbinary.prettyplayerapi.service
|
||||
|
||||
import net.halfbinary.prettyplayerapi.exception.AlbumFolderNotFoundException
|
||||
import net.halfbinary.prettyplayerapi.exception.AlbumHashNotFoundException
|
||||
import net.halfbinary.prettyplayerapi.repository.AlbumRepository
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import java.io.File
|
||||
|
||||
@Service
|
||||
class MusicService(private val albumCacheService: AlbumCacheService) {
|
||||
class MusicService(private val albumRepository: AlbumRepository) {
|
||||
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]
|
||||
val albumInfo = albumRepository.findByIdOrNull(albumHash)
|
||||
return if (albumInfo != null) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,23 +1,39 @@
|
||||
package net.halfbinary.prettyplayerapi.service
|
||||
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.*
|
||||
import net.halfbinary.prettyplayerapi.model.CacheType
|
||||
import net.halfbinary.prettyplayerapi.repository.AlbumRepository
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent
|
||||
import org.springframework.context.event.EventListener
|
||||
import org.springframework.scheduling.annotation.Async
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
private val logger = KotlinLogging.logger {}
|
||||
|
||||
@Component
|
||||
class StartupService(private val albumCacheService: AlbumCacheService) {
|
||||
class StartupService(
|
||||
private val albumRepository: AlbumRepository,
|
||||
private val albumCacheService: AlbumCacheService,
|
||||
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO
|
||||
) {
|
||||
|
||||
@Async
|
||||
@EventListener(ApplicationStartedEvent::class)
|
||||
suspend fun onStartup() {
|
||||
coroutineScope {
|
||||
launch {
|
||||
albumCacheService.doAlbumCache()
|
||||
if (withContext(ioDispatcher) {
|
||||
albumRepository.count()
|
||||
} == 0L) {
|
||||
coroutineScope {
|
||||
launch {
|
||||
albumCacheService.cacheAlbums(CacheType.ALL)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
coroutineScope {
|
||||
launch {
|
||||
albumCacheService.cacheAlbums(CacheType.NEW)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1 +1,6 @@
|
||||
spring.application.name=prettyplayerapi
|
||||
spring.datasource.url=jdbc:h2:file:./data/prettyplayerdata
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=pp
|
||||
spring.datasource.password=pppass
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user