diff --git a/app/src/main/kotlin/com/hippo/ehviewer/spider/SpiderDen.kt b/app/src/main/kotlin/com/hippo/ehviewer/spider/SpiderDen.kt index 4bb8f8614..a522ca8da 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/spider/SpiderDen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/spider/SpiderDen.kt @@ -42,6 +42,7 @@ import com.hippo.ehviewer.util.FileUtils import com.hippo.ehviewer.util.sendTo import com.hippo.unifile.UniFile import com.hippo.unifile.asUniFile +import com.hippo.unifile.ensureDirOrThrow import com.hippo.unifile.openOutputStream import com.hippo.unifile.sha1 import eu.kanade.tachiyomi.util.system.logcat @@ -79,10 +80,10 @@ class SpiderDen(val info: GalleryInfo) { mode = value if (mode == SpiderQueen.MODE_DOWNLOAD) { if (downloadDir == null) { - downloadDir = getGalleryDownloadDir(gid)?.takeIf { it.ensureDir() } + downloadDir = getGalleryDownloadDir(gid)!!.apply { ensureDirOrThrow() } } if (saveAsCbz && tempDownloadDir == null) { - tempDownloadDir = info.tempDownloadDir?.takeIf { it.ensureDir() } + tempDownloadDir = info.tempDownloadDir!!.apply { ensureDirOrThrow() } } } } @@ -306,7 +307,7 @@ class SpiderDen(val info: GalleryInfo) { suspend fun initDownloadDir() { downloadDir = getGalleryDownloadDir(gid) ?: (downloadLocation / info.putToDownloadDir()) - check(downloadDir!!.ensureDir()) + downloadDir!!.ensureDirOrThrow() } suspend fun writeComicInfo(fetchMetadata: Boolean = true) { diff --git a/app/src/main/kotlin/com/hippo/ehviewer/util/FileUtils.kt b/app/src/main/kotlin/com/hippo/ehviewer/util/FileUtils.kt index 6dfa5bcec..f359708c4 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/util/FileUtils.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/util/FileUtils.kt @@ -118,10 +118,10 @@ object FileUtils { } } if (sizeInBytes > MAX_FILENAME_BYTES) { - do { + while (sizeInBytes > MAX_FILENAME_BYTES - 3) { deleteCharAt(length / 2) - } while (sizeInBytes >= MAX_FILENAME_BYTES) - insert(length / 2, '…') + } + insert(length / 2, "...") } } } diff --git a/app/src/main/kotlin/com/hippo/unifile/UniFileExtensions.kt b/app/src/main/kotlin/com/hippo/unifile/UniFileExtensions.kt index c6c726f03..d9b0c4dbf 100644 --- a/app/src/main/kotlin/com/hippo/unifile/UniFileExtensions.kt +++ b/app/src/main/kotlin/com/hippo/unifile/UniFileExtensions.kt @@ -46,3 +46,6 @@ val Uri.displayPath: String? } return toString() } + +fun UniFile.ensureDirOrThrow() = + check(ensureDir()) { "Failed to create directory: ${uri.displayPath}" }