Cleanup [Downloader.ensureSuccessfulDownload] (#8602)

This commit is contained in:
AntsyLich 2022-11-23 20:23:29 +06:00 committed by GitHub
parent acc2312384
commit 0347d3970a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 16 deletions

View file

@ -28,6 +28,7 @@ import eu.kanade.tachiyomi.util.lang.launchNow
import eu.kanade.tachiyomi.util.lang.plusAssign import eu.kanade.tachiyomi.util.lang.plusAssign
import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.lang.withUIContext
import eu.kanade.tachiyomi.util.storage.DiskUtil import eu.kanade.tachiyomi.util.storage.DiskUtil
import eu.kanade.tachiyomi.util.storage.DiskUtil.NOMEDIA_FILE
import eu.kanade.tachiyomi.util.storage.saveTo import eu.kanade.tachiyomi.util.storage.saveTo
import eu.kanade.tachiyomi.util.system.ImageUtil import eu.kanade.tachiyomi.util.system.ImageUtil
import eu.kanade.tachiyomi.util.system.logcat import eu.kanade.tachiyomi.util.system.logcat
@ -389,7 +390,7 @@ class Downloader(
tmpFile?.delete() tmpFile?.delete()
// Try to find the image file. // Try to find the image file.
val imageFile = tmpDir.listFiles()!!.find { it.name!!.startsWith("$filename.") || it.name!!.contains("${filename}__001") } val imageFile = tmpDir.listFiles()?.firstOrNull { it.name!!.startsWith("$filename.") || it.name!!.startsWith("${filename}__001") }
// If the image is already downloaded, do nothing. Otherwise download from network // If the image is already downloaded, do nothing. Otherwise download from network
val pageObservable = when { val pageObservable = when {
@ -496,8 +497,8 @@ class Downloader(
val imageFile = tmpDir.listFiles()?.firstOrNull { it.name.orEmpty().startsWith(filenamePrefix) } val imageFile = tmpDir.listFiles()?.firstOrNull { it.name.orEmpty().startsWith(filenamePrefix) }
?: throw Error(context.getString(R.string.download_notifier_split_page_not_found, page.number)) ?: throw Error(context.getString(R.string.download_notifier_split_page_not_found, page.number))
// check if the original page was previously splitted before then skip. // Check if the original page was previously splitted before then skip.
if (imageFile.name!!.contains("__")) return true if (imageFile.name.orEmpty().startsWith("${filenamePrefix}__")) return true
return try { return try {
ImageUtil.splitTallImage(tmpDir, imageFile, filenamePrefix) ImageUtil.splitTallImage(tmpDir, imageFile, filenamePrefix)
@ -521,18 +522,30 @@ class Downloader(
tmpDir: UniFile, tmpDir: UniFile,
dirname: String, dirname: String,
) { ) {
// Ensure that the chapter folder has all the images. // Page list hasn't been initialized
val downloadedImages = tmpDir.listFiles().orEmpty().filterNot { it.name!!.endsWith(".tmp") || (it.name!!.contains("__") && !it.name!!.contains("__001.jpg")) } val downloadPageCount = download.pages?.size ?: return
// Ensure that all pages has been downloaded
if (download.downloadedImages < downloadPageCount) return
// Ensure that the chapter folder has all the pages.
val downloadedImagesCount = tmpDir.listFiles().orEmpty().count {
val fileName = it.name.orEmpty()
when {
fileName in listOf(COMIC_INFO_FILE, NOMEDIA_FILE) -> false
fileName.endsWith(".tmp") -> false
// Only count the first split page and not the others
fileName.contains("__") && !fileName.endsWith("__001.jpg") -> false
else -> true
}
}
val chapterUrl = download.source.getChapterUrl(download.chapter) download.status = if (downloadedImagesCount == downloadPageCount) {
createComicInfoFile( val chapterUrl = download.source.getChapterUrl(download.chapter)
tmpDir, createComicInfoFile(
download.manga, tmpDir,
download.chapter.toDomainChapter()!!, download.manga,
chapterUrl, download.chapter.toDomainChapter()!!,
) chapterUrl,
)
download.status = if (downloadedImages.size == download.pages!!.size) {
// Only rename the directory if it's downloaded. // Only rename the directory if it's downloaded.
if (downloadPreferences.saveChaptersAsCBZ().get()) { if (downloadPreferences.saveChaptersAsCBZ().get()) {
archiveChapter(mangaDir, dirname, tmpDir) archiveChapter(mangaDir, dirname, tmpDir)

View file

@ -62,9 +62,9 @@ object DiskUtil {
*/ */
fun createNoMediaFile(dir: UniFile?, context: Context?) { fun createNoMediaFile(dir: UniFile?, context: Context?) {
if (dir != null && dir.exists()) { if (dir != null && dir.exists()) {
val nomedia = dir.findFile(".nomedia") val nomedia = dir.findFile(NOMEDIA_FILE)
if (nomedia == null) { if (nomedia == null) {
dir.createFile(".nomedia") dir.createFile(NOMEDIA_FILE)
context?.let { scanMedia(it, dir.uri) } context?.let { scanMedia(it, dir.uri) }
} }
} }
@ -112,4 +112,6 @@ object DiskUtil {
else -> true else -> true
} }
} }
const val NOMEDIA_FILE = ".nomedia"
} }