From 6e4a30e59352517346eb0f622424390a52fb95c0 Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Fri, 28 Oct 2022 21:40:43 +0600 Subject: [PATCH] Fix "Download split" not working while using SD card (#8305) * Fix "Download split" not working while using SD card * Update app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt Co-authored-by: arkon --- .../tachiyomi/data/download/Downloader.kt | 8 +++----- .../kanade/tachiyomi/util/system/ImageUtil.kt | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 14 deletions(-) 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 f70d4c65e..584ebcef3 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 @@ -485,17 +485,15 @@ class Downloader( private fun splitTallImageIfNeeded(page: Page, tmpDir: UniFile): Boolean { if (!downloadPreferences.splitTallImages().get()) return true - val filename = String.format("%03d", page.number) - val imageFile = tmpDir.listFiles()?.find { it.name!!.startsWith(filename) } + val filenamePrefix = String.format("%03d", page.number) + val imageFile = tmpDir.listFiles()?.firstOrNull { it.name.orEmpty().startsWith(filenamePrefix) } ?: throw Error(context.getString(R.string.download_notifier_split_page_not_found, page.number)) - val imageFilePath = imageFile.filePath - ?: throw Error(context.getString(R.string.download_notifier_split_page_path_not_found, page.number)) // check if the original page was previously splitted before then skip. if (imageFile.name!!.contains("__")) return true return try { - ImageUtil.splitTallImage(imageFile, imageFilePath) + ImageUtil.splitTallImage(tmpDir, imageFile, filenamePrefix) } catch (e: Exception) { logcat(LogPriority.ERROR, e) false diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt index b27523a99..66db84578 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt @@ -27,8 +27,6 @@ import tachiyomi.decoder.ImageDecoder import java.io.BufferedInputStream import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream -import java.io.File -import java.io.FileOutputStream import java.io.InputStream import java.net.URLConnection import kotlin.math.abs @@ -202,7 +200,7 @@ object ImageUtil { /** * Splits tall images to improve performance of reader */ - fun splitTallImage(imageFile: UniFile, imageFilePath: String): Boolean { + fun splitTallImage(tmpDir: UniFile, imageFile: UniFile, filenamePrefix: String): Boolean { if (isAnimatedAndSupported(imageFile.openInputStream()) || !isTallImage(imageFile.openInputStream())) { return true } @@ -221,11 +219,15 @@ object ImageUtil { return try { splitDataList.forEach { splitData -> - val splitPath = splitImagePath(imageFilePath, splitData.index) + val splitImageName = splitImageName(filenamePrefix, splitData.index) + // Remove pre-existing split if exists (this split shouldn't exist under normal circumstances) + tmpDir.findFile(splitImageName)?.delete() + + val splitFile = tmpDir.createFile(splitImageName) val region = Rect(0, splitData.topOffset, splitData.splitWidth, splitData.bottomOffset) - FileOutputStream(splitPath).use { outputStream -> + splitFile.openOutputStream().use { outputStream -> val splitBitmap = bitmapRegionDecoder.decodeRegion(region, options) splitBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream) splitBitmap.recycle() @@ -240,8 +242,8 @@ object ImageUtil { } catch (e: Exception) { // Image splits were not successfully saved so delete them and keep the original image splitDataList - .map { splitImagePath(imageFilePath, it.index) } - .forEach { File(it).delete() } + .map { splitImageName(filenamePrefix, it.index) } + .forEach { tmpDir.findFile(it)?.delete() } logcat(LogPriority.ERROR, e) false } finally { @@ -249,8 +251,7 @@ object ImageUtil { } } - private fun splitImagePath(imageFilePath: String, index: Int) = - imageFilePath.substringBeforeLast(".") + "__${"%03d".format(index + 1)}.jpg" + private fun splitImageName(filenamePrefix: String, index: Int) = "${filenamePrefix}__${"%03d".format(index + 1)}.jpg" /** * Check whether the image is a long Strip that needs splitting