|
| 1 | +package suwayomi.tachidesk.manga.impl.download |
| 2 | + |
| 3 | +import kotlinx.coroutines.CoroutineScope |
| 4 | +import kotlinx.coroutines.Dispatchers |
| 5 | +import kotlinx.coroutines.withContext |
| 6 | +import suwayomi.tachidesk.manga.impl.download.model.DownloadChapter |
| 7 | +import suwayomi.tachidesk.manga.impl.util.getChapterCbzPath |
| 8 | +import suwayomi.tachidesk.manga.impl.util.getChapterDownloadPath |
| 9 | +import java.io.File |
| 10 | +import java.io.InputStream |
| 11 | +import java.util.zip.ZipEntry |
| 12 | +import java.util.zip.ZipFile |
| 13 | +import java.util.zip.ZipInputStream |
| 14 | +import java.util.zip.ZipOutputStream |
| 15 | + |
| 16 | +class ArchiveProvider(mangaId: Int, chapterId: Int) : DownloadedFilesProvider(mangaId, chapterId) { |
| 17 | + override fun getImage(index: Int): Pair<InputStream, String> { |
| 18 | + val cbzPath = getChapterCbzPath(mangaId, chapterId) |
| 19 | + val zipFile = ZipFile(cbzPath) |
| 20 | + val zipEntry = zipFile.entries().toList().sortedWith(compareBy({ it.name }, { it.name }))[index] |
| 21 | + val inputStream = zipFile.getInputStream(zipEntry) |
| 22 | + val fileType = zipEntry.name.substringAfterLast(".") |
| 23 | + return Pair(inputStream.buffered(), "image/$fileType") |
| 24 | + } |
| 25 | + |
| 26 | + override suspend fun download( |
| 27 | + download: DownloadChapter, |
| 28 | + scope: CoroutineScope, |
| 29 | + step: suspend (DownloadChapter?, Boolean) -> Unit |
| 30 | + ): Boolean { |
| 31 | + val chapterDir = getChapterDownloadPath(mangaId, chapterId) |
| 32 | + val outputFile = File(getChapterCbzPath(mangaId, chapterId)) |
| 33 | + val chapterFolder = File(chapterDir) |
| 34 | + if (outputFile.exists()) handleExistingCbzFile(outputFile, chapterFolder) |
| 35 | + |
| 36 | + withContext(Dispatchers.IO) { |
| 37 | + outputFile.createNewFile() |
| 38 | + } |
| 39 | + |
| 40 | + FolderProvider(mangaId, chapterId).download(download, scope, step) |
| 41 | + |
| 42 | + ZipOutputStream(outputFile.outputStream()).use { zipOut -> |
| 43 | + if (chapterFolder.isDirectory) { |
| 44 | + chapterFolder.listFiles()?.sortedBy { it.name }?.forEach { |
| 45 | + val entry = ZipEntry(it.name) |
| 46 | + try { |
| 47 | + zipOut.putNextEntry(entry) |
| 48 | + it.inputStream().use { inputStream -> |
| 49 | + inputStream.copyTo(zipOut) |
| 50 | + } |
| 51 | + } finally { |
| 52 | + zipOut.closeEntry() |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (chapterFolder.exists() && chapterFolder.isDirectory) { |
| 59 | + chapterFolder.deleteRecursively() |
| 60 | + } |
| 61 | + |
| 62 | + return true |
| 63 | + } |
| 64 | + |
| 65 | + override fun delete(): Boolean { |
| 66 | + val cbzFile = File(getChapterCbzPath(mangaId, chapterId)) |
| 67 | + if (cbzFile.exists()) return cbzFile.delete() |
| 68 | + return false |
| 69 | + } |
| 70 | + |
| 71 | + private fun handleExistingCbzFile(cbzFile: File, chapterFolder: File) { |
| 72 | + if (!chapterFolder.exists()) chapterFolder.mkdirs() |
| 73 | + ZipInputStream(cbzFile.inputStream()).use { zipInputStream -> |
| 74 | + var zipEntry = zipInputStream.nextEntry |
| 75 | + while (zipEntry != null) { |
| 76 | + val file = File(chapterFolder, zipEntry.name) |
| 77 | + if (!file.exists()) { |
| 78 | + file.parentFile.mkdirs() |
| 79 | + file.createNewFile() |
| 80 | + } |
| 81 | + file.outputStream().use { outputStream -> |
| 82 | + zipInputStream.copyTo(outputStream) |
| 83 | + } |
| 84 | + zipEntry = zipInputStream.nextEntry |
| 85 | + } |
| 86 | + } |
| 87 | + cbzFile.delete() |
| 88 | + } |
| 89 | +} |
0 commit comments