Move spiderInfo compat to separate file

This commit is contained in:
tarsin committed 2023-01-30 10:58:05 +08:00
1 parent 7d00e01e00
commit 34445e6f92
5 files changed
+46 -44

No files matched your search

@@ -0,0 +1,40 @@
package com.hippo.compat
import com.hippo.ehviewer.spider.SpiderInfo
import com.hippo.yorozuya.IOUtils
import java.io.InputStream
fun readLegacySpiderInfo(inputStream: InputStream): SpiderInfo {
fun read(): String {
return IOUtils.readAsciiLine(inputStream)
}
fun readInt(): Int {
return read().toInt()
}
fun readLong(): Long {
return read().toLong()
}
repeat(2) { read() } // We assert that only info v2
val gid = readLong()
val token = read()
read()
val previewPages = readInt()
val previewPerPage = readInt()
val pages = read().toInt()
val pTokenMap = hashMapOf<Int, String>()
val info = SpiderInfo(gid, pages, pTokenMap, 0, token, previewPages, previewPerPage)
runCatching {
val line = read()
val pos = line.indexOf(" ")
if (pos > 0) {
val index = line.substring(0, pos).toInt()
val pToken = line.substring(pos + 1)
if (pToken.isNotEmpty()) {
pTokenMap[index] = pToken
}
}
}
return info
}
@@ -581,7 +581,7 @@ class DownloadManager : OnSpiderListener {
galleryInfo.rating = downloadInfo.rating
val downloadDir = SpiderDen.getGalleryDownloadDir(galleryInfo.gid) ?: continue
val file = downloadDir.findFile(SpiderQueen.SPIDER_INFO_FILENAME) ?: continue
val spiderInfo = SpiderInfo.read(file) ?: continue
val spiderInfo = SpiderInfo.readCompatFromUniFile(file) ?: continue
spiderInfo.startPage = 0
try {
spiderInfo.write(file)
@@ -32,10 +32,8 @@ import com.hippo.ehviewer.spider.SpiderQueen
import com.hippo.ehviewer.ui.scene.BaseScene
import com.hippo.unifile.UniFile
import com.hippo.util.ExceptionUtils
import com.hippo.yorozuya.IOUtils
import kotlinx.coroutines.runBlocking
import java.io.IOException
import java.io.InputStream
import java.util.Collections
class RestoreDownloadPreference constructor(
@@ -54,7 +52,7 @@ class RestoreDownloadPreference constructor(
}
val siFile = file.findFile(SpiderQueen.SPIDER_INFO_FILENAME) ?: return null
return try {
val spiderInfo = SpiderInfo.read(siFile) ?: return null
val spiderInfo = SpiderInfo.readCompatFromUniFile(siFile) ?: return null
val gid = spiderInfo.gid
val dirname = file.name
if (mManager.containDownloadInfo(gid)) {
@@ -4,16 +4,15 @@ package com.hippo.ehviewer.spider
import coil.annotation.ExperimentalCoilApi
import coil.disk.DiskCache
import com.hippo.compat.readLegacySpiderInfo
import com.hippo.ehviewer.EhApplication
import com.hippo.unifile.UniFile
import com.hippo.yorozuya.IOUtils
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.cbor.Cbor
import kotlinx.serialization.decodeFromByteArray
import kotlinx.serialization.encodeToByteArray
import java.io.File
import java.io.InputStream
@Serializable
class SpiderInfo @JvmOverloads constructor(
@@ -71,51 +70,16 @@ class SpiderInfo @JvmOverloads constructor(
}
@JvmStatic
fun read(file: UniFile): SpiderInfo? {
fun readCompatFromUniFile(file: UniFile): SpiderInfo? {
return runCatching {
file.openInputStream().use {
Cbor.decodeFromByteArray<SpiderInfo>(it.readBytes())
}
}.getOrNull() ?: runCatching {
file.openInputStream().use { readV1(it) }
file.openInputStream().use { readLegacySpiderInfo(it) }
}.getOrNull()
}
private fun readV1(inputStream: InputStream): SpiderInfo {
fun read(): String {
return IOUtils.readAsciiLine(inputStream)
}
fun readInt(): Int {
return read().toInt()
}
fun readLong(): Long {
return read().toLong()
}
repeat(2) { read() } // We assert that only info v2
val gid = readLong()
val token = read()
read()
val previewPages = readInt()
val previewPerPage = readInt()
val pages = read().toInt()
val pTokenMap = hashMapOf<Int, String>()
val info = SpiderInfo(gid, pages, pTokenMap, 0, token, previewPages, previewPerPage)
runCatching {
val line = read()
val pos = line.indexOf(" ")
if (pos > 0) {
val index = line.substring(0, pos).toInt()
val pToken = line.substring(pos + 1)
if (pToken.isNotEmpty()) {
pTokenMap[index] = pToken
}
}
}
return info
}
const val TOKEN_FAILED = "failed"
}
}
@@ -696,7 +696,7 @@ public final class SpiderQueen implements Runnable {
if (downloadDir != null) {
UniFile file = downloadDir.findFile(SPIDER_INFO_FILENAME);
if (file != null) {
spiderInfo = SpiderInfo.read(file);
spiderInfo = SpiderInfo.readCompatFromUniFile(file);
if (spiderInfo != null && spiderInfo.getGid() == mGalleryInfo.getGid() &&
spiderInfo.getToken().equals(mGalleryInfo.getToken())) {
return spiderInfo;