Add auto migration support from legacy extension store index (mihonapp/mihon#3398)
(cherry picked from commit d4327c8d48608ebc6eafc4a3df226c31d6586bbd)
This commit is contained in:
parent
1763d947df
commit
5e809fefd4
2 changed files with 15 additions and 1 deletions
|
|
@ -1,12 +1,15 @@
|
||||||
package mihon.data.extension.model
|
package mihon.data.extension.model
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import mihon.domain.extension.model.ExtensionStore
|
import mihon.domain.extension.model.ExtensionStore
|
||||||
|
|
||||||
@SuppressLint("UnsafeOptInUsageError")
|
@SuppressLint("UnsafeOptInUsageError")
|
||||||
@Serializable
|
@Serializable
|
||||||
data class NetworkLegacyExtensionRepo(
|
data class NetworkLegacyExtensionRepo(
|
||||||
|
@SerialName("index_v2")
|
||||||
|
val indexV2: String?,
|
||||||
val meta: Meta,
|
val meta: Meta,
|
||||||
) : BaseNetworkExtensionStore {
|
) : BaseNetworkExtensionStore {
|
||||||
@Serializable
|
@Serializable
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,10 @@ class ExtensionStoreService(
|
||||||
private val protoBuf: ProtoBuf,
|
private val protoBuf: ProtoBuf,
|
||||||
) {
|
) {
|
||||||
suspend fun fetch(indexUrl: String): Result<ExtensionStore> {
|
suspend fun fetch(indexUrl: String): Result<ExtensionStore> {
|
||||||
|
return fetch(indexUrl, forceV2 = false)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun fetch(indexUrl: String, forceV2: Boolean): Result<ExtensionStore> {
|
||||||
var updatedIndexUrl: String = indexUrl
|
var updatedIndexUrl: String = indexUrl
|
||||||
return try {
|
return try {
|
||||||
val store = network.client.newCall(GET(indexUrl)).awaitSuccess().body.source().use { source ->
|
val store = network.client.newCall(GET(indexUrl)).awaitSuccess().body.source().use { source ->
|
||||||
|
|
@ -40,10 +44,11 @@ class ExtensionStoreService(
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
if (e is CancellationException) throw e
|
if (e is CancellationException) throw e
|
||||||
// KMK <--
|
// KMK <--
|
||||||
|
if (forceV2) throw e
|
||||||
logcat(LogPriority.ERROR, e) {
|
logcat(LogPriority.ERROR, e) {
|
||||||
"Failed to add extension store '$updatedIndexUrl'"
|
"Failed to add extension store '$updatedIndexUrl'"
|
||||||
}
|
}
|
||||||
try {
|
val legacyIndex = try {
|
||||||
json.decodeFromBufferedSource<NetworkLegacyExtensionRepo>(source.peek())
|
json.decodeFromBufferedSource<NetworkLegacyExtensionRepo>(source.peek())
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
if (!indexUrl.endsWith("/index.min.json")) {
|
if (!indexUrl.endsWith("/index.min.json")) {
|
||||||
|
|
@ -57,6 +62,12 @@ class ExtensionStoreService(
|
||||||
json.decodeFromBufferedSource<NetworkLegacyExtensionRepo>(it.body.source())
|
json.decodeFromBufferedSource<NetworkLegacyExtensionRepo>(it.body.source())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (legacyIndex.indexV2 != null) {
|
||||||
|
return fetch(legacyIndex.indexV2, forceV2 = true)
|
||||||
|
} else {
|
||||||
|
legacyIndex
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.toExtensionStore(updatedIndexUrl)
|
.toExtensionStore(updatedIndexUrl)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue