From b1e997430b92a671207e2b78974abba28c536446 Mon Sep 17 00:00:00 2001 From: aarbit Date: Fri, 7 Jun 2024 22:44:13 -0500 Subject: [PATCH] Revamps the album cache service to make it comply with SonarLint. Also adds and renames some models. --- .../dispatcher/IODispatcher.kt | 10 ++++++ .../prettyplayerapi/model/AlbumInfo.kt | 6 +++- .../prettyplayerapi/model/AlbumTrackInfo.kt | 7 ++++ .../service/AlbumCacheService.kt | 32 +++++++++++-------- 4 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 src/main/kotlin/net/halfbinary/prettyplayerapi/dispatcher/IODispatcher.kt create mode 100644 src/main/kotlin/net/halfbinary/prettyplayerapi/model/AlbumTrackInfo.kt diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/dispatcher/IODispatcher.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/dispatcher/IODispatcher.kt new file mode 100644 index 0000000..4bdd348 --- /dev/null +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/dispatcher/IODispatcher.kt @@ -0,0 +1,10 @@ +package net.halfbinary.prettyplayerapi.dispatcher + +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import org.springframework.context.annotation.Bean + +@Bean +fun getIODispatcher(): CoroutineDispatcher { + return Dispatchers.IO +} \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/model/AlbumInfo.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/model/AlbumInfo.kt index 6cea8b1..78a2af2 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/model/AlbumInfo.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/model/AlbumInfo.kt @@ -1,3 +1,7 @@ package net.halfbinary.prettyplayerapi.model -data class AlbumInfo(val albumTitle: String, val albumFolder: String, val hash: String) +data class AlbumInfo( + val albumTitle: String, + val albumArtist: String, + val trackList: List +) diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/model/AlbumTrackInfo.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/model/AlbumTrackInfo.kt new file mode 100644 index 0000000..199e4b3 --- /dev/null +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/model/AlbumTrackInfo.kt @@ -0,0 +1,7 @@ +package net.halfbinary.prettyplayerapi.model + +data class AlbumTrackInfo( + val trackNumber: Int, + val trackArtist: String, + val trackTitle: String +) diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumCacheService.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumCacheService.kt index e42e423..44d55b3 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumCacheService.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumCacheService.kt @@ -2,10 +2,9 @@ package net.halfbinary.prettyplayerapi.service import com.mpatric.mp3agic.Mp3File import io.github.oshai.kotlinlogging.KotlinLogging -import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.launch +import kotlinx.coroutines.* import net.halfbinary.prettyplayerapi.exception.EnvironmentVariableNotFoundException -import net.halfbinary.prettyplayerapi.model.AlbumInfo +import net.halfbinary.prettyplayerapi.model.AlbumMetadata import net.halfbinary.prettyplayerapi.repository.AlbumRepository import org.springframework.stereotype.Service import java.awt.Image @@ -18,7 +17,8 @@ import kotlin.io.path.name private val logger = KotlinLogging.logger {} @Service -class AlbumCacheService(private val albumRepository: AlbumRepository) { +class AlbumCacheService(private val albumRepository: AlbumRepository, + private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO) { suspend fun doAlbumCache() { @@ -32,7 +32,7 @@ class AlbumCacheService(private val albumRepository: AlbumRepository) { } } - fun getAlbumCache(): LinkedHashMap { + fun getAlbumCache(): LinkedHashMap { return albumRepository.albumCache } @@ -53,7 +53,7 @@ class AlbumCacheService(private val albumRepository: AlbumRepository) { coroutineScope { launch {getAndWriteAlbumImageFile(it.first, folderHash, folder, refresh)} } - Pair(folderHash, AlbumInfo(it.second.id3v2Tag.album, it.first.parent, folderHash)) + Pair(folderHash, AlbumMetadata(it.second.id3v2Tag.album, it.first.parent, folderHash)) } albumRepository.albumCache = convertAlbumListToMapForCache(list) @@ -79,7 +79,7 @@ class AlbumCacheService(private val albumRepository: AlbumRepository) { return compareBy({ it.second.id3v2Tag.albumArtist }, {it.second.id3v2Tag.album}, {it.second.id3v2Tag.year}) } - fun convertAlbumListToMapForCache(list: List>): java.util.LinkedHashMap { + fun convertAlbumListToMapForCache(list: List>): java.util.LinkedHashMap { val array = list.toTypedArray() return linkedMapOf(*array) } @@ -90,20 +90,26 @@ class AlbumCacheService(private val albumRepository: AlbumRepository) { if(refresh || !hashedFile.exists()) { logger.debug { "No image for $folder, making one..." } val imageByteStream = Mp3File(file).id3v2Tag.albumImage.inputStream() - val bufferedImage = ImageIO.read(imageByteStream) + val bufferedImage = withContext(ioDispatcher) { + ImageIO.read(imageByteStream) + } val scaledImage = bufferedImage.getScaledInstance(300, 300, Image.SCALE_DEFAULT) val outputImage = BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB) outputImage.graphics.drawImage(scaledImage, 0, 0, null) - ImageIO.write(outputImage, "jpg", hashedFile) + withContext(ioDispatcher) { + ImageIO.write(outputImage, "jpg", hashedFile) + } logger.trace { "Wrote new image file for $folder with hash $folderHash" } } } companion object { - val ALBUM_ROOT_DIR = System.getenv("ALBUM_ROOT_DIR") - ?: throw EnvironmentVariableNotFoundException("Environment variable `ALBUM_ROOT_DIR` does not exist, please specify.") //"q:\\CDs" - val ALBUM_ART_ROOT_DIR = System.getenv("ALBUM_ART_ROOT_DIR") - ?: throw EnvironmentVariableNotFoundException("Environment variable `ALBUM_ART_ROOT_DIR` does not exist, please specify.") //"src/main/resources/images/" + private fun getEnv(envName: String): String { + 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/" } } \ No newline at end of file