Minor changes

This commit is contained in:
len 2016-07-03 14:25:51 +02:00
parent 466515c801
commit 3dbdc495e7
2 changed files with 28 additions and 14 deletions

View File

@ -1,11 +1,10 @@
package eu.kanade.tachiyomi.data.glide package eu.kanade.tachiyomi.data.glide
import android.support.v4.util.AtomicFile
import com.bumptech.glide.Priority import com.bumptech.glide.Priority
import com.bumptech.glide.load.data.DataFetcher import com.bumptech.glide.load.data.DataFetcher
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import java.io.File import java.io.File
import java.io.FileInputStream import java.io.FileNotFoundException
import java.io.InputStream import java.io.InputStream
/** /**
@ -27,20 +26,32 @@ class MangaDataFetcher(private val networkFetcher: DataFetcher<InputStream>,
@Throws(Exception::class) @Throws(Exception::class)
override fun loadData(priority: Priority): InputStream? { override fun loadData(priority: Priority): InputStream? {
if (manga.favorite) { if (manga.favorite) {
synchronized(file) {
if (!file.exists()) { if (!file.exists()) {
networkFetcher.loadData(priority)?.let { input -> val tmpFile = File(file.path + ".tmp")
val atomicFile = AtomicFile(file)
val output = atomicFile.startWrite()
try { try {
input.use { it.copyTo(output) } // Retrieve source stream.
atomicFile.finishWrite(output) 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) { } catch (e: Exception) {
atomicFile.failWrite(output) tmpFile.delete()
throw e throw e
} }
} }
} }
return FileInputStream(file) return file.inputStream()
} else { } else {
if (file.exists()) { if (file.exists()) {
file.delete() file.delete()

View File

@ -1,6 +1,7 @@
package eu.kanade.tachiyomi.data.glide package eu.kanade.tachiyomi.data.glide
import android.content.Context import android.content.Context
import android.util.LruCache
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.load.data.DataFetcher import com.bumptech.glide.load.data.DataFetcher
import com.bumptech.glide.load.model.* import com.bumptech.glide.load.model.*
@ -46,7 +47,7 @@ class MangaModelLoader(context: Context) : StreamModelLoader<Manga> {
* LRU cache whose key is the thumbnail url of the manga, and the value contains the request url * 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. * and the file where it should be stored in case the manga is a favorite.
*/ */
private val modelCache = ModelCache<String, Pair<GlideUrl, File>>(100) private val lruCache = LruCache<String, Pair<GlideUrl, File>>(100)
/** /**
* Map where request headers are stored for a source. * Map where request headers are stored for a source.
@ -74,6 +75,7 @@ class MangaModelLoader(context: Context) : StreamModelLoader<Manga> {
override fun getResourceFetcher(manga: Manga, override fun getResourceFetcher(manga: Manga,
width: Int, width: Int,
height: Int): DataFetcher<InputStream>? { height: Int): DataFetcher<InputStream>? {
// Check thumbnail is not null or empty // Check thumbnail is not null or empty
val url = manga.thumbnail_url val url = manga.thumbnail_url
if (url.isNullOrEmpty()) { if (url.isNullOrEmpty()) {
@ -82,9 +84,9 @@ class MangaModelLoader(context: Context) : StreamModelLoader<Manga> {
// Obtain the request url and the file for this url from the LRU cache, or calculate it // Obtain the request url and the file for this url from the LRU cache, or calculate it
// and add them to the cache. // 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 { 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. // Get the network fetcher for this request url.
@ -103,7 +105,8 @@ class MangaModelLoader(context: Context) : StreamModelLoader<Manga> {
val source = sourceManager.get(manga.source) as? OnlineSource ?: return LazyHeaders.DEFAULT val source = sourceManager.get(manga.source) as? OnlineSource ?: return LazyHeaders.DEFAULT
return cachedHeaders.getOrPut(manga.source) { return cachedHeaders.getOrPut(manga.source) {
LazyHeaders.Builder().apply { LazyHeaders.Builder().apply {
setHeader("User-Agent", null as? String) val nullStr: String? = null
setHeader("User-Agent", nullStr)
for ((key, value) in source.headers.toMultimap()) { for ((key, value) in source.headers.toMultimap()) {
addHeader(key, value[0]) addHeader(key, value[0])
} }