Conflicts:
	.github/workflows/test.yml
pull/1504/head
celetor 3 years ago
commit addbc0622e
  1. 8
      app/src/main/java/io/legado/app/help/CacheManager.kt
  2. 15
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  3. 4
      app/src/main/java/io/legado/app/ui/book/read/SearchMenu.kt
  4. 4
      app/src/main/java/io/legado/app/utils/ACache.kt
  5. 13
      app/src/main/java/io/legado/app/utils/UTF8BOMFighter.kt

@ -28,6 +28,10 @@ object CacheManager {
} }
} }
fun putString2File(key: String, value: String, saveTime: Int = 0) {
ACache.get(appCtx).put(key, value, saveTime)
}
fun get(key: String): String? { fun get(key: String): String? {
return appDb.cacheDao.get(key, System.currentTimeMillis()) return appDb.cacheDao.get(key, System.currentTimeMillis())
} }
@ -52,6 +56,10 @@ object CacheManager {
return ACache.get(appCtx).getAsBinary(key) return ACache.get(appCtx).getAsBinary(key)
} }
fun getString(key: String): String? {
return ACache.get(appCtx).getAsString(key)
}
fun getQueryTTF(key: String): QueryTTF? { fun getQueryTTF(key: String): QueryTTF? {
val cache = queryTTFMap[key] ?: return null val cache = queryTTFMap[key] ?: return null
if (cache.first == 0L || cache.first > System.currentTimeMillis()) { if (cache.first == 0L || cache.first > System.currentTimeMillis()) {

@ -138,15 +138,14 @@ interface JsExtensions {
* @param saveTime 缓存时间单位 * @param saveTime 缓存时间单位
* @return 返回缓存后的文件内容 * @return 返回缓存后的文件内容
*/ */
fun cacheFile(url: String, saveTime: Int = 0): String? { fun cacheFile(urlStr: String, saveTime: Int = 0): String? {
val key = md5Encode16(url) val key = md5Encode16(urlStr)
val cache = ACache.get(appCtx).getAsString(key) val cache = CacheManager.getString(key)
if(cache.isNullOrBlank()) { if(cache.isNullOrBlank()) {
log("首次下载${url}...") log("首次下载 ${urlStr}")
val value = ajax(url) ?: "" val value = ajax(urlStr) ?: return null
if (saveTime == 0) { CacheManager.putString2File(key, value, saveTime)
ACache.get(appCtx).put(key, value) return value
} else ACache.get(appCtx).put(key, value, saveTime)
} }
return cache return cache
} }

@ -40,8 +40,8 @@ class SearchMenu @JvmOverloads constructor(
private var onMenuOutEnd: (() -> Unit)? = null private var onMenuOutEnd: (() -> Unit)? = null
private val searchResultList: MutableList<SearchResult> = mutableListOf() private val searchResultList: MutableList<SearchResult> = mutableListOf()
private var currentSearchResultIndex: Int = 0 private var currentSearchResultIndex: Int = -1
private var lastSearchResultIndex: Int = 0 private var lastSearchResultIndex: Int = -1
private val hasSearchResult: Boolean private val hasSearchResult: Boolean
get() = searchResultList.isNotEmpty() get() = searchResultList.isNotEmpty()
val selectedSearchResult: SearchResult? val selectedSearchResult: SearchResult?

@ -109,7 +109,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @param saveTime 保存的时间单位 * @param saveTime 保存的时间单位
*/ */
fun put(key: String, value: String, saveTime: Int) { fun put(key: String, value: String, saveTime: Int) {
put(key, Utils.newStringWithDateInfo(saveTime, value)) if (saveTime == 0) put(key, value) else put(key, Utils.newStringWithDateInfo(saveTime, value))
} }
/** /**
@ -245,7 +245,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
* @param saveTime 保存的时间单位 * @param saveTime 保存的时间单位
*/ */
fun put(key: String, value: ByteArray, saveTime: Int) { fun put(key: String, value: ByteArray, saveTime: Int) {
put(key, Utils.newByteArrayWithDateInfo(saveTime, value)) if (saveTime == 0) put(key, value) else put(key, Utils.newByteArrayWithDateInfo(saveTime, value))
} }
/** /**

@ -18,9 +18,9 @@ object UTF8BOMFighter {
fun removeUTF8BOM(bytes: ByteArray): ByteArray { fun removeUTF8BOM(bytes: ByteArray): ByteArray {
val containsBOM = (bytes.size > 3 val containsBOM = (bytes.size > 3
&& bytes[0] == UTF8_BOM_BYTES[0] && bytes[0] == UTF8_BOM_BYTES[0]
&& bytes[1] == UTF8_BOM_BYTES[1] && bytes[1] == UTF8_BOM_BYTES[1]
&& bytes[2] == UTF8_BOM_BYTES[2]) && bytes[2] == UTF8_BOM_BYTES[2])
if (containsBOM) { if (containsBOM) {
val copy = ByteArray(bytes.size - 3) val copy = ByteArray(bytes.size - 3)
System.arraycopy(bytes, 3, copy, 0, bytes.size - 3) System.arraycopy(bytes, 3, copy, 0, bytes.size - 3)
@ -28,4 +28,11 @@ object UTF8BOMFighter {
} }
return bytes return bytes
} }
fun hasBom(bytes: ByteArray): Boolean {
return (bytes.size > 3
&& bytes[0] == UTF8_BOM_BYTES[0]
&& bytes[1] == UTF8_BOM_BYTES[1]
&& bytes[2] == UTF8_BOM_BYTES[2])
}
} }
Loading…
Cancel
Save