Skip to content

Commit 3f21b49

Browse files
committed
fix: snapshot import url (gkd-kit#695)
1 parent ae0d7c7 commit 3f21b49

File tree

3 files changed

+52
-35
lines changed

3 files changed

+52
-35
lines changed

app/src/main/kotlin/li/songe/gkd/ui/SnapshotPage.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import com.ramcosta.composedestinations.navigation.navigate
4949
import kotlinx.coroutines.Dispatchers
5050
import kotlinx.coroutines.withContext
5151
import li.songe.gkd.MainActivity
52+
import li.songe.gkd.data.GithubPoliciesAsset
5253
import li.songe.gkd.data.Snapshot
5354
import li.songe.gkd.db.DbSet
5455
import li.songe.gkd.debug.SnapshotExt
@@ -251,7 +252,16 @@ fun SnapshotPage() {
251252
text = "生成链接(需科学上网)", modifier = Modifier
252253
.clickable(onClick = vm.viewModelScope.launchAsFn(Dispatchers.IO) {
253254
selectedSnapshot = null
254-
vm.uploadOptions.startTask(SnapshotExt.getSnapshotZipFile(snapshotVal.id))
255+
vm.uploadOptions.startTask(
256+
file = SnapshotExt.getSnapshotZipFile(
257+
snapshotVal.id
258+
),
259+
onSuccessResult = vm.viewModelScope.launchAsFn<GithubPoliciesAsset>(
260+
Dispatchers.IO
261+
) {
262+
DbSet.snapshotDao.update(snapshotVal.copy(githubAssetId = it.id))
263+
}
264+
)
255265
})
256266
.then(modifier)
257267
)

app/src/main/kotlin/li/songe/gkd/ui/SnapshotVm.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.SharingStarted
77
import kotlinx.coroutines.flow.stateIn
88
import li.songe.gkd.db.DbSet
99
import li.songe.gkd.ui.component.UploadOptions
10+
import li.songe.gkd.util.IMPORT_BASE_URL
1011
import javax.inject.Inject
1112

1213

@@ -15,6 +16,8 @@ class SnapshotVm @Inject constructor() : ViewModel() {
1516
val snapshotsState = DbSet.snapshotDao.query()
1617
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())
1718

18-
19-
val uploadOptions = UploadOptions(viewModelScope)
19+
val uploadOptions = UploadOptions(
20+
scope = viewModelScope,
21+
showHref = { IMPORT_BASE_URL + it.id }
22+
)
2023
}

app/src/main/kotlin/li/songe/gkd/ui/component/UploadOptions.kt

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,50 @@ import li.songe.gkd.util.toast
2929
import java.io.File
3030

3131
class UploadOptions(
32-
private val scope: CoroutineScope
32+
private val scope: CoroutineScope,
33+
private val showHref: (GithubPoliciesAsset) -> String = { it.shortHref }
3334
) {
34-
private val statusFlow = MutableStateFlow<LoadStatus<GithubPoliciesAsset>?>(null)
35+
val statusFlow = MutableStateFlow<LoadStatus<GithubPoliciesAsset>?>(null)
3536
private var job: Job? = null
36-
private fun buildTask(file: File) = scope.launchTry(Dispatchers.IO) {
37-
statusFlow.value = LoadStatus.Loading()
38-
try {
39-
val response =
40-
client.submitFormWithBinaryData(url = FILE_UPLOAD_URL, formData = formData {
41-
append("\"file\"", file.readBytes(), Headers.build {
42-
append(HttpHeaders.ContentType, "application/x-zip-compressed")
43-
append(HttpHeaders.ContentDisposition, "filename=\"file.zip\"")
44-
})
45-
}) {
46-
onUpload { bytesSentTotal, contentLength ->
47-
if (statusFlow.value is LoadStatus.Loading) {
48-
statusFlow.value =
49-
LoadStatus.Loading(bytesSentTotal / contentLength.toFloat())
37+
private fun buildTask(file: File, onSuccessResult: ((GithubPoliciesAsset) -> Unit)?) =
38+
scope.launchTry(Dispatchers.IO) {
39+
statusFlow.value = LoadStatus.Loading()
40+
try {
41+
val response =
42+
client.submitFormWithBinaryData(url = FILE_UPLOAD_URL, formData = formData {
43+
append("\"file\"", file.readBytes(), Headers.build {
44+
append(HttpHeaders.ContentType, "application/x-zip-compressed")
45+
append(HttpHeaders.ContentDisposition, "filename=\"file.zip\"")
46+
})
47+
}) {
48+
onUpload { bytesSentTotal, contentLength ->
49+
if (statusFlow.value is LoadStatus.Loading) {
50+
statusFlow.value =
51+
LoadStatus.Loading(bytesSentTotal / contentLength.toFloat())
52+
}
5053
}
5154
}
55+
if (response.headers["X_RPC_OK"] == "true") {
56+
val policiesAsset = response.body<GithubPoliciesAsset>()
57+
statusFlow.value = LoadStatus.Success(policiesAsset)
58+
onSuccessResult?.invoke(policiesAsset)
59+
} else if (response.headers["X_RPC_OK"] == "false") {
60+
statusFlow.value = LoadStatus.Failure(response.body<RpcError>())
61+
} else {
62+
statusFlow.value = LoadStatus.Failure(Exception(response.bodyAsText()))
5263
}
53-
if (response.headers["X_RPC_OK"] == "true") {
54-
val policiesAsset = response.body<GithubPoliciesAsset>()
55-
statusFlow.value = LoadStatus.Success(policiesAsset)
56-
} else if (response.headers["X_RPC_OK"] == "false") {
57-
statusFlow.value = LoadStatus.Failure(response.body<RpcError>())
58-
} else {
59-
statusFlow.value = LoadStatus.Failure(Exception(response.bodyAsText()))
64+
} catch (e: Exception) {
65+
statusFlow.value = LoadStatus.Failure(e)
66+
} finally {
67+
job = null
6068
}
61-
} catch (e: Exception) {
62-
statusFlow.value = LoadStatus.Failure(e)
63-
} finally {
64-
job = null
6569
}
66-
}
6770

68-
fun startTask(file: File) {
71+
fun startTask(file: File, onSuccessResult: ((GithubPoliciesAsset) -> Unit)? = null) {
6972
if (job != null || statusFlow.value is LoadStatus.Loading) {
7073
return
7174
}
72-
job = buildTask(file)
75+
job = buildTask(file, onSuccessResult)
7376
}
7477

7578
private fun stopTask() {
@@ -104,8 +107,9 @@ class UploadOptions(
104107
}
105108

106109
is LoadStatus.Success -> {
110+
val href = showHref(status.result)
107111
AlertDialog(title = { Text(text = "上传完成") }, text = {
108-
Text(text = status.result.shortHref)
112+
Text(text = href)
109113
}, onDismissRequest = {}, dismissButton = {
110114
TextButton(onClick = {
111115
statusFlow.value = null
@@ -114,7 +118,7 @@ class UploadOptions(
114118
}
115119
}, confirmButton = {
116120
TextButton(onClick = {
117-
ClipboardUtils.copyText(status.result.shortHref)
121+
ClipboardUtils.copyText(href)
118122
toast("复制成功")
119123
statusFlow.value = null
120124
}) {

0 commit comments

Comments
 (0)