|
| 1 | +package suwayomi.tachidesk.manga.impl.download |
| 2 | + |
| 3 | +import suwayomi.tachidesk.manga.impl.Page.getPageName |
| 4 | +import suwayomi.tachidesk.manga.impl.util.getChapterDir |
| 5 | +import suwayomi.tachidesk.manga.impl.util.storage.ImageUtil |
| 6 | +import java.io.File |
| 7 | +import java.io.FileInputStream |
| 8 | +import java.io.InputStream |
| 9 | + |
| 10 | +/* |
| 11 | +* Provides downloaded files when pages were downloaded into folders |
| 12 | +* */ |
| 13 | +class FolderProvider(mangaId: Int, chapterId: Int) : DownloadedFilesProvider(mangaId, chapterId) { |
| 14 | + override fun getImage(index: Int): Pair<InputStream, String> { |
| 15 | + val chapterDir = getChapterDir(mangaId, chapterId) |
| 16 | + val folder = File(chapterDir) |
| 17 | + folder.mkdirs() |
| 18 | + val file = folder.listFiles()?.get(index) |
| 19 | + val fileType = file!!.name.substringAfterLast(".") |
| 20 | + return Pair(FileInputStream(file).buffered(), "image/$fileType") |
| 21 | + } |
| 22 | + |
| 23 | + override fun putImage(index: Int, image: InputStream): Boolean { |
| 24 | + val chapterDir = getChapterDir(mangaId, chapterId) |
| 25 | + val fileName = getPageName(index) |
| 26 | + val filePath = "$chapterDir/$fileName" |
| 27 | + val tmpSavePath = "$filePath.tmp" |
| 28 | + val tmpSaveFile = File(tmpSavePath) |
| 29 | + image.use { input -> tmpSaveFile.outputStream().use { output -> input.copyTo(output) } } |
| 30 | + |
| 31 | + // find image type |
| 32 | + val imageType = ImageUtil.findImageType { tmpSaveFile.inputStream() }?.mime |
| 33 | + ?: "image/jpeg" |
| 34 | + |
| 35 | + val actualSavePath = "$filePath.${imageType.substringAfter("/")}" |
| 36 | + |
| 37 | + tmpSaveFile.renameTo(File(actualSavePath)) |
| 38 | + return true |
| 39 | + } |
| 40 | + |
| 41 | + override fun delete(): Boolean { |
| 42 | + val chapterDir = getChapterDir(mangaId, chapterId) |
| 43 | + return File(chapterDir).deleteRecursively() |
| 44 | + } |
| 45 | +} |
0 commit comments