feat:文件上传支持自定义域名替换

This commit is contained in:
Max
2024-03-22 17:21:54 +08:00
parent 0695a74520
commit 4af7fcf877
2 changed files with 19 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package com.nnbc123.core.file.cos
import android.content.Context
import android.net.Uri
import android.util.Log
import com.nnbc123.core.Env
import com.tencent.cos.xml.CosXmlService
@@ -65,6 +66,7 @@ object CosClient {
uploadFile(context, file, outName, token).apply {
setCosXmlResultListener(object : CosXmlResultListener {
override fun onSuccess(request: CosXmlRequest?, result: CosXmlResult) {
transformDomain(result, token)
if (Env.isDebug()) {
Log.e("CosClient", "upload onSuccess result:${result.accessUrl}")
}
@@ -87,6 +89,22 @@ object CosClient {
}
}
private fun transformDomain(result: CosXmlResult, token: CosToken) {
try {
if (result.accessUrl.isNullOrEmpty()) {
return
}
if (token.customDomain.isNullOrEmpty()) {
return
}
val newUri = Uri.parse(token.customDomain)
result.accessUrl = Uri.parse(result.accessUrl).buildUpon().scheme(newUri.scheme)
.authority(newUri.authority).build().toString()
} catch (e: Exception) {
e.printStackTrace()
}
}
/**
* 上传文件
* @param outName 远端文件名

View File

@@ -16,6 +16,7 @@ data class CosToken(
val region: String?,
val startTime: Long?,
val expireTime: Long?,
val customDomain: String?
) {
/**