作者 yangfu

增加 多视频凭证获取

@@ -30,13 +30,50 @@ func (this *VodController) CreateUploadVideo() { @@ -30,13 +30,50 @@ func (this *VodController) CreateUploadVideo() {
30 msg = m 30 msg = m
31 return 31 return
32 } 32 }
33 - if len(request.FileName) == 0 {  
34 - request.FileName = aliyun.DefaultVideoFileName  
35 - } 33 + //if len(request.FileName) == 0 {
  34 + // request.FileName = aliyun.DefaultVideoFileName
  35 + //}
36 header := controllers.GetRequestHeader(this.Ctx) 36 header := controllers.GetRequestHeader(this.Ctx)
37 msg = protocol.NewReturnResponse(vod.CreateUploadVideo(header, request)) 37 msg = protocol.NewReturnResponse(vod.CreateUploadVideo(header, request))
38 } 38 }
39 39
  40 +//创建视频上传凭证(多个) CreateUploadVideos
  41 +// @router /createUploadVideos [post]
  42 +func (this *VodController) CreateUploadVideos() {
  43 + var msg *protocol.ResponseMessage
  44 + var response *aliyun.CreateUploadVideosResponse = &aliyun.CreateUploadVideosResponse{
  45 + List: make([]*aliyun.CreateUploadVideoResponse, 0),
  46 + }
  47 + defer func() {
  48 + this.Resp(msg)
  49 + }()
  50 + var request *aliyun.CreateUploadVideosRequest
  51 + if err := json.Unmarshal(this.ByteBody, &request); err != nil {
  52 + log.Error(err)
  53 + msg = protocol.BadRequestParam(1)
  54 + return
  55 + }
  56 + if b, m := this.Valid(request); !b {
  57 + msg = m
  58 + return
  59 + }
  60 + header := controllers.GetRequestHeader(this.Ctx)
  61 + var err error
  62 + if len(request.Items) == 0 {
  63 + msg = protocol.BadRequestParam(2)
  64 + }
  65 + for i := range request.Items {
  66 + r := request.Items[i]
  67 + if rsp, e := vod.CreateUploadVideo(header, r); e != nil {
  68 + err = e
  69 + break
  70 + } else {
  71 + response.List = append(response.List, rsp)
  72 + }
  73 + }
  74 + msg = protocol.NewReturnResponse(response, err)
  75 +}
  76 +
40 //刷新视频上传凭证 RefreshUploadVideo 77 //刷新视频上传凭证 RefreshUploadVideo
41 // @router /refreshUploadVideo [post] 78 // @router /refreshUploadVideo [post]
42 func (this *VodController) RefreshUploadVideo() { 79 func (this *VodController) RefreshUploadVideo() {
@@ -112,7 +149,6 @@ func (this *VodController) CreateUploadImages() { @@ -112,7 +149,6 @@ func (this *VodController) CreateUploadImages() {
112 err = e 149 err = e
113 break 150 break
114 } else { 151 } else {
115 - rsp.FileName = r.FileName  
116 response.List = append(response.List, rsp) 152 response.List = append(response.List, rsp)
117 } 153 }
118 } 154 }
@@ -5,6 +5,7 @@ go 1.12 @@ -5,6 +5,7 @@ go 1.12
5 require ( 5 require (
6 github.com/aliyun/alibaba-cloud-sdk-go v1.60.348 6 github.com/aliyun/alibaba-cloud-sdk-go v1.60.348
7 github.com/astaxie/beego v1.10.0 7 github.com/astaxie/beego v1.10.0
  8 + github.com/klauspost/cpuid v1.2.3 // indirect
8 gitlab.fjmaimaimai.com/mmm-go/gocomm v0.0.1 9 gitlab.fjmaimaimai.com/mmm-go/gocomm v0.0.1
9 ) 10 )
10 11
1 package aliyun 1 package aliyun
2 2
  3 +import "fmt"
  4 +
  5 +var (
  6 + OssErrFileFormat = fmt.Errorf("文件名格式有错")
  7 +)
  8 +
