Add auto migration support from legacy extension store index (#3398)
This commit is contained in:
@@ -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 ->
|
||||||
@@ -34,10 +38,11 @@ class ExtensionStoreService(
|
|||||||
try {
|
try {
|
||||||
json.decodeFromBufferedSource<NetworkExtensionStore>(source.peek())
|
json.decodeFromBufferedSource<NetworkExtensionStore>(source.peek())
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
|
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")) {
|
||||||
@@ -51,6 +56,12 @@ class ExtensionStoreService(
|
|||||||
json.decodeFromBufferedSource<NetworkLegacyExtensionRepo>(it)
|
json.decodeFromBufferedSource<NetworkLegacyExtensionRepo>(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (legacyIndex.indexV2 != null) {
|
||||||
|
return fetch(legacyIndex.indexV2, forceV2 = true)
|
||||||
|
} else {
|
||||||
|
legacyIndex
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.toExtensionStore(updatedIndexUrl)
|
.toExtensionStore(updatedIndexUrl)
|
||||||
|
|||||||
Reference in New Issue
Block a user