-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move downloader logic outside of caching/image download logic
- Loading branch information
Showing
7 changed files
with
106 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
server/src/main/kotlin/suwayomi/tachidesk/manga/impl/ChapterDownloadHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package suwayomi.tachidesk.manga.impl | ||
|
||
import suwayomi.tachidesk.manga.impl.download.DownloadedFilesProvider | ||
import suwayomi.tachidesk.manga.impl.download.FolderProvider | ||
import java.io.InputStream | ||
|
||
object ChapterDownloadHelper { | ||
fun getImage(mangaId: Int, chapterId: Int, index: Int): Pair<InputStream, String> { | ||
return provider(mangaId, chapterId).getImage(index) | ||
} | ||
|
||
fun delete(mangaId: Int, chapterId: Int): Boolean { | ||
return provider(mangaId, chapterId).delete() | ||
} | ||
|
||
fun putImage(mangaId: Int, chapterId: Int, index: Int, image: InputStream): Boolean { | ||
return provider(mangaId, chapterId).putImage(index, image) | ||
} | ||
|
||
// return the appropriate provider based on how the download was saved. For the logic is simple but will evolve when new types of downloads are available | ||
private fun provider(mangaId: Int, chapterId: Int): DownloadedFilesProvider { | ||
return FolderProvider(mangaId, chapterId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
server/src/main/kotlin/suwayomi/tachidesk/manga/impl/download/DownloadedFilesProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package suwayomi.tachidesk.manga.impl.download | ||
|
||
import java.io.InputStream | ||
|
||
/* | ||
* Base class for downloaded chapter files provider, example: Folder, Archive | ||
* */ | ||
abstract class DownloadedFilesProvider(val mangaId: Int, val chapterId: Int) { | ||
abstract fun getImage(index: Int): Pair<InputStream, String> | ||
|
||
abstract fun putImage(index: Int, image: InputStream): Boolean | ||
|
||
abstract fun delete(): Boolean | ||
|
||
private fun getPageName(index: Int): String { | ||
return String.format("%03d", index + 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
server/src/main/kotlin/suwayomi/tachidesk/manga/impl/download/FolderProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package suwayomi.tachidesk.manga.impl.download | ||
|
||
import suwayomi.tachidesk.manga.impl.Page.getPageName | ||
import suwayomi.tachidesk.manga.impl.util.getChapterDir | ||
import suwayomi.tachidesk.manga.impl.util.storage.ImageResponse | ||
import java.io.File | ||
import java.io.FileInputStream | ||
import java.io.InputStream | ||
|
||
/* | ||
* Provides downloaded files when pages were downloaded into folders | ||
* */ | ||
class FolderProvider(mangaId: Int, chapterId: Int) : DownloadedFilesProvider(mangaId, chapterId) { | ||
override fun getImage(index: Int): Pair<InputStream, String> { | ||
val chapterDir = getChapterDir(mangaId, chapterId) | ||
val folder = File(chapterDir) | ||
folder.mkdirs() | ||
val file = folder.listFiles()?.get(index) | ||
val fileType = file!!.name.substringAfterLast(".") | ||
return Pair(FileInputStream(file).buffered(), "image/$fileType") | ||
} | ||
|
||
override fun putImage(index: Int, image: InputStream): Boolean { | ||
val chapterDir = getChapterDir(mangaId, chapterId) | ||
val folder = File(chapterDir) | ||
folder.mkdirs() | ||
val fileName = getPageName(index) | ||
val filePath = "$chapterDir/$fileName" | ||
ImageResponse.saveImage(filePath, image) | ||
return true | ||
} | ||
|
||
override fun delete(): Boolean { | ||
val chapterDir = getChapterDir(mangaId, chapterId) | ||
return File(chapterDir).deleteRecursively() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters