Cleanup Save as CBZ

This commit is contained in:
Jobobby04
2020-08-22 18:08:07 -04:00
parent 79e4e3d2a0
commit 3967a569c4
4 changed files with 19 additions and 15 deletions
@@ -81,7 +81,7 @@ class DownloadCache(
if (sourceDir != null) {
val mangaDir = sourceDir.files[provider.getMangaDirName(manga)]
if (mangaDir != null) {
return provider.getValidChapterDirNames(chapter).any { it in mangaDir.files }
return provider.getValidChapterDirNames(chapter).any { it in mangaDir.files || "$it.cbz" in mangaDir.files }
}
}
return false
@@ -196,6 +196,8 @@ class DownloadCache(
provider.getValidChapterDirNames(chapter).forEach {
if (it in mangaDir.files) {
mangaDir.files -= it
} else if ("$it.cbz" in mangaDir.files) {
mangaDir.files -= "$it.cbz"
}
}
}
@@ -226,6 +228,8 @@ class DownloadCache(
provider.getValidChapterDirNames(chapter).forEach {
if (it in mangaDir.files) {
mangaDir.files -= it
} else if ("$it.cbz" in mangaDir.files) {
mangaDir.files -= "$it.cbz"
}
}
}
@@ -287,12 +287,12 @@ class DownloadManager(/* SY private */ val context: Context) {
// Assume there's only 1 version of the chapter name formats present
val oldFolder = oldNames.asSequence()
.mapNotNull { mangaDir.findFile(it) }
.mapNotNull { mangaDir.findFile(it) ?: mangaDir.findFile("$it.cbz") }
.firstOrNull()
if (oldFolder?.renameTo(newName) == true) {
if (oldFolder?.renameTo(newName + if (oldFolder.name?.endsWith(".cbz") == true) ".cbz" else "") == true) {
cache.removeChapter(oldChapter, manga)
cache.addChapter(newName, mangaDir, manga)
cache.addChapter(newName + if (oldFolder.name?.endsWith(".cbz") == true) ".cbz" else "", mangaDir, manga)
} else {
Timber.e("Could not rename downloaded chapter: %s.", oldNames.joinToString())
}
@@ -60,7 +60,7 @@ class Downloader(
private val sourceManager: SourceManager
) {
private val preferences: PreferencesHelper = Injekt.get()
private val preferences: PreferencesHelper by injectLazy()
private val chapterCache: ChapterCache by injectLazy()
@@ -473,14 +473,14 @@ class Downloader(
// Only rename the directory if it's downloaded.
if (download.status == Download.DOWNLOADED) {
mangaDir.findFile(dirname + ".tmp")?.delete()
if (preferences.saveChaptersAsCBZ().get()) {
val zip = mangaDir.createFile(dirname + ".tmp")
val zip = mangaDir.createFile("$dirname.cbz.tmp")
val zipOut = ZipOutputStream(BufferedOutputStream(zip.openOutputStream()))
val compressionLevel = preferences.saveChaptersAsCBZLevel().get()
zipOut.setLevel(preferences.saveChaptersAsCBZLevel().get())
zipOut.setLevel(compressionLevel)
if (preferences.saveChaptersAsCBZLevel().get() == 0) {
if (compressionLevel == 0) {
zipOut.setMethod(ZipEntry.STORED)
}
@@ -488,7 +488,7 @@ class Downloader(
val input = img.openInputStream()
val data = input.readBytes()
val entry = ZipEntry(img.name)
if (preferences.saveChaptersAsCBZLevel().get() == 0) {
if (compressionLevel == 0) {
val crc = CRC32()
val size = img.length()
crc.update(data)
@@ -501,7 +501,7 @@ class Downloader(
input.close()
}
zipOut.close()
zip.renameTo(dirname + ".cbz")
zip.renameTo("$dirname.cbz")
tmpDir.delete()
} else {
tmpDir.renameTo(dirname)