From 850c72197a4fe1c354b374c1a877ffba69eb0260 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Thu, 12 Sep 2024 15:59:58 +0200 Subject: [PATCH] fix: remove image not in database --- .../controllers/admin/AdminController.kt | 6 ----- .../fr/shikkanime/services/ImageService.kt | 27 +++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/fr/shikkanime/controllers/admin/AdminController.kt b/src/main/kotlin/fr/shikkanime/controllers/admin/AdminController.kt index 66d92c86..7584bd51 100644 --- a/src/main/kotlin/fr/shikkanime/controllers/admin/AdminController.kt +++ b/src/main/kotlin/fr/shikkanime/controllers/admin/AdminController.kt @@ -36,12 +36,6 @@ class AdminController { @Inject private lateinit var animeService: AnimeService - @Inject - private lateinit var episodeMappingService: EpisodeMappingService - - @Inject - private lateinit var episodeVariantService: EpisodeVariantService - @Path @Get private fun home(@QueryParam("error") error: String?): Response { diff --git a/src/main/kotlin/fr/shikkanime/services/ImageService.kt b/src/main/kotlin/fr/shikkanime/services/ImageService.kt index 212880a1..a9f307a0 100644 --- a/src/main/kotlin/fr/shikkanime/services/ImageService.kt +++ b/src/main/kotlin/fr/shikkanime/services/ImageService.kt @@ -305,6 +305,33 @@ object ImageService { get() = toHumanReadable(cache.toList().sumOf { it.size }) fun invalidate() { + Constant.injector.getInstance(Database::class.java).entityManager.use { + val query = it.createNativeQuery( + """ + SELECT uuid + FROM anime + UNION + SELECT uuid + FROM episode_mapping + UNION + SELECT uuid + FROM member + """, + UUID::class.java + ) + + val uuids = (query.resultList as List) // NOSONAR + .map { uuid -> uuid.toString() } + .toSet() + + // Calculate the difference between the cache and the UUIDs + val difference = cache.filter { img -> img.uuid !in uuids } + logger.warning("Removing ${difference.size} images from cache, not found in database") + logger.warning("${toHumanReadable(difference.sumOf { img -> img.size })} will be freed") + + cache.removeIf { img -> img.uuid !in uuids } + } + addAll(true) }