作者 yangfu

增加 多视频凭证获取

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