From 3f21b49f43975061ea92ab9458fbf6853f835287 Mon Sep 17 00:00:00 2001 From: lisonge Date: Thu, 8 Aug 2024 20:44:27 +0800 Subject: [PATCH] fix: snapshot import url (#695) --- .../kotlin/li/songe/gkd/ui/SnapshotPage.kt | 12 +++- .../main/kotlin/li/songe/gkd/ui/SnapshotVm.kt | 7 +- .../songe/gkd/ui/component/UploadOptions.kt | 68 ++++++++++--------- 3 files changed, 52 insertions(+), 35 deletions(-) diff --git a/app/src/main/kotlin/li/songe/gkd/ui/SnapshotPage.kt b/app/src/main/kotlin/li/songe/gkd/ui/SnapshotPage.kt index 6885fd1..8e7a119 100644 --- a/app/src/main/kotlin/li/songe/gkd/ui/SnapshotPage.kt +++ b/app/src/main/kotlin/li/songe/gkd/ui/SnapshotPage.kt @@ -49,6 +49,7 @@ import com.ramcosta.composedestinations.navigation.navigate import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import li.songe.gkd.MainActivity +import li.songe.gkd.data.GithubPoliciesAsset import li.songe.gkd.data.Snapshot import li.songe.gkd.db.DbSet import li.songe.gkd.debug.SnapshotExt @@ -251,7 +252,16 @@ fun SnapshotPage() { text = "生成链接(需科学上网)", modifier = Modifier .clickable(onClick = vm.viewModelScope.launchAsFn(Dispatchers.IO) { selectedSnapshot = null - vm.uploadOptions.startTask(SnapshotExt.getSnapshotZipFile(snapshotVal.id)) + vm.uploadOptions.startTask( + file = SnapshotExt.getSnapshotZipFile( + snapshotVal.id + ), + onSuccessResult = vm.viewModelScope.launchAsFn( + Dispatchers.IO + ) { + DbSet.snapshotDao.update(snapshotVal.copy(githubAssetId = it.id)) + } + ) }) .then(modifier) ) diff --git a/app/src/main/kotlin/li/songe/gkd/ui/SnapshotVm.kt b/app/src/main/kotlin/li/songe/gkd/ui/SnapshotVm.kt index c8f4ba2..cdb7615 100644 --- a/app/src/main/kotlin/li/songe/gkd/ui/SnapshotVm.kt +++ b/app/src/main/kotlin/li/songe/gkd/ui/SnapshotVm.kt @@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.stateIn import li.songe.gkd.db.DbSet import li.songe.gkd.ui.component.UploadOptions +import li.songe.gkd.util.IMPORT_BASE_URL import javax.inject.Inject @@ -15,6 +16,8 @@ class SnapshotVm @Inject constructor() : ViewModel() { val snapshotsState = DbSet.snapshotDao.query() .stateIn(viewModelScope, SharingStarted.Eagerly, emptyList()) - - val uploadOptions = UploadOptions(viewModelScope) + val uploadOptions = UploadOptions( + scope = viewModelScope, + showHref = { IMPORT_BASE_URL + it.id } + ) } \ No newline at end of file diff --git a/app/src/main/kotlin/li/songe/gkd/ui/component/UploadOptions.kt b/app/src/main/kotlin/li/songe/gkd/ui/component/UploadOptions.kt index 626b4e3..510874b 100644 --- a/app/src/main/kotlin/li/songe/gkd/ui/component/UploadOptions.kt +++ b/app/src/main/kotlin/li/songe/gkd/ui/component/UploadOptions.kt @@ -29,47 +29,50 @@ import li.songe.gkd.util.toast import java.io.File class UploadOptions( - private val scope: CoroutineScope + private val scope: CoroutineScope, + private val showHref: (GithubPoliciesAsset) -> String = { it.shortHref } ) { - private val statusFlow = MutableStateFlow?>(null) + val statusFlow = MutableStateFlow?>(null) private var job: Job? = null - private fun buildTask(file: File) = scope.launchTry(Dispatchers.IO) { - statusFlow.value = LoadStatus.Loading() - try { - val response = - client.submitFormWithBinaryData(url = FILE_UPLOAD_URL, formData = formData { - append("\"file\"", file.readBytes(), Headers.build { - append(HttpHeaders.ContentType, "application/x-zip-compressed") - append(HttpHeaders.ContentDisposition, "filename=\"file.zip\"") - }) - }) { - onUpload { bytesSentTotal, contentLength -> - if (statusFlow.value is LoadStatus.Loading) { - statusFlow.value = - LoadStatus.Loading(bytesSentTotal / contentLength.toFloat()) + private fun buildTask(file: File, onSuccessResult: ((GithubPoliciesAsset) -> Unit)?) = + scope.launchTry(Dispatchers.IO) { + statusFlow.value = LoadStatus.Loading() + try { + val response = + client.submitFormWithBinaryData(url = FILE_UPLOAD_URL, formData = formData { + append("\"file\"", file.readBytes(), Headers.build { + append(HttpHeaders.ContentType, "application/x-zip-compressed") + append(HttpHeaders.ContentDisposition, "filename=\"file.zip\"") + }) + }) { + onUpload { bytesSentTotal, contentLength -> + if (statusFlow.value is LoadStatus.Loading) { + statusFlow.value = + LoadStatus.Loading(bytesSentTotal / contentLength.toFloat()) + } } } + if (response.headers["X_RPC_OK"] == "true") { + val policiesAsset = response.body() + statusFlow.value = LoadStatus.Success(policiesAsset) + onSuccessResult?.invoke(policiesAsset) + } else if (response.headers["X_RPC_OK"] == "false") { + statusFlow.value = LoadStatus.Failure(response.body()) + } else { + statusFlow.value = LoadStatus.Failure(Exception(response.bodyAsText())) } - if (response.headers["X_RPC_OK"] == "true") { - val policiesAsset = response.body() - statusFlow.value = LoadStatus.Success(policiesAsset) - } else if (response.headers["X_RPC_OK"] == "false") { - statusFlow.value = LoadStatus.Failure(response.body()) - } else { - statusFlow.value = LoadStatus.Failure(Exception(response.bodyAsText())) + } catch (e: Exception) { + statusFlow.value = LoadStatus.Failure(e) + } finally { + job = null } - } catch (e: Exception) { - statusFlow.value = LoadStatus.Failure(e) - } finally { - job = null } - } - fun startTask(file: File) { + fun startTask(file: File, onSuccessResult: ((GithubPoliciesAsset) -> Unit)? = null) { if (job != null || statusFlow.value is LoadStatus.Loading) { return } - job = buildTask(file) + job = buildTask(file, onSuccessResult) } private fun stopTask() { @@ -104,8 +107,9 @@ class UploadOptions( } is LoadStatus.Success -> { + val href = showHref(status.result) AlertDialog(title = { Text(text = "上传完成") }, text = { - Text(text = status.result.shortHref) + Text(text = href) }, onDismissRequest = {}, dismissButton = { TextButton(onClick = { statusFlow.value = null @@ -114,7 +118,7 @@ class UploadOptions( } }, confirmButton = { TextButton(onClick = { - ClipboardUtils.copyText(status.result.shortHref) + ClipboardUtils.copyText(href) toast("复制成功") statusFlow.value = null }) {