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
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<InputStream>,
@Throws(Exception::class)
override fun loadData(priority: Priority): InputStream? {
if (manga.favorite) {
synchronized(file) {
if (!file.exists()) {
networkFetcher.loadData(priority)?.let { input ->
val atomicFile = AtomicFile(file)
val output = atomicFile.startWrite()
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()

View file

@ -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<Manga> {
* 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<String, Pair<GlideUrl, File>>(100)
private val lruCache = LruCache<String, Pair<GlideUrl, File>>(100)
/**
* Map where request headers are stored for a source.
@ -74,6 +75,7 @@ class MangaModelLoader(context: Context) : StreamModelLoader<Manga> {
override fun getResourceFetcher(manga: Manga,
width: Int,
height: Int): DataFetcher<InputStream>? {
// Check thumbnail is not null or empty
val url = manga.thumbnail_url
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
// 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<Manga> {
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])
}