Commit bfd5e754 authored by yinjiacheng's avatar yinjiacheng

update Ks3Core逻辑

parent 1eb0eeab
...@@ -66,7 +66,6 @@ class NewsContentApplication : YdBaseApplication() { ...@@ -66,7 +66,6 @@ class NewsContentApplication : YdBaseApplication() {
initVideo() initVideo()
initChameleon() initChameleon()
initChameleonService() initChameleonService()
initKs3Core()
} }
} }
...@@ -328,10 +327,6 @@ class NewsContentApplication : YdBaseApplication() { ...@@ -328,10 +327,6 @@ class NewsContentApplication : YdBaseApplication() {
PlayerFactory.setPlayManager(CustomPlayerManager::class.java) PlayerFactory.setPlayManager(CustomPlayerManager::class.java)
} }
private fun initKs3Core() {
Ks3Core.instance.init(this)
}
override fun onTerminate() { override fun onTerminate() {
super.onTerminate() super.onTerminate()
ChameleonServiceManager.unregisterAllChameleonService() ChameleonServiceManager.unregisterAllChameleonService()
......
...@@ -11,10 +11,11 @@ import com.ksyun.ks3.services.Ks3ClientConfiguration ...@@ -11,10 +11,11 @@ import com.ksyun.ks3.services.Ks3ClientConfiguration
import com.ksyun.ks3.services.handler.PutObjectACLResponseHandler import com.ksyun.ks3.services.handler.PutObjectACLResponseHandler
import com.ksyun.ks3.services.handler.PutObjectResponseHandler import com.ksyun.ks3.services.handler.PutObjectResponseHandler
import com.ksyun.ks3.services.request.PutObjectRequest import com.ksyun.ks3.services.request.PutObjectRequest
import com.yidian.common.YdBaseApplication
import com.yidian.framework.mobile.xdiamond.SecretUtil import com.yidian.framework.mobile.xdiamond.SecretUtil
import com.yidian.shenghuoquan.newscontent.bean.KSYunTokenBean import com.yidian.shenghuoquan.newscontent.bean.KSYunTokenBean
import com.yidian.shenghuoquan.newscontent.http.ApiService import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.http.httpbean.GetKSYunTokenBean import com.yidian.shenghuoquan.newscontent.http.httpbean.*
import com.yidian.utils.MD5Util import com.yidian.utils.MD5Util
import com.yidian.yac.ftdevicefinger.core.FtDeviceFingerManager import com.yidian.yac.ftdevicefinger.core.FtDeviceFingerManager
import cz.msebera.android.httpclient.Header import cz.msebera.android.httpclient.Header
...@@ -25,45 +26,13 @@ import java.io.File ...@@ -25,45 +26,13 @@ import java.io.File
* date: 5/20/21 11:08 AM * date: 5/20/21 11:08 AM
* description: 金山云存储 * description: 金山云存储
*/ */
class Ks3Core private constructor() : AuthListener { class Ks3Core private constructor(val context: Context) : AuthListener {
private lateinit var client: Ks3Client private val client by lazy { Ks3Client(this, context) }
private lateinit var putObjectResponseHandler: PutObjectResponseHandler private val gson by lazy { Gson() }
private lateinit var putObjectACLResponseHandler: PutObjectACLResponseHandler
private lateinit var json: Gson
private val onKs3UploadListenerList: ArrayList<OnKs3UploadListener> by lazy { ArrayList<OnKs3UploadListener>() } private val onKs3UploadListenerList: ArrayList<OnKs3UploadListener> by lazy { ArrayList<OnKs3UploadListener>() }
private val putObjectResponseHandler by lazy {
companion object { object : PutObjectResponseHandler() {
const val TAG = "Ks3Core"
const val BUCKET_NAME = "bp-yidian"
const val END_POINT = "ks3-cn-beijing.ksyun.com"
const val VIDEO_SUFFIX = ".mp4"
const val IMAGE_SUFFIX = ".png"
val instance: Ks3Core by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
Ks3Core()
}
}
/**
* 初始化
*/
fun init(context: Context) {
initClient(context)
initListener()
initOther()
}
/**
* 初始化金山云存储client
*/
private fun initClient(context: Context) {
client = Ks3Client(this, context)
client.setConfiguration(Ks3ClientConfiguration.getDefaultConfiguration())
client.setEndpoint(END_POINT)
}
private fun initListener() {
putObjectResponseHandler = object : PutObjectResponseHandler() {
override fun onTaskProgress(progress: Double) { override fun onTaskProgress(progress: Double) {
// work thread // work thread
if (onKs3UploadListenerList.size > 0) { if (onKs3UploadListenerList.size > 0) {
...@@ -82,7 +51,7 @@ class Ks3Core private constructor() : AuthListener { ...@@ -82,7 +51,7 @@ class Ks3Core private constructor() : AuthListener {
) { ) {
Log.e( Log.e(
TAG, TAG,
"put object fail:, stateCode: $statesCode, errorMsg: ${error?.errorMessage}, response: $response" "put object fail:, errorMsg: ${error?.errorMessage}"
) )
if (onKs3UploadListenerList.size > 0) { if (onKs3UploadListenerList.size > 0) {
for (listener in onKs3UploadListenerList) { for (listener in onKs3UploadListenerList) {
...@@ -123,10 +92,11 @@ class Ks3Core private constructor() : AuthListener { ...@@ -123,10 +92,11 @@ class Ks3Core private constructor() : AuthListener {
} }
} }
} }
} }
}
putObjectACLResponseHandler = object : PutObjectACLResponseHandler() { private val putObjectACLResponseHandler by lazy {
object : PutObjectACLResponseHandler() {
override fun onSuccess(statesCode: Int, responceHeaders: Array<out Header>?) { override fun onSuccess(statesCode: Int, responceHeaders: Array<out Header>?) {
Log.d(TAG, "put object acl success.") Log.d(TAG, "put object acl success.")
} }
...@@ -140,34 +110,68 @@ class Ks3Core private constructor() : AuthListener { ...@@ -140,34 +110,68 @@ class Ks3Core private constructor() : AuthListener {
) { ) {
Log.e(TAG, "put object acl fail:, errorMsg: ${error?.errorMessage}") Log.e(TAG, "put object acl fail:, errorMsg: ${error?.errorMessage}")
} }
}
}
companion object {
const val TAG = "Ks3Core"
const val DEFAULT_BUCKET_NAME = "bp-yidian"
const val DEFAULT_END_POINT = "ks3-cn-beijing.ksyun.com"
const val VIDEO_SUFFIX = ".mp4"
const val IMAGE_SUFFIX = ".png"
val instance: Ks3Core by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
Ks3Core(context = YdBaseApplication.context)
} }
} }
private fun initOther() { init {
json = Gson() setupClient()
}
/**
* 配置金山云client
*/
private fun setupClient() {
client.setConfiguration(Ks3ClientConfiguration.getDefaultConfiguration())
client.setEndpoint(DEFAULT_END_POINT)
} }
/** /**
* 文件上传 * 文件上传
* @param path 本地文件全路径 * @param path 本地文件全路径
*/ */
fun uploadObject(path: String, type: ObjectType) { fun uploadObject(path: String) {
val file = File(path) ApiService.getKSYunObjectId(object : IGetKSYunObjectIdCallback {
val request = PutObjectRequest(BUCKET_NAME, getObjectKey(file.name, type), file) override fun getKSYunObjectIdSuccess(result: GetKSYunObjectIdBean.Response?) {
request.cannedAcl = CannedAccessControlList.PublicRead Log.d(
client.putObject(request, putObjectResponseHandler) TAG,
"request getKSYun objectId success, objectId: ${result?.objectid}, bucket: ${result?.bucket}"
)
// 执行金山云上传
// val request = PutObjectRequest(result?.bucket, result?.objectid, File(path))
val request = PutObjectRequest(DEFAULT_BUCKET_NAME, result?.objectid, File(path))
request.cannedAcl = CannedAccessControlList.PublicRead
client.putObject(request, putObjectResponseHandler)
}
override fun getKSYunObjectIdFailure(message: String?) {
Log.e(TAG, "request get KSYun objectId fail, message: $message")
}
})
} }
/** /**
* 获取ObjectKey * 本地生成ObjectKey
* 作为Object在云空间的唯一标识 * 作为Object在云空间的唯一标识
*/ */
private fun getObjectKey(name: String, type: ObjectType): String { private fun getLocalObjectKey(name: String, type: ObjectType): String {
return MD5Util.md5Encrypt32Upper(name + FtDeviceFingerManager.getDeviceFinger()) + return MD5Util.md5Encrypt32Upper(name + FtDeviceFingerManager.getDeviceFinger()) +
if (type == ObjectType.VIDEO) VIDEO_SUFFIX else IMAGE_SUFFIX if (type == ObjectType.VIDEO) VIDEO_SUFFIX else IMAGE_SUFFIX
} }
/**
* 调用金山云API后 SDK会回调此函数用于获取必要参数向AppServer请求token
*/
override fun onCalculateAuth( override fun onCalculateAuth(
httpMethod: String, httpMethod: String,
contentType: String, contentType: String,
...@@ -183,7 +187,7 @@ class Ks3Core private constructor() : AuthListener { ...@@ -183,7 +187,7 @@ class Ks3Core private constructor() : AuthListener {
"KSYun calculate auth, httpMethod: $httpMethod, contentType: $contentType, date: $date, contentMD5: $contentMD5, resource: $resource, headers: $headers" "KSYun calculate auth, httpMethod: $httpMethod, contentType: $contentType, date: $date, contentMD5: $contentMD5, resource: $resource, headers: $headers"
) )
val tok = SecretUtil.rsaEncrypt( val tok = SecretUtil.rsaEncrypt(
json.toJson( gson.toJson(
KSYunTokenBean( KSYunTokenBean(
SecretUtil.sign(httpMethod + date + resource), SecretUtil.sign(httpMethod + date + resource),
httpMethod, httpMethod,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment