From 36532050eb0897b185e067cb4b2e72fc572317fc Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sun, 19 Nov 2023 11:37:36 +0100 Subject: [PATCH] Add mutation to clear the cached images --- .../graphql/mutations/ImageMutation.kt | 57 +++++++++++++++++++ .../graphql/server/TachideskGraphQLSchema.kt | 2 + .../manga/impl/util/storage/ImageResponse.kt | 4 ++ 3 files changed, 63 insertions(+) create mode 100644 server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ImageMutation.kt diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ImageMutation.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ImageMutation.kt new file mode 100644 index 0000000000..1b0269ace3 --- /dev/null +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ImageMutation.kt @@ -0,0 +1,57 @@ +package suwayomi.tachidesk.graphql.mutations + +import org.kodein.di.DI +import org.kodein.di.conf.global +import org.kodein.di.instance +import suwayomi.tachidesk.manga.impl.util.storage.ImageResponse +import suwayomi.tachidesk.server.ApplicationDirs + +private val applicationDirs by DI.global.instance() + +class ImageMutation { + data class ClearCachedImagesInput( + val clientMutationId: String? = null, + val downloadedThumbnails: Boolean? = null, + val cachedThumbnails: Boolean? = null, + val cachedPages: Boolean? = null, + ) + + data class ClearCachedImagesPayload( + val clientMutationId: String? = null, + val downloadedThumbnails: Boolean?, + val cachedThumbnails: Boolean?, + val cachedPages: Boolean?, + ) + + fun clearCachedImages(input: ClearCachedImagesInput): ClearCachedImagesPayload { + val (clientMutationId, downloadedThumbnails, cachedThumbnails, cachedPages) = input + + val downloadedThumbnailsResult = + if (downloadedThumbnails == true) { + ImageResponse.clearImages(applicationDirs.thumbnailDownloadsRoot) + } else { + null + } + + val cachedThumbnailsResult = + if (cachedThumbnails == true) { + ImageResponse.clearImages(applicationDirs.tempThumbnailCacheRoot) + } else { + null + } + + val cachedPagesResult = + if (cachedPages == true) { + ImageResponse.clearImages(applicationDirs.tempMangaCacheRoot) + } else { + null + } + + return ClearCachedImagesPayload( + clientMutationId, + downloadedThumbnails = downloadedThumbnailsResult, + cachedThumbnails = cachedThumbnailsResult, + cachedPages = cachedPagesResult, + ) + } +} diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/TachideskGraphQLSchema.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/TachideskGraphQLSchema.kt index b0d3504821..63db0a46f2 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/TachideskGraphQLSchema.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/TachideskGraphQLSchema.kt @@ -18,6 +18,7 @@ import suwayomi.tachidesk.graphql.mutations.CategoryMutation import suwayomi.tachidesk.graphql.mutations.ChapterMutation import suwayomi.tachidesk.graphql.mutations.DownloadMutation import suwayomi.tachidesk.graphql.mutations.ExtensionMutation +import suwayomi.tachidesk.graphql.mutations.ImageMutation import suwayomi.tachidesk.graphql.mutations.InfoMutation import suwayomi.tachidesk.graphql.mutations.MangaMutation import suwayomi.tachidesk.graphql.mutations.MetaMutation @@ -84,6 +85,7 @@ val schema = TopLevelObject(ChapterMutation()), TopLevelObject(DownloadMutation()), TopLevelObject(ExtensionMutation()), + TopLevelObject(ImageMutation()), TopLevelObject(InfoMutation()), TopLevelObject(MangaMutation()), TopLevelObject(MetaMutation()), diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/storage/ImageResponse.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/storage/ImageResponse.kt index 8776a46281..53326fe1f6 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/storage/ImageResponse.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/storage/ImageResponse.kt @@ -114,4 +114,8 @@ object ImageResponse { File(it).delete() } } + + fun clearImages(saveDir: String): Boolean { + return File(saveDir).deleteRecursively() + } }