// 短视频 云点播 package tencent import ( "crypto/hmac" "crypto/sha1" "encoding/base64" "encoding/json" "github.com/dgrijalva/jwt-go" "live/app/lib/config" "strconv" "time" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" vod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod/v20180717" ) type short struct { secretId string secretKey string expire int64 oneTimeValid bool appId string key string } var Short *short func init() { Short = &short{ secretId: config.Config.Section("tencent.short").Key("secretId").String(), secretKey: config.Config.Section("tencent.short").Key("secretKey").String(), expire: config.Config.Section("tencent.short").Key("expire").MustInt64(0), oneTimeValid: config.Config.Section("tencent.short").Key("oneTimeValid").MustBool(), appId: config.Config.Section("tencent.short").Key("appId").String(), key: config.Config.Section("tencent.short").Key("key").String(), } } // @Title 获取上传签名 func (s *short) Sign() string { timestamp := time.Now().Unix() expireTime := timestamp + s.expire timestampStr := strconv.FormatInt(timestamp, 10) expireTimeStr := strconv.FormatInt(expireTime, 10) random := Worker.Rander() randomStr := strconv.FormatUint(uint64(random), 10) original := "secretId=" + s.secretId + "¤tTimeStamp=" + timestampStr + "&expireTime=" + expireTimeStr + "&random=" + randomStr + "&procedure=recook" // 单次有效 if s.oneTimeValid { original += "&oneTimeValid=1" } signature := generateHmacSHA1(s.secretKey, original) signature = append(signature, []byte(original)...) signatureB64 := base64.StdEncoding.EncodeToString(signature) return signatureB64 } func generateHmacSHA1(secretToken, payloadBody string) []byte { mac := hmac.New(sha1.New, []byte(secretToken)) sha1.New() mac.Write([]byte(payloadBody)) return mac.Sum(nil) } // @Title 播放签名 func (s *short) SignPlay(fileId string) (string, error) { currentTime := time.Now().Unix() psignExpire := currentTime + 3600 // 可任意设置过期时间,示例1h urlTimeExpire := strconv.FormatInt(psignExpire, 16) // 可任意设置过期时间,16进制字符串形式,示例1h // Create a new token object, specifying signing method and the claims // you would like it to contain. token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ "appId": s.appId, "fileId": fileId, "currentTimeStamp": currentTime, "expireTimeStamp": psignExpire, "urlAccessInfo": map[string]string{ "t": urlTimeExpire, }, }) // Sign and get the complete encoded token as a string using the secret return token.SignedString([]byte(s.key)) } type notify struct { EventType string `json:"EventType"` FileUploadEvent struct { FileID string `json:"FileId"` MediaBasicInfo struct { ClassID int64 `json:"ClassId"` ClassName string `json:"ClassName"` ClassPath string `json:"ClassPath"` CoverURL string `json:"CoverUrl"` CreateTime string `json:"CreateTime"` Description string `json:"Description"` ExpireTime string `json:"ExpireTime"` MediaURL string `json:"MediaUrl"` Name string `json:"Name"` SourceInfo struct { SourceContext string `json:"SourceContext"` SourceType string `json:"SourceType"` } `json:"SourceInfo"` StorageRegion string `json:"StorageRegion"` TagSet []interface{} `json:"TagSet"` Type string `json:"Type"` UpdateTime string `json:"UpdateTime"` Vid string `json:"Vid"` } `json:"MediaBasicInfo"` ProcedureTaskID string `json:"ProcedureTaskId"` } `json:"FileUploadEvent"` } func (s *short) Notify(data string) { dataStruct := notify{} err := json.Unmarshal([]byte(data), &dataStruct) if err != nil { return } // 文件上传通知 if dataStruct.EventType == "NewFileUpload" { } } type FileResult struct { Response struct { NotExistFileIdSet []string `json:"NotExistFileIdSet"` MediaInfoSet []struct { BasicInfo struct { CoverUrl string `json:"CoverUrl"` MediaUrl string `json:"MediaUrl"` SourceInfo struct { SourceType string `json:"SourceType"` } `json:"SourceInfo"` } `json:"BasicInfo"` FileId string `json:"FileId"` } `json:"MediaInfoSet"` } `json:"Response"` } // @Title 获取视频信息 func (s *short) GetInfo(fileId string) (result FileResult, err error) { credential := common.NewCredential( s.secretId, s.secretKey, ) cpf := profile.NewClientProfile() cpf.HttpProfile.Endpoint = "vod.tencentcloudapi.com" client, _ := vod.NewClient(credential, "", cpf) request := vod.NewDescribeMediaInfosRequest() jsonData := map[string][]string{ "FileIds": {fileId}, } params, _ := json.Marshal(jsonData) err = request.FromJsonString(string(params)) if err != nil { return } response, err := client.DescribeMediaInfos(request) if _, ok := err.(*errors.TencentCloudSDKError); ok { return } if err != nil { return } err = json.Unmarshal([]byte(response.ToJsonString()), &result) return }