|
| 1 | +package com.melonhead.lib_chapter_cache |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import com.melonhead.data_app_data.AppDataService |
| 5 | +import com.melonhead.data_at_home.AtHomeService |
| 6 | +import com.melonhead.lib_database.chapter.ChapterEntity |
| 7 | +import com.melonhead.lib_database.manga.MangaEntity |
| 8 | +import com.melonhead.lib_logging.Clog |
| 9 | +import kotlinx.coroutines.CoroutineScope |
| 10 | +import kotlinx.coroutines.Dispatchers |
| 11 | +import kotlinx.coroutines.flow.firstOrNull |
| 12 | +import kotlinx.coroutines.launch |
| 13 | +import java.io.* |
| 14 | +import java.net.URL |
| 15 | + |
| 16 | +interface ChapterCache { |
| 17 | + fun cacheImagesForChapters(manga: List<MangaEntity>, chapters: List<ChapterEntity>) |
| 18 | + fun getChapterFromCache(mangaId: String, chapterId: String): List<String> |
| 19 | + fun clearChapterFromCache(mangaId: String, chapterId: String) |
| 20 | +} |
| 21 | + |
| 22 | +internal class ChapterCacheImpl( |
| 23 | + private val appDataService: AppDataService, |
| 24 | + private val atHomeService: AtHomeService, |
| 25 | + private val externalScope: CoroutineScope, |
| 26 | + private val appContext: Context, |
| 27 | +) : ChapterCache { |
| 28 | + |
| 29 | + private suspend fun getChapterData(chapterId: String): List<String>? { |
| 30 | + val token = appDataService.token.firstOrNull() ?: return null |
| 31 | + val chapterData = atHomeService.getChapterData(token, chapterId) |
| 32 | + return if (appDataService.useDataSaver) { |
| 33 | + chapterData?.pagesDataSaver() |
| 34 | + } else { |
| 35 | + chapterData?.pages() |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + override fun cacheImagesForChapters(manga: List<MangaEntity>, chapters: List<ChapterEntity>) { |
| 40 | + Clog.d("Caching images for ${chapters.size} chapters") |
| 41 | + for (chapter in chapters) { |
| 42 | + val mangaForChapter = manga.find { it.id == chapter.mangaId } ?: continue |
| 43 | + if (mangaForChapter.useWebview) continue |
| 44 | + externalScope.launch(Dispatchers.IO) { |
| 45 | + Clog.d("Caching images for manga ${mangaForChapter.chosenTitle} chapter ${chapter.chapterTitle}") |
| 46 | + val cacheDirectory = appContext.cacheDir |
| 47 | + val mangaDirectory = File(cacheDirectory, mangaForChapter.id) |
| 48 | + |
| 49 | + val chapterData = getChapterData(chapter.id) |
| 50 | + if (chapterData.isNullOrEmpty()) return@launch |
| 51 | + |
| 52 | + if (!mangaDirectory.exists()) { |
| 53 | + mangaDirectory.mkdir() |
| 54 | + } |
| 55 | + |
| 56 | + val chapterDirectory = File(mangaDirectory, chapter.id) |
| 57 | + if (!chapterDirectory.exists()) { |
| 58 | + chapterDirectory.mkdir() |
| 59 | + } |
| 60 | + |
| 61 | + val oldFiles = chapterDirectory.listFiles()?.size ?: 0 |
| 62 | + if (oldFiles != chapterData.size) { |
| 63 | + for (file in chapterDirectory.listFiles()!!) { |
| 64 | + file.delete() |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + Clog.d("Downloading images to cache for ${mangaForChapter.chosenTitle} chapter ${chapter.chapterTitle}") |
| 69 | + for ((i, page) in chapterData.withIndex()) { |
| 70 | + Clog.d("Downloading page $i for ${mangaForChapter.chosenTitle} chapter ${chapter.chapterTitle} - $page") |
| 71 | + val fileExtension = page.substringAfterLast(".") |
| 72 | + val pageFile = File(chapterDirectory, "$i.$fileExtension") |
| 73 | + pageFile.createNewFile() |
| 74 | + |
| 75 | + try { |
| 76 | + val oStream = FileOutputStream(pageFile) |
| 77 | + val inputStream = URL(page).openStream() |
| 78 | + copy(inputStream, oStream) |
| 79 | + oStream.flush() |
| 80 | + inputStream.close() |
| 81 | + oStream.close() |
| 82 | + } catch (e: Exception) { |
| 83 | + Clog.i("Error downloading page $i for ${mangaForChapter.chosenTitle} chapter ${chapter.chapterTitle} - $page") |
| 84 | + Clog.e("Error downloading page", e) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Throws(IOException::class) |
| 92 | + private fun copy(source: InputStream, target: OutputStream) { |
| 93 | + val buf = ByteArray(8192) |
| 94 | + var length: Int |
| 95 | + while (source.read(buf).also { length = it } > 0) { |
| 96 | + target.write(buf, 0, length) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + override fun getChapterFromCache(mangaId: String, chapterId: String): List<String> { |
| 101 | + val cacheDirectory = appContext.cacheDir |
| 102 | + val mangaDirectory = File(cacheDirectory, mangaId) |
| 103 | + val chapterDirectory = File(mangaDirectory, chapterId) |
| 104 | + return if (chapterDirectory.exists()) { |
| 105 | + chapterDirectory.listFiles()?.map { it.absolutePath } ?: emptyList() |
| 106 | + } else { |
| 107 | + emptyList() |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + override fun clearChapterFromCache(mangaId: String, chapterId: String) { |
| 112 | + try { |
| 113 | + Clog.d("Clearing cache for manga $mangaId chapter $chapterId") |
| 114 | + val cacheDirectory = appContext.cacheDir |
| 115 | + val mangaDirectory = File(cacheDirectory, mangaId) |
| 116 | + val chapterDirectory = File(mangaDirectory, chapterId) |
| 117 | + if (chapterDirectory.exists()) { |
| 118 | + val result = chapterDirectory.deleteRecursively() |
| 119 | + Clog.d("Cleared cache for manga $mangaId chapter $chapterId - $result") |
| 120 | + } |
| 121 | + } catch (e: Exception) { |
| 122 | + Clog.i("Error clearing cache for manga $mangaId chapter $chapterId") |
| 123 | + Clog.e("Error clearing cache for manga", e) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments