You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

vod_api.go 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package video
  15. import (
  16. "bytes"
  17. "fmt"
  18. "time"
  19. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  20. "github.com/casibase/casibase/util"
  21. )
  22. func GetVideoPlayAuth(videoId string) string {
  23. r := vod.CreateGetVideoPlayAuthRequest()
  24. r.VideoId = videoId
  25. r.AcceptFormat = "JSON"
  26. resp, err := vodClient.GetVideoPlayAuth(r)
  27. if err != nil {
  28. fmt.Println(err)
  29. return err.Error()
  30. }
  31. playAuth := resp.PlayAuth
  32. return playAuth
  33. }
  34. type UploadAddress struct {
  35. Endpoint string `json:"Endpoint"`
  36. Bucket string `json:"Bucket"`
  37. FileName string `json:"FileName"`
  38. }
  39. type UploadAuth struct {
  40. SecurityToken string `json:"SecurityToken"`
  41. AccessKeyId string `json:"AccessKeyId"`
  42. ExpireUTCTime time.Time `json:"ExpireUTCTime"`
  43. AccessKeySecret string `json:"AccessKeySecret"`
  44. Expiration string `json:"Expiration"`
  45. Region string `json:"Region"`
  46. }
  47. func UploadVideo(fileId string, filename string, fileBuffer *bytes.Buffer) string {
  48. // https://help.aliyun.com/document_detail/476208.html
  49. r := vod.CreateCreateUploadVideoRequest()
  50. r.Scheme = "https"
  51. r.FileName = filename
  52. r.Title = fileId
  53. resp, err := vodClient.CreateUploadVideo(r)
  54. if err != nil {
  55. panic(err)
  56. }
  57. encodedUploadAddress := resp.UploadAddress
  58. videoId := resp.VideoId
  59. encodedUploadAuth := resp.UploadAuth
  60. uploadAddressStr := util.DecodeBase64(encodedUploadAddress)
  61. uploadAuthStr := util.DecodeBase64(encodedUploadAuth)
  62. uploadAddress := &UploadAddress{}
  63. err = util.JsonToStruct(uploadAddressStr, uploadAddress)
  64. if err != nil {
  65. panic(err)
  66. }
  67. uploadAuth := &UploadAuth{}
  68. err = util.JsonToStruct(uploadAuthStr, uploadAuth)
  69. if err != nil {
  70. panic(err)
  71. }
  72. ossClient := getOssClient(uploadAddress.Endpoint, uploadAuth.AccessKeyId, uploadAuth.AccessKeySecret, uploadAuth.SecurityToken)
  73. uploadLocalFile(ossClient, uploadAddress.Bucket, uploadAddress.FileName, fileBuffer)
  74. return videoId
  75. }
  76. func GetVideoCoverUrl(videoId string) string {
  77. r := vod.CreateGetVideoInfoRequest()
  78. r.VideoId = videoId
  79. r.AcceptFormat = "JSON"
  80. resp, err := vodClient.GetVideoInfo(r)
  81. if err != nil {
  82. fmt.Println(err)
  83. return err.Error()
  84. }
  85. return resp.Video.CoverURL
  86. }