feat: 替换文件上传 SDK https://jira.shanqu.cc/browse/GHZS-3763
This commit is contained in:
@ -301,7 +301,6 @@ class CloudArchiveManagerActivity : BaseActivity_TabLayout(), ArchiveLimitSelect
|
||||
toast("存档上传失败")
|
||||
}
|
||||
},
|
||||
null,
|
||||
mGameEntity?.id ?: "",
|
||||
entity.md5
|
||||
)
|
||||
|
||||
@ -560,7 +560,7 @@ class VideoPublishFragment : ToolbarFragment(), KeyboardHeightObserver {
|
||||
totalSize: Long,
|
||||
speed: Long
|
||||
) {
|
||||
runOnUiThread {
|
||||
mBaseHandler.post {
|
||||
mBinding.uploadInfoContainer.visibility = View.VISIBLE
|
||||
mBinding.uploadStatus.text = "视频上传中..."
|
||||
mBinding.uploadSpeed.visibility = View.VISIBLE
|
||||
|
||||
@ -1651,9 +1651,9 @@ public interface ApiService {
|
||||
Observable<List<CommentEntity>> getVideoCommentConversationList(@Path("video_id") String videoId, @Path("comment_id") String commentId, @Query("page") int page);
|
||||
|
||||
/**
|
||||
* 获取阿里云上传配置
|
||||
* 获取火山云上传配置
|
||||
*/
|
||||
@GET("sts/oss?type=user")
|
||||
@GET("sts/tos?type=user")
|
||||
Single<OssEntity> getOssUpdateConfig();
|
||||
|
||||
/**
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package com.gh.gamecenter.video.upload
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import com.alibaba.sdk.android.oss.common.OSSLog
|
||||
import com.gh.gamecenter.core.utils.MD5Utils
|
||||
import com.gh.gamecenter.common.utils.tryCatchInRelease
|
||||
import com.gh.gamecenter.BuildConfig
|
||||
@ -21,10 +20,6 @@ object UploadManager : OnUploadListener {
|
||||
|
||||
private val mUploadDao = AppDatabase.getInstance().uploadDao()
|
||||
|
||||
init {
|
||||
if (BuildConfig.DEBUG) OSSLog.enableLog()
|
||||
}
|
||||
|
||||
override fun onProgressChanged(uploadFilePath: String, currentSize: Long, totalSize: Long, speed: Long) {
|
||||
mUploadListenerMap[uploadFilePath]?.onProgressChanged(uploadFilePath, currentSize, totalSize, speed)
|
||||
}
|
||||
@ -61,7 +56,7 @@ object UploadManager : OnUploadListener {
|
||||
if (mUploadThreadMap[uploadFilePath]?.isAlive == true) return
|
||||
|
||||
RetrofitManager.getInstance()
|
||||
.api
|
||||
.newApi
|
||||
.ossUpdateConfig
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
|
||||
@ -1,21 +1,18 @@
|
||||
package com.gh.gamecenter.video.upload
|
||||
|
||||
import android.os.Environment
|
||||
import com.alibaba.sdk.android.oss.ClientConfiguration
|
||||
import com.alibaba.sdk.android.oss.ClientException
|
||||
import com.alibaba.sdk.android.oss.OSSClient
|
||||
import com.alibaba.sdk.android.oss.ServiceException
|
||||
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback
|
||||
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback
|
||||
import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider
|
||||
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask
|
||||
import com.alibaba.sdk.android.oss.model.*
|
||||
import com.gh.gamecenter.common.utils.NetworkUtils
|
||||
import com.gh.gamecenter.common.entity.OssEntity
|
||||
import com.gh.gamecenter.common.utils.NetworkUtils
|
||||
import com.halo.assistant.HaloApp
|
||||
import com.lightgame.utils.Utils
|
||||
import com.volcengine.tos.TOSClientConfiguration
|
||||
import com.volcengine.tos.TOSV2ClientBuilder
|
||||
import com.volcengine.tos.auth.StaticCredentials
|
||||
import com.volcengine.tos.model.`object`.UploadFileV2Input
|
||||
import com.volcengine.tos.transport.TransportConfig
|
||||
import java.io.File
|
||||
|
||||
|
||||
/**
|
||||
* todo BUG 上传过程中遇到网络问题中断 onFailure()方法无法响应
|
||||
* https://github.com/aliyun/aliyun-oss-android-sdk/issues/75
|
||||
@ -25,7 +22,7 @@ class UploadThread(
|
||||
private val mUploadListener: OnUploadListener
|
||||
) : Thread() {
|
||||
|
||||
private var mOssAsyncTask: OSSAsyncTask<ResumableUploadResult>? = null
|
||||
private var mProgressCallbackTimeInMills: Long = 0
|
||||
|
||||
private var mLastSize: Long = 0
|
||||
private var mLastTime: Long = 0
|
||||
@ -35,64 +32,80 @@ class UploadThread(
|
||||
private val mMaxRetryCount = 3
|
||||
private var mRetryCount: Int = 0
|
||||
|
||||
private var mUploadInput: UploadFileV2Input? = null
|
||||
|
||||
override fun run() {
|
||||
super.run()
|
||||
|
||||
val conf = ClientConfiguration()
|
||||
conf.connectionTimeout = 15 * 1000
|
||||
conf.socketTimeout = 15 * 1000
|
||||
conf.maxConcurrentRequest = 5
|
||||
conf.maxErrorRetry = 2
|
||||
|
||||
val credentialProvider =
|
||||
OSSStsTokenCredentialProvider(mOssEntity.accessKeyId, mOssEntity.accessKeySecret, mOssEntity.securityToken)
|
||||
val oss = OSSClient(HaloApp.getInstance().application, mOssEntity.endPoint, credentialProvider, conf)
|
||||
|
||||
val recordDirectory = Environment.getExternalStorageDirectory().absolutePath + "/oss_record/"
|
||||
val recordDir = File(recordDirectory)
|
||||
if (!recordDir.exists()) {
|
||||
recordDir.mkdirs()
|
||||
}
|
||||
|
||||
val request =
|
||||
ResumableUploadRequest(mOssEntity.bucket, mOssEntity.key, mOssEntity.uploadFilePath, recordDirectory)
|
||||
request.setDeleteUploadOnCancelling(false)
|
||||
request.progressCallback = OSSProgressCallback<ResumableUploadRequest> { _,
|
||||
currentSize,
|
||||
totalSize ->
|
||||
mUploadListener.onProgressChanged(
|
||||
try {
|
||||
val input = UploadFileV2Input()
|
||||
.setKey(mOssEntity.key)
|
||||
.setBucket(mOssEntity.bucket)
|
||||
.setFilePath(mOssEntity.uploadFilePath)
|
||||
.setEnableCheckpoint(true)
|
||||
.setCancelHook(true)
|
||||
.setCheckpointFile(recordDirectory)
|
||||
.setPartSize(10 * 1024 * 1024)
|
||||
.setTaskNum(2)
|
||||
.setDataTransferListener {
|
||||
Utils.log(
|
||||
"OssUpload",
|
||||
"upload progress changed, type: ${it.type}, consumedBytes -> ${it.consumedBytes}, totalBytes -> ${it.totalBytes}"
|
||||
)
|
||||
val currentTimeInMills = System.currentTimeMillis()
|
||||
if (currentTimeInMills - mProgressCallbackTimeInMills > 200L) {
|
||||
// consumedBytes和totalBytes以字节为单位
|
||||
mUploadListener.onProgressChanged(
|
||||
mOssEntity.uploadFilePath,
|
||||
it.consumedBytes / 1000,
|
||||
it.totalBytes / 1000,
|
||||
getSpeed(it.consumedBytes / 1000)
|
||||
)
|
||||
mProgressCallbackTimeInMills = currentTimeInMills
|
||||
}
|
||||
}
|
||||
.apply {
|
||||
mUploadInput = this
|
||||
}
|
||||
val config = TransportConfig.builder()
|
||||
.readTimeoutMills(15 * 1000)
|
||||
.writeTimeoutMills(15 * 1000)
|
||||
.connectTimeoutMills(15 * 1000)
|
||||
.maxRetryCount(2)
|
||||
.build()
|
||||
|
||||
val configuration = TOSClientConfiguration.builder()
|
||||
.transportConfig(config)
|
||||
.region(mOssEntity.region)
|
||||
.endpoint(mOssEntity.endPoint)
|
||||
.credentials(
|
||||
StaticCredentials(mOssEntity.accessKey, mOssEntity.secretKey)
|
||||
.withSecurityToken(mOssEntity.securityToken)
|
||||
)
|
||||
.build()
|
||||
|
||||
val tos = TOSV2ClientBuilder().build(configuration)
|
||||
|
||||
tos.uploadFile(input)
|
||||
Utils.log("OssUpload", "upload succeed, path -> ${mOssEntity.uploadFilePath}")
|
||||
mRetryCount = 0
|
||||
mNeedInvokeFailureInCompleted = false
|
||||
mUploadListener.onUploadSuccess(mOssEntity.uploadFilePath, mOssEntity.domain + mOssEntity.key)
|
||||
} catch (e: Throwable) {
|
||||
Utils.log("OssUpload", "upload failed, exception -> $e")
|
||||
mNeedInvokeFailureInCompleted = false
|
||||
mUploadListener.onUploadFailure(
|
||||
mOssEntity.uploadFilePath,
|
||||
currentSize / 1000,
|
||||
totalSize / 1000,
|
||||
getSpeed(currentSize / 1000)
|
||||
e.message ?: "$e"
|
||||
)
|
||||
}
|
||||
|
||||
mOssAsyncTask = oss.asyncResumableUpload(
|
||||
request,
|
||||
object : OSSCompletedCallback<ResumableUploadRequest, ResumableUploadResult> {
|
||||
override fun onSuccess(request: ResumableUploadRequest, result: ResumableUploadResult) {
|
||||
mRetryCount = 0
|
||||
mNeedInvokeFailureInCompleted = false
|
||||
mUploadListener.onUploadSuccess(mOssEntity.uploadFilePath, mOssEntity.domain + mOssEntity.key)
|
||||
|
||||
}
|
||||
|
||||
override fun onFailure(
|
||||
request: ResumableUploadRequest,
|
||||
clientExcepion: ClientException,
|
||||
serviceException: ServiceException
|
||||
) {
|
||||
mNeedInvokeFailureInCompleted = false
|
||||
mUploadListener.onUploadFailure(
|
||||
mOssEntity.uploadFilePath,
|
||||
clientExcepion.message + "/" + serviceException.message
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
mOssAsyncTask?.waitUntilFinished()
|
||||
|
||||
Utils.log(UploadThread::class.java.simpleName + "=>upload task completed::" + mNeedInvokeFailureInCompleted)
|
||||
if (mNeedInvokeFailureInCompleted) {
|
||||
if (File(mOssEntity.uploadFilePath).exists() &&
|
||||
@ -111,19 +124,28 @@ class UploadThread(
|
||||
|
||||
// kb/s
|
||||
private fun getSpeed(currentSize: Long): Long {
|
||||
return if (mLastTime != 0L) {
|
||||
val time = System.currentTimeMillis() - mLastTime
|
||||
val currentTime = System.currentTimeMillis()
|
||||
Utils.log(
|
||||
"OssUpload",
|
||||
"getSpeed currentSize: $currentSize currentTime: $currentTime, lastTime: $mLastTime, lastSize: $mLastSize"
|
||||
)
|
||||
return if (currentTime != mLastTime && mLastTime != 0L) {
|
||||
val time = currentTime - mLastTime
|
||||
val size = currentSize - mLastSize
|
||||
size / (time / 1000L)
|
||||
} else {
|
||||
mLastTime = currentTime
|
||||
mLastSize = currentSize
|
||||
size * 1000 / time
|
||||
} else if (mLastTime == 0L) {
|
||||
mLastTime = System.currentTimeMillis()
|
||||
mLastSize = currentSize
|
||||
0
|
||||
} else {// currentTime == mLastTime
|
||||
mLastSize * 1000 / mLastTime
|
||||
}
|
||||
}
|
||||
|
||||
fun cancel() {
|
||||
mNeedInvokeFailureInCompleted = false
|
||||
mOssAsyncTask?.cancel()
|
||||
mUploadInput?.cancelHook?.cancel(false)
|
||||
}
|
||||
}
|
||||
@ -790,7 +790,7 @@ class UploadVideoActivity : ToolBarActivity() {
|
||||
mBinding.uploadStatus.text = "视频上传中..."
|
||||
UploadManager.createUploadTask(videoPath, object : OnUploadListener {
|
||||
override fun onProgressChanged(uploadFilePath: String, currentSize: Long, totalSize: Long, speed: Long) {
|
||||
runOnUiThread {
|
||||
mBaseHandler.post {
|
||||
mBinding.uploadStatus.text = "视频上传中..."
|
||||
mBinding.uploadSpeed.visibility = View.VISIBLE
|
||||
mBinding.uploadButton.visibility = View.VISIBLE
|
||||
|
||||
@ -26,6 +26,7 @@ import com.gh.gamecenter.common.baselist.ListFragment
|
||||
import com.gh.gamecenter.common.callback.CancelListener
|
||||
import com.gh.gamecenter.common.constant.EntranceConsts
|
||||
import com.gh.gamecenter.common.utils.*
|
||||
import com.gh.gamecenter.core.runOnUiThread
|
||||
import com.gh.gamecenter.core.utils.MtaHelper.onEvent
|
||||
import com.gh.gamecenter.core.utils.ToastUtils
|
||||
import com.gh.gamecenter.databinding.DialogAddVoteBinding
|
||||
@ -215,12 +216,14 @@ class VoteFragment : ListFragment<VersionVoteEntity, VoteViewModel>() {
|
||||
mListViewModel.postApkInfo(mApkUrl, installGameEntity)
|
||||
}
|
||||
|
||||
override fun onProgress(consumeBytes: Long, totalBytes: Long) {
|
||||
val progress = (consumeBytes / totalBytes.toFloat()) * 357 //即最多99%
|
||||
mBaseHandler.post { mUploadDialog.updateProgress(progress.toInt()) }
|
||||
}
|
||||
|
||||
override fun onError(e: Throwable?) {
|
||||
mUploadDialog.uploadFail()
|
||||
}
|
||||
}, { _, currentSize, totalSize ->
|
||||
val progress = (currentSize / totalSize.toFloat()) * 357 //即最多99%
|
||||
mUploadDialog.updateProgress(progress.toInt())
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user