3 //创建视频上传凭证 9 //创建视频上传凭证
4 /*CreateUploadVideo */ 10 /*CreateUploadVideo */
5 type CreateUploadVideoRequest struct { 11 type CreateUploadVideoRequest struct {
6 FileName string `json:"fileName"` 12 FileName string `json:"fileName"`
  13 + Title string `json:"title"`
7 } 14 }
8 15
9 type CreateUploadVideoResponse struct { 16 type CreateUploadVideoResponse struct {
  17 + FileName string `json:"fileName"`
10 RequestId string `json:"requestId"` 18 RequestId string `json:"requestId"`
11 VideoId string `json:"videoId"` 19 VideoId string `json:"videoId"`
12 UploadAddress string `json:"uploadAddress"` 20 UploadAddress string `json:"uploadAddress"`
@@ -14,6 +22,14 @@ type CreateUploadVideoResponse struct { @@ -14,6 +22,14 @@ type CreateUploadVideoResponse struct {
14 FileURL string `json:"fileURL"` 22 FileURL string `json:"fileURL"`
15 } 23 }
16 24
  25 +/*CreateUploadVideos */
  26 +type CreateUploadVideosRequest struct {
  27 + Items []*CreateUploadVideoRequest `json:"items"`
  28 +}
  29 +type CreateUploadVideosResponse struct {
  30 + List []*CreateUploadVideoResponse `json:"list"`
  31 +}
  32 +
17 //刷新视频上传凭证 RefreshUploadVideo 33 //刷新视频上传凭证 RefreshUploadVideo
18 type RefreshUploadVideoRequest struct { 34 type RefreshUploadVideoRequest struct {
19 VideoId string `json:"videoId" valid:"Required;"` 35 VideoId string `json:"videoId" valid:"Required;"`
@@ -2,6 +2,7 @@ package aliyun @@ -2,6 +2,7 @@ package aliyun
2 2
3 type CreateUploadImageRequest struct { 3 type CreateUploadImageRequest struct {
4 FileName string `json:"fileName"` 4 FileName string `json:"fileName"`
  5 + Title string `json:"title"`
5 } 6 }
6 7
7 type CreateUploadImageResponse struct { 8 type CreateUploadImageResponse struct {
@@ -4,9 +4,16 @@ import ( @@ -4,9 +4,16 @@ import (
4 "encoding/base64" 4 "encoding/base64"
5 "encoding/json" 5 "encoding/json"
6 "fmt" 6 "fmt"
  7 + "path/filepath"
7 "strings" 8 "strings"
8 ) 9 )
9 10
  11 +var (
  12 + VoiceFileExt = ".mp3.wma.wav.aac.ra.m4a.flac.ape.ac3.amr"
  13 + ImageFileExt = ".png.jpg.jpeg.gif"
  14 + VideoFileExt = ".mp4.ts.3gp.mpg.mpeg.mpe.dat.vob.asf"
  15 +)
  16 +
10 type UploadAddress struct { 17 type UploadAddress struct {
11 Endpoint string 18 Endpoint string
12 Bucket string 19 Bucket string
@@ -38,3 +45,19 @@ func ParseUploadAddress(uploadAddress string) (up UploadAddress, err error) { @@ -38,3 +45,19 @@ func ParseUploadAddress(uploadAddress string) (up UploadAddress, err error) {
38 } 45 }
39 return 46 return
40 } 47 }
  48 +
  49 +func validFileExt(rule string, ext string) bool {
  50 + if strings.ContainsAny(rule, strings.ToLower(ext)) {
  51 + return true
  52 + }
  53 + return false
  54 +}
  55 +
  56 +//@rule 规则
  57 +func ValidFileExt(rule, filename string) bool {
  58 + if len(filename) == 0 {
  59 + return false
  60 + }
  61 + ext := filepath.Ext(filename)
  62 + return validFileExt(rule, ext)
  63 +}
@@ -41,12 +41,8 @@ func InitVodClient(accessKeyId string, accessKeySecret string) (client *vod.Clie @@ -41,12 +41,8 @@ func InitVodClient(accessKeyId string, accessKeySecret string) (client *vod.Clie
41 //获取视频上传地址和凭证,并创建视频信息 41 //获取视频上传地址和凭证,并创建视频信息
42 func CreateUploadVideo(client *vod.Client, r *CreateUploadVideoRequest) (response *CreateUploadVideoResponse, err error) { 42 func CreateUploadVideo(client *vod.Client, r *CreateUploadVideoRequest) (response *CreateUploadVideoResponse, err error) {
43 request := vod.CreateCreateUploadVideoRequest() 43 request := vod.CreateCreateUploadVideoRequest()
44 - filePath := getFileName(FileVideo, r.FileName)  
45 - if filepath.Ext(r.FileName) == "mp3" {  
46 - filePath = getFileName(FileVoice, r.FileName)  
47 - }  
48 - request.Title = filePath  
49 - request.FileName = filePath 44 + request.Title = r.Title
  45 + request.FileName = r.FileName
50 request.AcceptFormat = "JSON" 46 request.AcceptFormat = "JSON"
51 rsp, err := client.CreateUploadVideo(request) 47 rsp, err := client.CreateUploadVideo(request)
52 if err != nil { 48 if err != nil {
@@ -58,11 +54,6 @@ func CreateUploadVideo(client *vod.Client, r *CreateUploadVideoRequest) (respons @@ -58,11 +54,6 @@ func CreateUploadVideo(client *vod.Client, r *CreateUploadVideoRequest) (respons
58 UploadAddress: rsp.UploadAddress, 54 UploadAddress: rsp.UploadAddress,
59 UploadAuth: rsp.UploadAuth, 55 UploadAuth: rsp.UploadAuth,
60 } 56 }
61 - if up, e := ParseUploadAddress(rsp.UploadAddress); e != nil {  
62 - log.Error(e)  
63 - } else {  
64 - response.FileURL = up.GetFileUrl(beego.AppConfig.String("cname"))  
65 - }  
66 return 57 return
67 } 58 }
68 59
@@ -90,9 +81,8 @@ func RefreshUploadVideo(client *vod.Client, r *RefreshUploadVideoRequest) (respo @@ -90,9 +81,8 @@ func RefreshUploadVideo(client *vod.Client, r *RefreshUploadVideoRequest) (respo
90 //获取图片上传地址和凭证,并创建视频信息 81 //获取图片上传地址和凭证,并创建视频信息
91 func CreateUploadImage(client *vod.Client, r *CreateUploadImageRequest) (response *CreateUploadImageResponse, err error) { 82 func CreateUploadImage(client *vod.Client, r *CreateUploadImageRequest) (response *CreateUploadImageResponse, err error) {
92 request := vod.CreateCreateUploadImageRequest() 83 request := vod.CreateCreateUploadImageRequest()
93 - filePath := getFileName(FileImage, r.FileName)  
94 request.ImageType = "default" 84 request.ImageType = "default"
95 - request.Title = filePath 85 + request.Title = r.Title
96 request.ImageExt = filepath.Ext(r.FileName)[1:] 86 request.ImageExt = filepath.Ext(r.FileName)[1:]
97 request.AcceptFormat = "JSON" 87 request.AcceptFormat = "JSON"
98 //request.StorageLocation = filepath.Base(request.Title) 88 //request.StorageLocation = filepath.Base(request.Title)
@@ -127,7 +117,7 @@ func GetGetVideoPlayAuth(client *vod.Client, r *GetVideoPlayAuthRequest) (respon @@ -127,7 +117,7 @@ func GetGetVideoPlayAuth(client *vod.Client, r *GetVideoPlayAuthRequest) (respon
127 } 117 }
128 118
129 //fileType: video voice image 119 //fileType: video voice image
130 -func getFileName(fileType string, filename string) string { 120 +func GetFileName(fileType string, filename string) string {
131 date := comm_time.GetTimeByYyyymmdd() 121 date := comm_time.GetTimeByYyyymmdd()
132 subfix := path.Ext(filename) 122 subfix := path.Ext(filename)
133 prefix := fmt.Sprintf("%v_%v", time.Now().Unix(), common.RandomString(32)) 123 prefix := fmt.Sprintf("%v_%v", time.Now().Unix(), common.RandomString(32))
1 package utils 1 package utils
2 2
3 -import "os" 3 +import (
  4 + "fmt"
  5 + "github.com/astaxie/beego"
  6 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
  7 + comm_time "gitlab.fjmaimaimai.com/mmm-go/gocomm/time"
  8 + "os"
  9 + "path"
  10 + "time"
  11 +)
4 12
5 // 判断所给路径文件/文件夹是否存在 13 // 判断所给路径文件/文件夹是否存在
6 func Exists(path string) bool { 14 func Exists(path string) bool {
@@ -27,3 +35,16 @@ func IsDir(path string) bool { @@ -27,3 +35,16 @@ func IsDir(path string) bool {
27 func IsFile(path string) bool { 35 func IsFile(path string) bool {
28 return !IsDir(path) 36 return !IsDir(path)
29 } 37 }
  38 +
  39 +//fileType: video voice image
  40 +func GetFileName(projectName, fileType string, filename string) string {
  41 + date := comm_time.GetTimeByYyyymmdd()
  42 + subfix := path.Ext(filename)
  43 + if len(projectName) == 0 {
  44 + projectName = beego.BConfig.AppName
  45 + }
  46 + prefix := fmt.Sprintf("%v_%v", time.Now().Unix(), common.RandomString(32))
  47 + filename = fmt.Sprintf("%v%v", prefix, subfix)
  48 + sourcePath := fmt.Sprintf("%v/%v/%v/%v/%v", projectName, beego.BConfig.RunMode, date, fileType, filename)
  49 + return sourcePath
  50 +}
@@ -33,6 +33,14 @@ func init() { @@ -33,6 +33,14 @@ func init() {
33 33
34 beego.GlobalControllerRouter["openapi/controllers/v1:VodController"] = append(beego.GlobalControllerRouter["openapi/controllers/v1:VodController"], 34 beego.GlobalControllerRouter["openapi/controllers/v1:VodController"] = append(beego.GlobalControllerRouter["openapi/controllers/v1:VodController"],
35 beego.ControllerComments{ 35 beego.ControllerComments{
  36 + Method: "CreateUploadVideos",
  37 + Router: `/createUploadVideos`,
  38 + AllowHTTPMethods: []string{"post"},
  39 + MethodParams: param.Make(),
  40 + Params: nil})
  41 +
  42 + beego.GlobalControllerRouter["openapi/controllers/v1:VodController"] = append(beego.GlobalControllerRouter["openapi/controllers/v1:VodController"],
  43 + beego.ControllerComments{
36 Method: "GetPlayInfo", 44 Method: "GetPlayInfo",
37 Router: `/getPlayInfo`, 45 Router: `/getPlayInfo`,
38 AllowHTTPMethods: []string{"post"}, 46 AllowHTTPMethods: []string{"post"},
@@ -9,8 +9,9 @@ import ( @@ -9,8 +9,9 @@ import (
9 var nsV1 *beego.Namespace 9 var nsV1 *beego.Namespace
10 10
11 func init() { 11 func init() {
12 - nsVod := beego.NewNamespace("vod", beego.NSBefore(controllers.LogRequestData), beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.VodController{}))  
13 - 12 + nsV1 := beego.NewNamespace("v1",
  13 + beego.NSNamespace("vod", beego.NSBefore(controllers.FilterComm), beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.VodController{})),
  14 + )
14 beego.SetStaticPath("/log", beego.AppConfig.String("aliyun_logs_access")) 15 beego.SetStaticPath("/log", beego.AppConfig.String("aliyun_logs_access"))
15 - beego.AddNamespace(nsVod) 16 + beego.AddNamespace(nsV1)
16 } 17 }
  1 +package vod
  2 +
  3 +import (
  4 + "openapi/internal/aliyun"
  5 + "openapi/protocol"
  6 +)
  7 +
  8 +//错误处理
  9 +func ErrorHandler(in error) (err error) {
  10 + switch in {
  11 + case aliyun.OssErrFileFormat:
  12 + err = protocol.NewCustomMessage(2, in.Error())
  13 + break
  14 + default:
  15 + err = in
  16 + return
  17 + }
  18 + return
  19 +}
1 package vod 1 package vod
2 2
3 import ( 3 import (
  4 + "github.com/astaxie/beego"
4 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 5 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
5 "openapi/internal/aliyun" 6 "openapi/internal/aliyun"
  7 + "openapi/internal/utils"
6 "openapi/protocol" 8 "openapi/protocol"
7 ) 9 )
8 10
9 //创建视频上传凭证 11 //创建视频上传凭证
10 func CreateUploadVideo(header *protocol.RequestHeader, request *aliyun.CreateUploadVideoRequest) (rsp *aliyun.CreateUploadVideoResponse, err error) { 12 func CreateUploadVideo(header *protocol.RequestHeader, request *aliyun.CreateUploadVideoRequest) (rsp *aliyun.CreateUploadVideoResponse, err error) {
11 - var () 13 + var (
  14 + up aliyun.UploadAddress
  15 + filePath string
  16 + fileName = request.FileName
  17 + )
  18 + defer func() {
  19 + err = ErrorHandler(err)
  20 + }()
12 client, e := aliyun.DefaultVodClient() 21 client, e := aliyun.DefaultVodClient()
13 if e != nil { 22 if e != nil {
14 log.Error(e) 23 log.Error(e)
15 err = e 24 err = e
16 return 25 return
17 } 26 }
  27 + if aliyun.ValidFileExt(aliyun.VideoFileExt, request.FileName) {
  28 + filePath = utils.GetFileName(header.AppProject, aliyun.FileVideo, request.FileName)
  29 + } else if aliyun.ValidFileExt(aliyun.VoiceFileExt, request.FileName) {
  30 + filePath = utils.GetFileName(header.AppProject, aliyun.FileVoice, request.FileName)
  31 + } else {
  32 + err = aliyun.OssErrFileFormat
  33 + return
  34 + }
  35 + request.FileName = filePath
  36 + request.Title = filePath
18 rsp, err = aliyun.CreateUploadVideo(client, request) 37 rsp, err = aliyun.CreateUploadVideo(client, request)
19 - if err != nil { 38 + if up, err = aliyun.ParseUploadAddress(rsp.UploadAddress); err != nil {
20 log.Error(err) 39 log.Error(err)
  40 + return
21 } 41 }
  42 + rsp.FileName = fileName
  43 + rsp.FileURL = up.GetFileUrl(beego.AppConfig.String("cname"))
22 return 44 return
23 } 45 }
24 46
@@ -50,8 +72,13 @@ func CreateUploadImage(header *protocol.RequestHeader, request *aliyun.CreateUpl @@ -50,8 +72,13 @@ func CreateUploadImage(header *protocol.RequestHeader, request *aliyun.CreateUpl
50 if len(request.FileName) == 0 { 72 if len(request.FileName) == 0 {
51 request.FileName = aliyun.DefaultImageFileName 73 request.FileName = aliyun.DefaultImageFileName
52 } 74 }
  75 + if !aliyun.ValidFileExt(aliyun.ImageFileExt, request.FileName) {
  76 + return nil, aliyun.OssErrFileFormat
  77 + }
  78 + request.Title = utils.GetFileName(header.AppProject, aliyun.FileImage, request.FileName)
53 rsp, err = aliyun.CreateUploadImage(client, request) 79 rsp, err = aliyun.CreateUploadImage(client, request)
54 rsp.FileURL = rsp.ImageURL 80 rsp.FileURL = rsp.ImageURL
  81 + rsp.FileName = request.FileName
55 if err != nil { 82 if err != nil {
56 log.Error(err) 83 log.Error(err)
57 } 84 }