|
|
|
@ -4,7 +4,7 @@ import android.content.Context
|
|
|
|
import com.tomclaw.cache.DiskLruCache
|
|
|
|
import com.tomclaw.cache.DiskLruCache
|
|
|
|
import org.koitharu.kotatsu.utils.FileSizeUtils
|
|
|
|
import org.koitharu.kotatsu.utils.FileSizeUtils
|
|
|
|
import org.koitharu.kotatsu.utils.ext.longHashCode
|
|
|
|
import org.koitharu.kotatsu.utils.ext.longHashCode
|
|
|
|
import org.koitharu.kotatsu.utils.ext.sub
|
|
|
|
import org.koitharu.kotatsu.utils.ext.subdir
|
|
|
|
import org.koitharu.kotatsu.utils.ext.takeIfReadable
|
|
|
|
import org.koitharu.kotatsu.utils.ext.takeIfReadable
|
|
|
|
import java.io.File
|
|
|
|
import java.io.File
|
|
|
|
import java.io.InputStream
|
|
|
|
import java.io.InputStream
|
|
|
|
@ -13,8 +13,10 @@ import java.io.OutputStream
|
|
|
|
class PagesCache(context: Context) {
|
|
|
|
class PagesCache(context: Context) {
|
|
|
|
|
|
|
|
|
|
|
|
private val cacheDir = context.externalCacheDir ?: context.cacheDir
|
|
|
|
private val cacheDir = context.externalCacheDir ?: context.cacheDir
|
|
|
|
private val lruCache =
|
|
|
|
private val lruCache = DiskLruCache.create(
|
|
|
|
DiskLruCache.create(cacheDir.sub(Cache.PAGES.dir), FileSizeUtils.mbToBytes(200))
|
|
|
|
cacheDir.subdir(Cache.PAGES.dir),
|
|
|
|
|
|
|
|
FileSizeUtils.mbToBytes(200)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
operator fun get(url: String): File? {
|
|
|
|
operator fun get(url: String): File? {
|
|
|
|
return lruCache.get(url)?.takeIfReadable()
|
|
|
|
return lruCache.get(url)?.takeIfReadable()
|
|
|
|
@ -22,7 +24,7 @@ class PagesCache(context: Context) {
|
|
|
|
|
|
|
|
|
|
|
|
@Deprecated("Useless lambda")
|
|
|
|
@Deprecated("Useless lambda")
|
|
|
|
fun put(url: String, writer: (OutputStream) -> Unit): File {
|
|
|
|
fun put(url: String, writer: (OutputStream) -> Unit): File {
|
|
|
|
val file = cacheDir.sub(url.longHashCode().toString())
|
|
|
|
val file = File(cacheDir, url.longHashCode().toString())
|
|
|
|
file.outputStream().use(writer)
|
|
|
|
file.outputStream().use(writer)
|
|
|
|
val res = lruCache.put(url, file)
|
|
|
|
val res = lruCache.put(url, file)
|
|
|
|
file.delete()
|
|
|
|
file.delete()
|
|
|
|
@ -30,7 +32,7 @@ class PagesCache(context: Context) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun put(url: String, inputStream: InputStream): File {
|
|
|
|
fun put(url: String, inputStream: InputStream): File {
|
|
|
|
val file = cacheDir.sub(url.longHashCode().toString())
|
|
|
|
val file = File(cacheDir, url.longHashCode().toString())
|
|
|
|
file.outputStream().use { out ->
|
|
|
|
file.outputStream().use { out ->
|
|
|
|
inputStream.copyTo(out)
|
|
|
|
inputStream.copyTo(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|