diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/CacheController.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/CacheController.kt index 47213a0..579ae3b 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/CacheController.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/CacheController.kt @@ -1,15 +1,22 @@ package net.halfbinary.prettyplayerapi.controller +import net.halfbinary.prettyplayerapi.model.CacheType import net.halfbinary.prettyplayerapi.service.AlbumCacheService 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 @RequestMapping("cache") class CacheController(private val albumCacheService: AlbumCacheService) { @GetMapping - suspend fun refreshAlbumCache() { - albumCacheService.refreshAlbumCache() + suspend fun refreshAlbumCache(@RequestParam type: CacheType) { + when(type) { + CacheType.ART -> albumCacheService.doAlbumCacheOnlyNewArt() + CacheType.NEW -> albumCacheService.doAddNewAlbumCache() // TODO: This will make more sense with a DB backing + CacheType.ALL -> albumCacheService.refreshAlbumCache() + } + } } \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/model/CacheType.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/model/CacheType.kt new file mode 100644 index 0000000..121876b --- /dev/null +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/model/CacheType.kt @@ -0,0 +1,7 @@ +package net.halfbinary.prettyplayerapi.model + +enum class CacheType { + ART, + NEW, + ALL +} \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumCacheService.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumCacheService.kt index 44d55b3..6ea051e 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumCacheService.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/AlbumCacheService.kt @@ -5,6 +5,7 @@ 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.AlbumRepository import org.springframework.stereotype.Service import java.awt.Image @@ -21,14 +22,20 @@ class AlbumCacheService(private val albumRepository: AlbumRepository, private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO) { - suspend fun doAlbumCache() { + suspend fun doAlbumCacheOnlyNewArt() { coroutineScope { - launch { cacheAlbums(false) } + launch { cacheAlbums(CacheType.ART) } } } suspend fun refreshAlbumCache() { coroutineScope{ - launch { cacheAlbums(true)} + launch { cacheAlbums(CacheType.ALL)} + } + } + + suspend fun doAddNewAlbumCache() { + coroutineScope { + launch { cacheAlbums(CacheType.NEW) } } } @@ -36,8 +43,8 @@ class AlbumCacheService(private val albumRepository: AlbumRepository, return albumRepository.albumCache } - suspend fun cacheAlbums(refresh: Boolean = false) { - logger.debug { "Starting the cache process with refresh set to $refresh" } + suspend fun cacheAlbums(type: CacheType = CacheType.ART) { + logger.debug { "Starting the cache process with type ${type.name}" } createAlbumArtRootDir() val list = getAlbumRootDir() .walkTopDown() @@ -50,6 +57,7 @@ class AlbumCacheService(private val albumRepository: AlbumRepository, .map { val folder = it.first.parentFile.toPath() val folderHash = folder.name.hashCode().toString() + val refresh = type == CacheType.ALL coroutineScope { launch {getAndWriteAlbumImageFile(it.first, folderHash, folder, refresh)} } diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/StartupService.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/StartupService.kt index ea134f5..d28c8ea 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/service/StartupService.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/service/StartupService.kt @@ -17,7 +17,7 @@ class StartupService(private val albumCacheService: AlbumCacheService) { suspend fun onStartup() { coroutineScope { launch { - albumCacheService.doAlbumCache() + albumCacheService.doAlbumCacheOnlyNewArt() } } }