Revamps the album cache service to make it comply with SonarLint. Also adds and renames some models.
This commit is contained in:
parent
0404cff354
commit
b1e997430b
@ -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
|
||||
}
|
||||
@ -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<AlbumTrackInfo>
|
||||
)
|
||||
|
||||
@ -0,0 +1,7 @@
|
||||
package net.halfbinary.prettyplayerapi.model
|
||||
|
||||
data class AlbumTrackInfo(
|
||||
val trackNumber: Int,
|
||||
val trackArtist: String,
|
||||
val trackTitle: String
|
||||
)
|
||||
@ -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<String, AlbumInfo> {
|
||||
fun getAlbumCache(): LinkedHashMap<String, AlbumMetadata> {
|
||||
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<Pair<String, AlbumInfo>>): java.util.LinkedHashMap<String, AlbumInfo> {
|
||||
fun convertAlbumListToMapForCache(list: List<Pair<String, AlbumMetadata>>): java.util.LinkedHashMap<String, AlbumMetadata> {
|
||||
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/"
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user