From 3dbdc495e7aa467f43a2997c26add2108ff83c44 Mon Sep 17 00:00:00 2001 From: len Date: Sun, 3 Jul 2016 14:25:51 +0200 Subject: [PATCH] Minor changes --- .../tachiyomi/data/glide/MangaDataFetcher.kt | 31 +++++++++++++------ .../tachiyomi/data/glide/MangaModelLoader.kt | 11 ++++--- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/glide/MangaDataFetcher.kt b/app/src/main/java/eu/kanade/tachiyomi/data/glide/MangaDataFetcher.kt index 7d7e876a7..2853fea7e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/glide/MangaDataFetcher.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/glide/MangaDataFetcher.kt @@ -1,11 +1,10 @@ package eu.kanade.tachiyomi.data.glide -import android.support.v4.util.AtomicFile import com.bumptech.glide.Priority import com.bumptech.glide.load.data.DataFetcher import eu.kanade.tachiyomi.data.database.models.Manga import java.io.File -import java.io.FileInputStream +import java.io.FileNotFoundException import java.io.InputStream /** @@ -27,20 +26,32 @@ class MangaDataFetcher(private val networkFetcher: DataFetcher, @Throws(Exception::class) override fun loadData(priority: Priority): InputStream? { if (manga.favorite) { - if (!file.exists()) { - networkFetcher.loadData(priority)?.let { input -> - val atomicFile = AtomicFile(file) - val output = atomicFile.startWrite() + synchronized(file) { + if (!file.exists()) { + val tmpFile = File(file.path + ".tmp") try { - input.use { it.copyTo(output) } - atomicFile.finishWrite(output) + // Retrieve source stream. + val input = networkFetcher.loadData(priority) + ?: throw Exception("Couldn't open source stream") + + // Retrieve destination stream, create parent folders if needed. + val output = try { + tmpFile.outputStream() + } catch (e: FileNotFoundException) { + tmpFile.parentFile.mkdirs() + tmpFile.outputStream() + } + + // Copy the file and rename to the original. + input.use { output.use { input.copyTo(output) } } + tmpFile.renameTo(file) } catch (e: Exception) { - atomicFile.failWrite(output) + tmpFile.delete() throw e } } } - return FileInputStream(file) + return file.inputStream() } else { if (file.exists()) { file.delete() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/glide/MangaModelLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/glide/MangaModelLoader.kt index dbfa768f8..429086ff0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/glide/MangaModelLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/glide/MangaModelLoader.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.data.glide import android.content.Context +import android.util.LruCache import com.bumptech.glide.Glide import com.bumptech.glide.load.data.DataFetcher import com.bumptech.glide.load.model.* @@ -46,7 +47,7 @@ class MangaModelLoader(context: Context) : StreamModelLoader { * LRU cache whose key is the thumbnail url of the manga, and the value contains the request url * and the file where it should be stored in case the manga is a favorite. */ - private val modelCache = ModelCache>(100) + private val lruCache = LruCache>(100) /** * Map where request headers are stored for a source. @@ -74,6 +75,7 @@ class MangaModelLoader(context: Context) : StreamModelLoader { override fun getResourceFetcher(manga: Manga, width: Int, height: Int): DataFetcher? { + // Check thumbnail is not null or empty val url = manga.thumbnail_url if (url.isNullOrEmpty()) { @@ -82,9 +84,9 @@ class MangaModelLoader(context: Context) : StreamModelLoader { // Obtain the request url and the file for this url from the LRU cache, or calculate it // and add them to the cache. - val (glideUrl, file) = modelCache.get(url, width, height) ?: + val (glideUrl, file) = lruCache.get(url) ?: Pair(GlideUrl(url, getHeaders(manga)), coverCache.getCoverFile(url!!)).apply { - modelCache.put(url, width, height, this) + lruCache.put(url, this) } // Get the network fetcher for this request url. @@ -103,7 +105,8 @@ class MangaModelLoader(context: Context) : StreamModelLoader { val source = sourceManager.get(manga.source) as? OnlineSource ?: return LazyHeaders.DEFAULT return cachedHeaders.getOrPut(manga.source) { LazyHeaders.Builder().apply { - setHeader("User-Agent", null as? String) + val nullStr: String? = null + setHeader("User-Agent", nullStr) for ((key, value) in source.headers.toMultimap()) { addHeader(key, value[0]) }