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
|
||||
|
||||
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 ->
|
||||
|
|
@ -40,10 +44,11 @@ class ExtensionStoreService(
|
|||
} catch (e: Exception) {
|
||||
if (e is CancellationException) throw e
|
||||
// KMK <--
|
||||
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")) {
|
||||
|
|
@ -57,6 +62,12 @@ class ExtensionStoreService(
|
|||
json.decodeFromBufferedSource<NetworkLegacyExtensionRepo>(it.body.source())
|
||||
}
|
||||
}
|
||||
|
||||
if (legacyIndex.indexV2 != null) {
|
||||
return fetch(legacyIndex.indexV2, forceV2 = true)
|
||||
} else {
|
||||
legacyIndex
|
||||
}
|
||||
}
|
||||
}
|
||||
.toExtensionStore(updatedIndexUrl)
|
||||
|
|
|
|||
Loading…
Reference in a new issue