Use cache to complete downloads

This commit is contained in:
Jay 2020-01-11 04:22:24 -08:00
parent 00b1b097a7
commit a5120edd0e
2 changed files with 38 additions and 8 deletions

View file

@ -151,10 +151,10 @@ class ChapterCache(private val context: Context) {
* @return true if in cache otherwise false. * @return true if in cache otherwise false.
*/ */
fun isImageInCache(imageUrl: String): Boolean { fun isImageInCache(imageUrl: String): Boolean {
try { return try {
return diskCache.get(DiskUtil.hashKeyForDisk(imageUrl)) != null diskCache.get(DiskUtil.hashKeyForDisk(imageUrl)) != null
} catch (e: IOException) { } catch (e: IOException) {
return false false
} }
} }

View file

@ -1,10 +1,12 @@
package eu.kanade.tachiyomi.data.download package eu.kanade.tachiyomi.data.download
import android.content.Context import android.content.Context
import android.net.Uri
import android.webkit.MimeTypeMap import android.webkit.MimeTypeMap
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import com.jakewharton.rxrelay.BehaviorRelay import com.jakewharton.rxrelay.BehaviorRelay
import com.jakewharton.rxrelay.PublishRelay import com.jakewharton.rxrelay.PublishRelay
import eu.kanade.tachiyomi.data.cache.ChapterCache
import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.data.download.model.Download
@ -27,6 +29,7 @@ import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers import rx.schedulers.Schedulers
import rx.subscriptions.CompositeSubscription import rx.subscriptions.CompositeSubscription
import timber.log.Timber import timber.log.Timber
import java.io.File
/** /**
* This class is the one in charge of downloading chapters. * This class is the one in charge of downloading chapters.
@ -338,12 +341,14 @@ class Downloader(
// Try to find the image file. // Try to find the image file.
val imageFile = tmpDir.listFiles()!!.find { it.name!!.startsWith("$filename.") } val imageFile = tmpDir.listFiles()!!.find { it.name!!.startsWith("$filename.") }
val cache = ChapterCache(context)
// If the image is already downloaded, do nothing. Otherwise download from network // If the image is already downloaded, do nothing. Otherwise download from network
val pageObservable = if (imageFile != null) val pageObservable = when {
Observable.just(imageFile) imageFile != null -> Observable.just(imageFile)
else cache.isImageInCache(page.imageUrl!!) ->
downloadImage(page, download.source, tmpDir, filename) moveFromCache(page, cache.getImageFile(page.imageUrl!!), tmpDir, filename)
else -> downloadImage(page, download.source, tmpDir, filename)
}
return pageObservable return pageObservable
// When the image is ready, set image path, progress (just in case) and status // When the image is ready, set image path, progress (just in case) and status
@ -362,6 +367,31 @@ class Downloader(
} }
} }
/**
* Returns the observable which takes from the downloaded image from cache
*
* @param page the page to download.
* @param file the file from cache
* @param tmpDir the temporary directory of the download.
* @param filename the filename of the image.
*/
private fun moveFromCache(page: Page, file: File, tmpDir: UniFile, filename: String):
Observable<UniFile> {
return Observable.just(file).map {
val tmpFile = tmpDir.createFile("$filename.tmp")
val inputStream = file.inputStream()
inputStream.use { input ->
tmpFile.openOutputStream().use { output ->
input.copyTo(output)
}
}
val extension = ImageUtil.findImageType(file.inputStream()) ?: return@map tmpFile
tmpFile.renameTo("$filename.${extension.extension}")
file.delete()
tmpFile
}
}
/** /**
* Returns the observable which downloads the image from network. * Returns the observable which downloads the image from network.
* *