Add auto migration support from legacy extension store index (#3398)
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
package mihon.data.extension.model
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import mihon.domain.extension.model.ExtensionStore
|
||||
|
||||
@SuppressLint("UnsafeOptInUsageError")
|
||||
@Serializable
|
||||
data class NetworkLegacyExtensionRepo(
|
||||
@SerialName("index_v2")
|
||||
val indexV2: String?,
|
||||
val meta: Meta,
|
||||
) : BaseNetworkExtensionStore {
|
||||
@Serializable
|
||||
|
||||
@@ -22,6 +22,10 @@ class ExtensionStoreService(
|
||||
private val protoBuf: ProtoBuf,
|
||||
) {
|
||||
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
|
||||
return try {
|
||||
val store = network.client.newCall(GET(indexUrl)).awaitSuccess().body.source().use { source ->
|
||||
@@ -34,10 +38,11 @@ class ExtensionStoreService(
|
||||
try {
|
||||
json.decodeFromBufferedSource<NetworkExtensionStore>(source.peek())
|
||||
} catch (e: IllegalArgumentException) {
|
||||
if (forceV2) throw e
|
||||
logcat(LogPriority.ERROR, e) {
|
||||
"Failed to add extension store '$updatedIndexUrl'"
|
||||
}
|
||||
try {
|
||||
val legacyIndex = try {
|
||||
json.decodeFromBufferedSource<NetworkLegacyExtensionRepo>(source.peek())
|
||||
} catch (e: IllegalArgumentException) {
|
||||
if (!indexUrl.endsWith("/index.min.json")) {
|
||||
@@ -51,6 +56,12 @@ class ExtensionStoreService(
|
||||
json.decodeFromBufferedSource<NetworkLegacyExtensionRepo>(it)
|
||||
}
|
||||
}
|
||||
|
||||
if (legacyIndex.indexV2 != null) {
|
||||
return fetch(legacyIndex.indexV2, forceV2 = true)
|
||||
} else {
|
||||
legacyIndex
|
||||
}
|
||||
}
|
||||
}
|
||||
.toExtensionStore(updatedIndexUrl)
|
||||
|
||||
Reference in New Issue
Block a user