Commit 36fe67f8 authored by yinjiacheng's avatar yinjiacheng

add 金山云存储获取token

parent 93484557
package com.yidian.shenghuoquan.newscontent.bean
/**
* author: yinjiacheng
* date: 5/20/21 8:03 PM
* description: 金山云token 与AppServer约定的RSA加密原始数据格式
*/
data class KSYunTokenBean(
val secret: String?,
val http_method: String,
val date: String,
val resource: String,
val content_type: String,
val content_md5: String,
val headers: String
)
......@@ -2,6 +2,7 @@ package com.yidian.shenghuoquan.newscontent.utils
import android.content.Context
import android.util.Log
import com.google.gson.Gson
import com.ksyun.ks3.exception.Ks3Error
import com.ksyun.ks3.model.acl.CannedAccessControlList
import com.ksyun.ks3.services.AuthListener
......@@ -10,7 +11,12 @@ import com.ksyun.ks3.services.Ks3ClientConfiguration
import com.ksyun.ks3.services.handler.PutObjectACLResponseHandler
import com.ksyun.ks3.services.handler.PutObjectResponseHandler
import com.ksyun.ks3.services.request.PutObjectRequest
import com.yidian.common.utils.EncryptUtil
import com.yidian.framework.mobile.xdiamond.SecretUtil
import com.yidian.shenghuoquan.newscontent.bean.KSYunTokenBean
import com.yidian.shenghuoquan.newscontent.http.ApiService
import com.yidian.shenghuoquan.newscontent.http.httpbean.GetKSYunTokenBean
import com.yidian.utils.MD5Util
import com.yidian.yac.ftdevicefinger.core.FtDeviceFingerManager
import cz.msebera.android.httpclient.Header
import java.io.File
......@@ -24,12 +30,16 @@ class Ks3Core private constructor() : AuthListener {
private lateinit var client: Ks3Client
private lateinit var putObjectResponseHandler: PutObjectResponseHandler
private lateinit var putObjectACLResponseHandler: PutObjectACLResponseHandler
private lateinit var json: Gson
private val onKs3UploadListenerList: ArrayList<OnKs3UploadListener> by lazy { ArrayList<OnKs3UploadListener>() }
companion object {
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"
const val KSYUN_TOKEN_APPID = "merchant-b"
val instance: Ks3Core by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
Ks3Core()
}
......@@ -41,8 +51,12 @@ class Ks3Core private constructor() : AuthListener {
fun init(context: Context) {
initClient(context)
initListener()
initOther()
}
/**
* 初始化金山云存储client
*/
private fun initClient(context: Context) {
client = Ks3Client(this, context)
client.setConfiguration(Ks3ClientConfiguration.getDefaultConfiguration())
......@@ -67,7 +81,10 @@ class Ks3Core private constructor() : AuthListener {
response: String?,
throwable: Throwable?
) {
Log.e(TAG, "put object fail:, errorMsg: ${error?.errorMessage}")
Log.e(
TAG,
"put object fail:, stateCode: $statesCode, errorMsg: ${error?.errorMessage}, response: $response"
)
if (onKs3UploadListenerList.size > 0) {
for (listener in onKs3UploadListenerList) {
listener.onUploadFailure(error?.errorMessage)
......@@ -128,57 +145,59 @@ class Ks3Core private constructor() : AuthListener {
}
}
/**
* 文件上传
* @param filePath 本地文件全路径
*/
/*fun uploadFile(filePath: String) {
client.putObject(
BUCKET_NAME,
EncryptUtil.getMD5(filePath),
File(filePath),
putObjectResponseHandler
)
}*/
private fun initOther() {
json = Gson()
}
/**
* 文件上传
* @param filePath 本地文件全路径
* @param path 本地文件全路径
*/
fun uploadFile(filePath: String) {
val request = PutObjectRequest(BUCKET_NAME, EncryptUtil.getMD5(filePath), File(filePath))
request.cannedAcl = CannedAccessControlList.PublicRead
fun uploadObject(path: String, type: ObjectType) {
val file = File(path)
val request = PutObjectRequest(BUCKET_NAME, getObjectKey(file.name, type), file)
request.cannedAcl = CannedAccessControlList.Private
client.putObject(request, putObjectResponseHandler)
}
/**
* 为已存在于bucket中的object设置访问控制权限
* 获取ObjectKey
* 作为Object在云空间的唯一标识
*/
private fun putObjectACL(filePath: String) {
val controlList = CannedAccessControlList.PublicRead
client.putObjectACL(
BUCKET_NAME,
EncryptUtil.getMD5(filePath),
controlList,
putObjectACLResponseHandler
)
private fun getObjectKey(name: String, type: ObjectType): String {
return MD5Util.md5Encrypt32Upper(name + FtDeviceFingerManager.getDeviceFinger()) +
if (type == ObjectType.VIDEO) VIDEO_SUFFIX else IMAGE_SUFFIX
}
override fun onCalculateAuth(
httpMethod: String?,
ContentType: String?,
Date: String?,
ContentMD5: String?,
Resource: String?,
Headers: String?
): String {
// 此处由APP端向业务服务器发送post请求返回Token
// 需要注意该回调方法运行在非主线程
httpMethod: String,
contentType: String,
date: String,
contentMD5: String,
resource: String,
headers: String
): String? {
// 此处由APP端向业务服务器发送post请求返回Token
// 该回调方法运行在非主线程
Log.d(
TAG,
"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"
)
return ""
val tok = SecretUtil.rsaEncrypt(
json.toJson(
KSYunTokenBean(
SecretUtil.sign(httpMethod + date + resource),
httpMethod,
date,
resource,
contentType,
contentMD5,
headers
)
)
)
return ApiService.getKSYunToken(GetKSYunTokenBean.Request(KSYUN_TOKEN_APPID, tok))
?.result?.token
}
fun addOnKs3UploadListener(listener: OnKs3UploadListener) {
......@@ -191,6 +210,9 @@ class Ks3Core private constructor() : AuthListener {
onKs3UploadListenerList.remove(listener)
}
/**
* 业务监听
*/
interface OnKs3UploadListener {
fun onUploadStart()
fun onUploadProgress(progress: Double)
......@@ -200,4 +222,11 @@ class Ks3Core private constructor() : AuthListener {
fun onUploadFailure(message: String?)
}
/**
* 上传对象的类型
*/
enum class ObjectType {
VIDEO, IMAGE
}
}
\ No newline at end of file
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