From 46aeab9a7ac6fd5c45d426e3a733d716dcb1d25f Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 26 Nov 2023 15:56:43 -0500 Subject: [PATCH] Add extensions for handling UniFile name/file extensions --- .../eu/kanade/tachiyomi/data/download/DownloadCache.kt | 5 +++-- .../eu/kanade/tachiyomi/data/download/DownloadManager.kt | 3 ++- .../java/eu/kanade/tachiyomi/data/download/Downloader.kt | 3 ++- .../java/tachiyomi/core/storage/UniFileExtensions.kt | 9 +++++++++ 4 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/tachiyomi/core/storage/UniFileExtensions.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt index efeb9e416..9fe7dbfee 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt @@ -40,6 +40,8 @@ import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.protobuf.ProtoBuf import logcat.LogPriority +import tachiyomi.core.storage.extension +import tachiyomi.core.storage.nameWithoutExtension import tachiyomi.core.util.lang.launchIO import tachiyomi.core.util.lang.launchNonCancellable import tachiyomi.core.util.system.logcat @@ -351,8 +353,7 @@ class DownloadCache( // Folder of images it.isDirectory -> it.name // CBZ files - it.isFile && it.name?.endsWith(".cbz") == true -> - it.name!!.substringBeforeLast(".cbz") + it.isFile && it.extension == "cbz" -> it.nameWithoutExtension // Anything else is irrelevant else -> null } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt index 0b404165f..ba5c4d81a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.runBlocking import logcat.LogPriority import tachiyomi.core.i18n.stringResource +import tachiyomi.core.storage.extension import tachiyomi.core.util.lang.launchIO import tachiyomi.core.util.system.logcat import tachiyomi.domain.category.interactor.GetCategories @@ -340,7 +341,7 @@ class DownloadManager( .firstOrNull() ?: return var newName = provider.getChapterDirName(newChapter.name, newChapter.scanlator) - if (oldDownload.isFile && oldDownload.name?.endsWith(".cbz") == true) { + if (oldDownload.isFile && oldDownload.extension == "cbz") { newName += ".cbz" } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index f89146e3a..74a90b3ba 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -43,6 +43,7 @@ import okhttp3.Response import tachiyomi.core.i18n.stringResource import tachiyomi.core.metadata.comicinfo.COMIC_INFO_FILE import tachiyomi.core.metadata.comicinfo.ComicInfo +import tachiyomi.core.storage.extension import tachiyomi.core.util.lang.launchIO import tachiyomi.core.util.lang.launchNow import tachiyomi.core.util.lang.withIOContext @@ -353,7 +354,7 @@ class Downloader( // Delete all temporary (unfinished) files tmpDir.listFiles() - ?.filter { it.name!!.endsWith(".tmp") } + ?.filter { it.extension == "tmp" } ?.forEach { it.delete() } download.status = Download.State.DOWNLOADING diff --git a/core/src/main/java/tachiyomi/core/storage/UniFileExtensions.kt b/core/src/main/java/tachiyomi/core/storage/UniFileExtensions.kt new file mode 100644 index 000000000..9a869c883 --- /dev/null +++ b/core/src/main/java/tachiyomi/core/storage/UniFileExtensions.kt @@ -0,0 +1,9 @@ +package tachiyomi.core.storage + +import com.hippo.unifile.UniFile + +val UniFile.extension: String? + get() = name?.substringAfterLast('.') + +val UniFile.nameWithoutExtension: String? + get() = name?.substringBeforeLast('.')