refactor(downloader): Extract ensureSuccessfulDownload

Also double-bang archiveChapter, since exceptions already being catch by
`downloadChapter`
This commit is contained in:
Ahmad Ansori Palembani 2024-12-19 08:08:59 +07:00
parent 985ac6d7a8
commit cd8ff6f898
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6

View file

@ -389,7 +389,30 @@ class Downloader(
} }
// Do after download completes // Do after download completes
ensureSuccessfulDownload(download, mangaDir, tmpDir, chapterDirname)
if (!isDownloadSuccessful(download, tmpDir)) {
download.status = Download.State.ERROR
return
}
createComicInfoFile(
tmpDir,
download.manga,
download.chapter,
download.source,
)
// Only rename the directory if it's downloaded
if (preferences.saveChaptersAsCBZ().get()) {
archiveChapter(mangaDir, chapterDirname, tmpDir)
} else {
tmpDir.renameTo(chapterDirname)
}
cache.addChapter(chapterDirname, mangaDir, download.manga)
DiskUtil.createNoMediaFile(tmpDir, context)
download.status = Download.State.DOWNLOADED
} catch (error: Throwable) { } catch (error: Throwable) {
if (error is CancellationException) throw error if (error is CancellationException) throw error
// If the page list threw, it will resume here // If the page list threw, it will resume here
@ -399,6 +422,31 @@ class Downloader(
} }
} }
private fun isDownloadSuccessful(
download: Download,
tmpDir: UniFile,
): Boolean {
// Page list hasn't been initialized
val downloadPageCount = download.pages?.size ?: return false
// Ensure that all pages has been downloaded
if (download.downloadedImages != downloadPageCount) return false
// 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
}
}
return downloadedImagesCount == downloadPageCount
}
/** /**
* Returns the observable which gets the image from the filesystem if it exists or downloads it * Returns the observable which gets the image from the filesystem if it exists or downloads it
* otherwise. * otherwise.
@ -549,60 +597,6 @@ class Downloader(
} }
} }
/**
* Checks if the download was successful.
*
* @param download the download to check.
* @param mangaDir the manga directory of the download.
* @param tmpDir the directory where the download is currently stored.
* @param dirname the real (non temporary) directory name of the download.
*/
private suspend fun ensureSuccessfulDownload(
download: Download,
mangaDir: UniFile,
tmpDir: UniFile,
dirname: String,
) {
// Page list hasn't been initialized
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
}
}
download.status = if (downloadedImagesCount == downloadPageCount) {
createComicInfoFile(
tmpDir,
download.manga,
download.chapter,
download.source,
)
// Only rename the directory if it's downloaded
if (preferences.saveChaptersAsCBZ().get()) {
archiveChapter(mangaDir, dirname, tmpDir)
} else {
tmpDir.renameTo(dirname)
}
cache.addChapter(dirname, mangaDir, download.manga)
DiskUtil.createNoMediaFile(tmpDir, context)
Download.State.DOWNLOADED
} else {
Download.State.ERROR
}
}
/** /**
* Archive the chapter pages as a CBZ. * Archive the chapter pages as a CBZ.
*/ */
@ -611,7 +605,7 @@ class Downloader(
dirname: String, dirname: String,
tmpDir: UniFile, tmpDir: UniFile,
) { ) {
val zip = mangaDir.createFile("$dirname.cbz$TMP_DIR_SUFFIX") ?: return val zip = mangaDir.createFile("$dirname.cbz$TMP_DIR_SUFFIX")!!
ZipWriter(context, zip).use { writer -> ZipWriter(context, zip).use { writer ->
tmpDir.listFiles()?.forEach { file -> tmpDir.listFiles()?.forEach { file ->
writer.write(file) writer.write(file)