Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
ShenghuoquanBusiness
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bp
ShenghuoquanBusiness
Commits
36fe67f8
Commit
36fe67f8
authored
May 21, 2021
by
yinjiacheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add 金山云存储获取token
parent
93484557
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
38 deletions
+83
-38
KSYunTokenBean.kt
...om/yidian/shenghuoquan/newscontent/bean/KSYunTokenBean.kt
+16
-0
Ks3Core.kt
...java/com/yidian/shenghuoquan/newscontent/utils/Ks3Core.kt
+67
-38
No files found.
Components/newscontent/src/main/java/com/yidian/shenghuoquan/newscontent/bean/KSYunTokenBean.kt
0 → 100644
View file @
36fe67f8
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
)
Components/newscontent/src/main/java/com/yidian/shenghuoquan/newscontent/utils/Ks3Core.kt
View file @
36fe67f8
...
...
@@ -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
fileP
ath 本地文件全路径
* @param
p
ath 本地文件全路径
*/
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: $H
eaders"
"
KSYun calculate auth, httpMethod: $httpMethod, contentType: $contentType, date: $date, contentMD5: $contentMD5, resource: $resource, headers: $h
eaders"
)
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment