作者 yangfu

Merge branch 'dev' of http://gitlab.fjmaimaimai.com/mmm-go/openapi into dev

... ... @@ -8,6 +8,7 @@ type OssConfig struct {
bucketName string
callbackUrl string
sessionName string
hostStr string //host的格式为 bucketname.endpoint
}
func NewOssConfig() *OssConfig {
... ... @@ -19,6 +20,7 @@ func NewOssConfig() *OssConfig {
callbackUrl: "",
endPoint: "",
sessionName: "",
hostStr: "",
}
}
... ...
package oss
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"hash"
"io"
"time"
)
type PolicyToken struct {
AccessKeyId string `json:"accessid"`
Host string `json:"host"`
... ... @@ -10,54 +21,63 @@ type PolicyToken struct {
Callback string `json:"callback"`
}
type PolicyConfigStruct struct {
Expiration string `json:"expiration"`
Conditions [][]string `json:"conditions"`
}
type CallbackParam struct {
CallbackUrl string `json:"callbackUrl"`
CallbackBody string `json:"callbackBody"`
CallbackBodyType string `json:"callbackBodyType"`
}
// func get_policy_token() string {
// now := time.Now().Unix()
// expire_end := now + expire_time
// var tokenExpire = get_gmt_iso8601(expire_end)
var (
// 用户上传文件时指定的文件夹。
upload_dir = "demo/"
)
// //create post policy json
// var config ConfigStruct
// config.Expiration = tokenExpire
// var condition []string
// condition = append(condition, "starts-with")
// condition = append(condition, "$key")
// condition = append(condition, upload_dir)
// config.Conditions = append(config.Conditions, condition)
func GetPolicyToken() string {
now := time.Now().Unix()
expire_end := now + 30
var tokenExpire = time.Unix(expire_end, 0).Format("2006-01-02T15:04:05Z")
// //calucate signature
// result,err:=json.Marshal(config)
// debyte := base64.StdEncoding.EncodeToString(result)
// h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(accessKeySecret))
// io.WriteString(h, debyte)
// signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
// var callbackParam CallbackParam
// callbackParam.CallbackUrl = callbackUrl
// callbackParam.CallbackBody = "filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}"
// callbackParam.CallbackBodyType = "application/x-www-form-urlencoded"
// callback_str,err:=json.Marshal(callbackParam)
// if err != nil {
// fmt.Println("callback json err:", err)
// }
// callbackBase64 := base64.StdEncoding.EncodeToString(callback_str)
// var policyToken PolicyToken
// policyToken.AccessKeyId = accessKeyId
// policyToken.Host = host
// policyToken.Expire = expire_end
// policyToken.Signature = string(signedStr)
// policyToken.Directory = upload_dir
// policyToken.Policy = string(debyte)
// policyToken.Callback = string(callbackBase64)
// response,err:=json.Marshal(policyToken)
// if err != nil {
// fmt.Println("json err:", err)
// }
// return string(response)
// }
//create post policy json
var policyConfig PolicyConfigStruct
policyConfig.Expiration = tokenExpire
var condition []string
condition = append(condition, "starts-with")
condition = append(condition, "$key")
condition = append(condition, upload_dir)
policyConfig.Conditions = append(policyConfig.Conditions, condition)
ossConfig := NewOssConfig()
//calucate signature
result, err := json.Marshal(policyConfig)
debyte := base64.StdEncoding.EncodeToString(result)
h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(ossConfig.accessKey))
io.WriteString(h, debyte)
signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
var callbackParam CallbackParam
callbackParam.CallbackUrl = ossConfig.callbackUrl
callbackParam.CallbackBody = "filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}"
callbackParam.CallbackBodyType = "application/x-www-form-urlencoded"
callback_str, err := json.Marshal(callbackParam)
if err != nil {
fmt.Println("callback json err:", err)
}
callbackBase64 := base64.StdEncoding.EncodeToString(callback_str)
var policyToken PolicyToken
policyToken.AccessKeyId = ossConfig.accessID
policyToken.Host = ossConfig.hostStr
policyToken.Expire = expire_end
policyToken.Signature = string(signedStr)
//TODO 文件夹另外处理
policyToken.Directory = upload_dir
policyToken.Policy = string(debyte)
policyToken.Callback = string(callbackBase64)
response, err := json.Marshal(policyToken)
if err != nil {
fmt.Println("json err:", err)
}
return string(response)
}
... ...