This commit is contained in:
len
2016-11-24 21:42:01 +01:00
parent 87281d34c1
commit 93e244b4c4
6 changed files with 86 additions and 115 deletions

View File

@@ -6,7 +6,7 @@ import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jakewharton.disklrucache.DiskLruCache
import eu.kanade.tachiyomi.data.source.model.Page
import eu.kanade.tachiyomi.util.DiskUtils
import eu.kanade.tachiyomi.util.DiskUtil
import eu.kanade.tachiyomi.util.saveTo
import okhttp3.Response
import okio.Okio
@@ -70,7 +70,7 @@ class ChapterCache(private val context: Context) {
* @return real size of directory.
*/
private val realSize: Long
get() = DiskUtils.getDirectorySize(cacheDir)
get() = DiskUtil.getDirectorySize(cacheDir)
/**
* Returns real size of directory in human readable format.
@@ -107,7 +107,7 @@ class ChapterCache(private val context: Context) {
fun getPageListFromCache(chapterUrl: String): Observable<List<Page>> {
return Observable.fromCallable<List<Page>> {
// Get the key for the chapter.
val key = DiskUtils.hashKeyForDisk(chapterUrl)
val key = DiskUtil.hashKeyForDisk(chapterUrl)
// Convert JSON string to list of objects. Throws an exception if snapshot is null
diskCache.get(key).use {
@@ -130,7 +130,7 @@ class ChapterCache(private val context: Context) {
try {
// Get editor from md5 key.
val key = DiskUtils.hashKeyForDisk(chapterUrl)
val key = DiskUtil.hashKeyForDisk(chapterUrl)
editor = diskCache.edit(key) ?: return
// Write chapter urls to cache.
@@ -157,7 +157,7 @@ class ChapterCache(private val context: Context) {
*/
fun isImageInCache(imageUrl: String): Boolean {
try {
return diskCache.get(DiskUtils.hashKeyForDisk(imageUrl)) != null
return diskCache.get(DiskUtil.hashKeyForDisk(imageUrl)) != null
} catch (e: IOException) {
return false
}
@@ -171,7 +171,7 @@ class ChapterCache(private val context: Context) {
fun getImagePath(imageUrl: String): File? {
try {
// Get file from md5 key.
val imageName = DiskUtils.hashKeyForDisk(imageUrl) + ".0"
val imageName = DiskUtil.hashKeyForDisk(imageUrl) + ".0"
return File(diskCache.directory, imageName)
} catch (e: IOException) {
return null
@@ -191,7 +191,7 @@ class ChapterCache(private val context: Context) {
try {
// Get editor from md5 key.
val key = DiskUtils.hashKeyForDisk(imageUrl)
val key = DiskUtil.hashKeyForDisk(imageUrl)
editor = diskCache.edit(key) ?: throw IOException("Unable to edit key")
// Get OutputStream and write image with Okio.

View File

@@ -1,7 +1,7 @@
package eu.kanade.tachiyomi.data.cache
import android.content.Context
import eu.kanade.tachiyomi.util.DiskUtils
import eu.kanade.tachiyomi.util.DiskUtil
import java.io.File
import java.io.IOException
import java.io.InputStream
@@ -29,7 +29,7 @@ class CoverCache(private val context: Context) {
* @return cover image.
*/
fun getCoverFile(thumbnailUrl: String): File {
return File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl))
return File(cacheDir, DiskUtil.hashKeyForDisk(thumbnailUrl))
}
/**

View File

@@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.source.Source
import eu.kanade.tachiyomi.util.DiskUtil
import uy.kohesive.injekt.injectLazy
/**
@@ -82,7 +83,7 @@ class DownloadProvider(private val context: Context) {
* @param manga the manga to query.
*/
fun getMangaDirName(manga: Manga): String {
return buildValidFatFilename(manga.title.trim('.', ' '))
return DiskUtil.buildValidFatFilename(manga.title)
}
/**
@@ -91,40 +92,7 @@ class DownloadProvider(private val context: Context) {
* @param chapter the chapter to query.
*/
fun getChapterDirName(chapter: Chapter): String {
return buildValidFatFilename(chapter.name.trim('.', ' '))
return DiskUtil.buildValidFatFilename(chapter.name)
}
/**
* Mutate the given filename to make it valid for a FAT filesystem,
* replacing any invalid characters with "_".
*/
private fun buildValidFatFilename(name: String): String {
if (name.isNullOrEmpty()) {
return "(invalid)"
}
val res = StringBuilder(name.length)
name.forEach { c ->
if (isValidFatFilenameChar(c)) {
res.append(c)
} else {
res.append('_')
}
}
// Even though vfat allows 255 UCS-2 chars, we might eventually write to
// ext4 through a FUSE layer, so use that limit minus 5 reserved characters.
return res.toString().take(250)
}
/**
* Returns true if the given character is a valid filename character, false otherwise.
*/
private fun isValidFatFilenameChar(c: Char): Boolean {
if (0x00.toChar() <= c && c <= 0x1f.toChar()) {
return false
}
when (c) {
'"', '*', '/', ':', '<', '>', '?', '\\', '|', 0x7f.toChar() -> return false
else -> return true
}
}
}