作者 yangfu

Merge remote-tracking branch 'origin/dev' into test

# Conflicts:
#	conf/dev.conf
#	conf/prod.conf
#	conf/test.conf
#	deploy/k8s/dev/opp.yaml
#	deploy/k8s/prd/opp.yaml
@@ -40,4 +40,7 @@ user_center_app_secret ="cykbjnfqgctn" @@ -40,4 +40,7 @@ user_center_app_secret ="cykbjnfqgctn"
40 h5_host = "http://mmm-web-open-test.fjmaimaimai.com" 40 h5_host = "http://mmm-web-open-test.fjmaimaimai.com"
41 41
42 #审核中心 42 #审核中心
43 -suplus_approve_host ="http://suplus-approve-dev.fjmaimaimai.com"  
  43 +suplus_approve_host ="http://suplus-approve-dev.fjmaimaimai.com"
  44 +
  45 +#阿里云
  46 +cname ="https://media.fjmaimaimai.com/"
@@ -43,5 +43,8 @@ h5_host = "http://mmm-web-open-test.fjmaimaimai.com" @@ -43,5 +43,8 @@ h5_host = "http://mmm-web-open-test.fjmaimaimai.com"
43 #审核中心 43 #审核中心
44 suplus_approve_host ="http://suplus-approve-dev.fjmaimaimai.com" 44 suplus_approve_host ="http://suplus-approve-dev.fjmaimaimai.com"
45 45
  46 +#阿里云 https://media.goexample.live/
  47 +cname ="https://media.fjmaimaimai.com/"
  48 +
46 #企业平台 49 #企业平台
47 BUSINESS_ADMIN_SERVICE_HOST ="${BUSINESS_ADMIN_SERVICE_HOST||http://suplus-business-admin-test.fjmaimaimai.com}" 50 BUSINESS_ADMIN_SERVICE_HOST ="${BUSINESS_ADMIN_SERVICE_HOST||http://suplus-business-admin-test.fjmaimaimai.com}"
@@ -43,4 +43,7 @@ h5_host = "https://web-open.fjmaimaimai.com" @@ -43,4 +43,7 @@ h5_host = "https://web-open.fjmaimaimai.com"
43 suplus_approve_host ="https://public-interface.fjmaimaimai.com/approve" 43 suplus_approve_host ="https://public-interface.fjmaimaimai.com/approve"
44 44
45 #企业平台 45 #企业平台
46 -BUSINESS_ADMIN_SERVICE_HOST ="${BUSINESS_ADMIN_SERVICE_HOST||http://suplus-business-admin-prd.fjmaimaimai.com}"  
  46 +BUSINESS_ADMIN_SERVICE_HOST ="${BUSINESS_ADMIN_SERVICE_HOST||http://suplus-business-admin-prd.fjmaimaimai.com}"
  47 +
  48 +#阿里云
  49 +cname ="https://media.fjmaimaimai.com/"
@@ -42,5 +42,8 @@ h5_host = "http://mmm-web-open-test.fjmaimaimai.com" @@ -42,5 +42,8 @@ h5_host = "http://mmm-web-open-test.fjmaimaimai.com"
42 #审核中心 42 #审核中心
43 suplus_approve_host ="http://suplus-approve-test.fjmaimaimai.com" 43 suplus_approve_host ="http://suplus-approve-test.fjmaimaimai.com"
44 44
  45 +#阿里云
  46 +cname ="https://media.fjmaimaimai.com/"
  47 +
45 #企业平台 48 #企业平台
46 BUSINESS_ADMIN_SERVICE_HOST ="${BUSINESS_ADMIN_SERVICE_HOST||http://suplus-business-admin-test.fjmaimaimai.com}" 49 BUSINESS_ADMIN_SERVICE_HOST ="${BUSINESS_ADMIN_SERVICE_HOST||http://suplus-business-admin-test.fjmaimaimai.com}"
@@ -159,7 +159,17 @@ func MetricCounter(ctx *context.Context) { @@ -159,7 +159,17 @@ func MetricCounter(ctx *context.Context) {
159 }).Inc() 159 }).Inc()
160 } 160 }
161 161
162 -//检查签名 162 +/*检查签名
  163 +app 传入 Sign TimeStamp Uuid AccessToken
  164 +
  165 +Sign=》客户端签名串
  166 +TimeStamp=》时间戳
  167 +UUID字符串=》Uuid(36位)/AccessToken(32位)
  168 +
  169 +//生成签名串与客户端生成的签名串比较
  170 +sign="v!(MmM"+TimeStamp + Uuid + AccessToken+"MmM)i^"
  171 +signHex = hex.EncodeToString(sha256(sign))
  172 +*/
163 func CheckSign(ctx *context.Context) (result bool) { 173 func CheckSign(ctx *context.Context) (result bool) {
164 var ( 174 var (
165 h *protocol.RequestHeader 175 h *protocol.RequestHeader
@@ -269,6 +279,10 @@ var LogRequestData = func(ctx *context.Context) { @@ -269,6 +279,10 @@ var LogRequestData = func(ctx *context.Context) {
269 } 279 }
270 } 280 }
271 281
  282 +func CheckSignMiddleware(ctx *context.Context) {
  283 + CheckSign(ctx)
  284 +}
  285 +
272 func (this *BaseController) Prepare() { 286 func (this *BaseController) Prepare() {
273 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") 287 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
274 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*") 288 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
@@ -2,10 +2,14 @@ package aliyun @@ -2,10 +2,14 @@ package aliyun
2 2
3 const ( 3 const (
4 RegionID = "cn-shanghai" 4 RegionID = "cn-shanghai"
5 - //AccessKeyID = "LTAI4Fv7TX3UP9nYuofpLHRy"  
6 - //AccessKeySecret = "Yx6lSe7JE9KjtWyOV2LSBnjalrGUk6"  
7 - AccessKeyID = "LTAI4FhiZ3UktC6N1u3H5GFC"  
8 - AccessKeySecret = "UyspWwdni55CYQ02hUCint4qY2jNYO" 5 +
  6 + //公司
  7 + AccessKeyID = "LTAI4Fz1LUBW2fXp6QWaJHRS"
  8 + AccessKeySecret = "aLZXwK8pgrs10Ws03qcN7NsrSXFVsg"
  9 +
  10 + //个人
  11 + //AccessKeyID = "LTAI4FhiZ3UktC6N1u3H5GFC"
  12 + //AccessKeySecret = "UyspWwdni55CYQ02hUCint4qY2jNYO"
9 ) 13 )
10 14
11 const ( 15 const (
@@ -20,6 +20,7 @@ var ( @@ -20,6 +20,7 @@ var (
20 20
21 type OSSClient struct { 21 type OSSClient struct {
22 client *oss.Client 22 client *oss.Client
  23 + Bucket *oss.Bucket
23 UpAddr UploadAddress 24 UpAddr UploadAddress
24 Auth UploadAuth 25 Auth UploadAuth
25 } 26 }
@@ -54,22 +55,35 @@ func NewOSSClient(ep, key, secret, token string) (o *OSSClient, err error) { @@ -54,22 +55,35 @@ func NewOSSClient(ep, key, secret, token string) (o *OSSClient, err error) {
54 } 55 }
55 56
56 //简单上传 (通过文件地址) 57 //简单上传 (通过文件地址)
57 -func (o *OSSClient) PutObjectByUrl(bucketName string, fileName string, url string) error { 58 +func (o *OSSClient) PutObjectByUrl(bucketName string, fileName string, url string, options ...oss.Option) error {
58 r, err := GetFileFromUrl(url) 59 r, err := GetFileFromUrl(url)
59 if err != nil { 60 if err != nil {
60 return err 61 return err
61 } 62 }
62 - return o.PutObject(bucketName, fileName, r) 63 + return o.PutObjectByBucket(bucketName, fileName, r, options...)
63 } 64 }
64 -func (o *OSSClient) PutObjectByUrlDefault(url string) error {  
65 - return o.PutObjectByUrl(o.UpAddr.Bucket, o.UpAddr.FileName, url) 65 +func (o *OSSClient) PutObjectByUrlDefault(url string, options ...oss.Option) error {
  66 + return o.PutObjectByUrl(o.UpAddr.Bucket, o.UpAddr.FileName, url, options...)
66 } 67 }
67 68
68 //简单上传 69 //简单上传
69 -func (o *OSSClient) PutObject(bucketName, objKey string, r io.Reader, options ...oss.Option) error { 70 +func (o *OSSClient) PutObjectByBucket(bucketName, objKey string, r io.Reader, options ...oss.Option) error {
70 bucket, err := o.client.Bucket(bucketName) 71 bucket, err := o.client.Bucket(bucketName)
71 if err != nil { 72 if err != nil {
72 return err 73 return err
73 } 74 }
74 return bucket.PutObject(objKey, r, options...) 75 return bucket.PutObject(objKey, r, options...)
75 } 76 }
  77 +
  78 +func (o *OSSClient) SetBucket(bucketName string) error {
  79 + bucket, err := o.client.Bucket(bucketName)
  80 + if err != nil {
  81 + return err
  82 + }
  83 + o.Bucket = bucket
  84 + return nil
  85 +}
  86 +
  87 +func (o *OSSClient) PutObject(objKey string, r io.Reader, options ...oss.Option) error {
  88 + return o.Bucket.PutObject(objKey, r, options...)
  89 +}
@@ -10,7 +10,7 @@ type CreateUploadImageResponse struct { @@ -10,7 +10,7 @@ type CreateUploadImageResponse struct {
10 ImageURL string `json:"imageURL"` 10 ImageURL string `json:"imageURL"`
11 UploadAddress string `json:"uploadAddress"` 11 UploadAddress string `json:"uploadAddress"`
12 UploadAuth string `json:"uploadAuth"` 12 UploadAuth string `json:"uploadAuth"`
13 - FileURL string `json:"fileURL"` 13 + FileURL string `json:"fileURL"` //fileURL
14 } 14 }
15 15
16 type CreateUploadImagesRequest struct { 16 type CreateUploadImagesRequest struct {
@@ -45,7 +45,7 @@ func InitVodClient(accessKeyId string, accessKeySecret string) (client *vod.Clie @@ -45,7 +45,7 @@ func InitVodClient(accessKeyId string, accessKeySecret string) (client *vod.Clie
45 func CreateUploadVideo(client *vod.Client, r *CreateUploadVideoRequest) (response *CreateUploadVideoResponse, err error) { 45 func CreateUploadVideo(client *vod.Client, r *CreateUploadVideoRequest) (response *CreateUploadVideoResponse, err error) {
46 request := vod.CreateCreateUploadVideoRequest() 46 request := vod.CreateCreateUploadVideoRequest()
47 filePath := getFileName(FileVideo, r.FileName) 47 filePath := getFileName(FileVideo, r.FileName)
48 - if filepath.Ext(r.FileName) == "mp3" { 48 + if filepath.Ext(r.FileName) == ".mp3" {
49 filePath = getFileName(FileVoice, r.FileName) 49 filePath = getFileName(FileVoice, r.FileName)
50 } 50 }
51 request.Title = filePath 51 request.Title = filePath
@@ -64,7 +64,7 @@ func CreateUploadVideo(client *vod.Client, r *CreateUploadVideoRequest) (respons @@ -64,7 +64,7 @@ func CreateUploadVideo(client *vod.Client, r *CreateUploadVideoRequest) (respons
64 if up, e := ParseUploadAddress(rsp.UploadAddress); e != nil { 64 if up, e := ParseUploadAddress(rsp.UploadAddress); e != nil {
65 log.Error(e) 65 log.Error(e)
66 } else { 66 } else {
67 - response.FileURL = up.GetFileUrl("") 67 + response.FileURL = up.GetFileUrl(beego.AppConfig.String("cname"))
68 } 68 }
69 return 69 return
70 } 70 }
@@ -85,7 +85,7 @@ func RefreshUploadVideo(client *vod.Client, r *RefreshUploadVideoRequest) (respo @@ -85,7 +85,7 @@ func RefreshUploadVideo(client *vod.Client, r *RefreshUploadVideoRequest) (respo
85 if up, e := ParseUploadAddress(rsp.UploadAddress); e != nil { 85 if up, e := ParseUploadAddress(rsp.UploadAddress); e != nil {
86 log.Error(e) 86 log.Error(e)
87 } else { 87 } else {
88 - response.FileURL = up.GetFileUrl("") 88 + response.FileURL = up.GetFileUrl(beego.AppConfig.String("cname"))
89 } 89 }
90 return 90 return
91 } 91 }
@@ -96,7 +96,15 @@ func CreateUploadImage(client *vod.Client, r *CreateUploadImageRequest) (respons @@ -96,7 +96,15 @@ func CreateUploadImage(client *vod.Client, r *CreateUploadImageRequest) (respons
96 filePath := getFileName(FileImage, r.FileName) 96 filePath := getFileName(FileImage, r.FileName)
97 request.ImageType = "default" 97 request.ImageType = "default"
98 request.Title = filePath 98 request.Title = filePath
  99 + if filepath.Ext(r.FileName) == "" {
  100 + err = fmt.Errorf("filename:%v invalid", r.FileName)
  101 + return
  102 + }
99 request.ImageExt = filepath.Ext(r.FileName)[1:] 103 request.ImageExt = filepath.Ext(r.FileName)[1:]
  104 + if request.ImageExt == "" {
  105 + err = fmt.Errorf("filename:%v invalid", r.FileName)
  106 + return
  107 + }
100 request.AcceptFormat = "JSON" 108 request.AcceptFormat = "JSON"
101 //request.StorageLocation = filepath.Base(request.Title) 109 //request.StorageLocation = filepath.Base(request.Title)
102 rsp, err := client.CreateUploadImage(request) 110 rsp, err := client.CreateUploadImage(request)
@@ -135,7 +143,7 @@ func getFileName(fileType string, filename string) string { @@ -135,7 +143,7 @@ func getFileName(fileType string, filename string) string {
135 subfix := path.Ext(filename) 143 subfix := path.Ext(filename)
136 prefix := fmt.Sprintf("%v_%v", time.Now().Unix(), common.RandomString(32)) 144 prefix := fmt.Sprintf("%v_%v", time.Now().Unix(), common.RandomString(32))
137 filename = fmt.Sprintf("%v%v", prefix, subfix) 145 filename = fmt.Sprintf("%v%v", prefix, subfix)
138 - sourcePath := fmt.Sprintf("%v/%v/%v/%v/%v", beego.BConfig.AppName, beego.BConfig.RunMode, fileType, date, filename) 146 + sourcePath := fmt.Sprintf("%v/%v/%v/%v/%v", beego.BConfig.AppName, beego.BConfig.RunMode, date, fileType, filename)
139 return sourcePath 147 return sourcePath
140 } 148 }
141 149
@@ -534,17 +534,17 @@ func ClearEmptyForm(inputFormList []*Form) (FormList []*Form) { @@ -534,17 +534,17 @@ func ClearEmptyForm(inputFormList []*Form) (FormList []*Form) {
534 type Speech struct { 534 type Speech struct {
535 Path string `json:"path"` 535 Path string `json:"path"`
536 Duration int `json:"duration"` 536 Duration int `json:"duration"`
537 - VideoId string `json:"videoId"` //videoId  
538 - PathBak string `json:"path_bak"` //备份路径 537 + VideoId string `json:"videoId"` //videoId
  538 + PathBak string `json:"-"` //备份路径
539 } 539 }
540 540
541 //图片 541 //图片
542 type Picture struct { 542 type Picture struct {
543 Path string `json:"path"` 543 Path string `json:"path"`
544 - W int `json:"w"`  
545 - H int `json:"h"` 544 + W int `json:"-"` //w
  545 + H int `json:"-"` //h
546 ImageId string `json:"imageId"` //imageId 546 ImageId string `json:"imageId"` //imageId
547 - PathBak string `json:"path_bak"` 547 + PathBak string `json:"-"`
548 //JobId string `json:"job_id"` 548 //JobId string `json:"job_id"`
549 } 549 }
550 550
@@ -554,7 +554,7 @@ type Video struct { @@ -554,7 +554,7 @@ type Video struct {
554 Cover Cover `json:"cover"` //封面 554 Cover Cover `json:"cover"` //封面
555 Duration int `json:"duration"` 555 Duration int `json:"duration"`
556 VideoId string `json:"videoId"` //videoId 556 VideoId string `json:"videoId"` //videoId
557 - PathBak string `json:"path_bak"` 557 + PathBak string `json:"-"` //
558 } 558 }
559 559
560 //审批配置 560 //审批配置
@@ -161,9 +161,10 @@ type QuestionContent struct { @@ -161,9 +161,10 @@ type QuestionContent struct {
161 Content string `json:"content" valid:"Required"` 161 Content string `json:"content" valid:"Required"`
162 } 162 }
163 type Cover struct { 163 type Cover struct {
164 - Path string `json:"path" valid:"Required"`  
165 - H int `json:"h"`  
166 - W int `json:"w"` 164 + Path string `json:"path" valid:"Required"`
  165 + H int `json:"-"`
  166 + W int `json:"-"`
  167 + ImageId string `json:"imageId"`
167 } 168 }
168 169
169 /*公告列表 BulletinList */ 170 /*公告列表 BulletinList */
@@ -23,4 +23,20 @@ func init() { @@ -23,4 +23,20 @@ func init() {
23 MethodParams: param.Make(), 23 MethodParams: param.Make(),
24 Params: nil}) 24 Params: nil})
25 25
  26 + beego.GlobalControllerRouter["opp/controllers:H5Controller"] = append(beego.GlobalControllerRouter["opp/controllers:H5Controller"],
  27 + beego.ControllerComments{
  28 + Method: "ChanceExample",
  29 + Router: `/chanceExample`,
  30 + AllowHTTPMethods: []string{"post"},
  31 + MethodParams: param.Make(),
  32 + Params: nil})
  33 +
  34 + beego.GlobalControllerRouter["opp/controllers:H5Controller"] = append(beego.GlobalControllerRouter["opp/controllers:H5Controller"],
  35 + beego.ControllerComments{
  36 + Method: "SysCommand",
  37 + Router: `/sysCommand`,
  38 + AllowHTTPMethods: []string{"post"},
  39 + MethodParams: param.Make(),
  40 + Params: nil})
  41 +
26 } 42 }
@@ -35,7 +35,7 @@ func init() { @@ -35,7 +35,7 @@ func init() {
35 beego.AddNamespace(nsV2) 35 beego.AddNamespace(nsV2)
36 36
37 nsH5 := beego.NewNamespace("h5", beego.NSBefore(controllers.LogRequestData), beego.NSBefore(controllers.AllowOption), beego.NSInclude(&controllers.H5Controller{})) 37 nsH5 := beego.NewNamespace("h5", beego.NSBefore(controllers.LogRequestData), beego.NSBefore(controllers.AllowOption), beego.NSInclude(&controllers.H5Controller{}))
38 - nsVod := beego.NewNamespace("vod", beego.NSBefore(controllers.LogRequestData), beego.NSBefore(controllers.AllowOption), beego.NSInclude(&v1.VodController{})) 38 + nsVod := beego.NewNamespace("vod", beego.NSBefore(controllers.LogRequestData), beego.NSBefore(controllers.AllowOption), beego.NSBefore(controllers.CheckSignMiddleware), beego.NSInclude(&v1.VodController{}))
39 beego.AddNamespace(nsH5) 39 beego.AddNamespace(nsH5)
40 beego.AddNamespace(nsVod) 40 beego.AddNamespace(nsVod)
41 //post 下载文件 41 //post 下载文件
@@ -3,16 +3,17 @@ package contrab @@ -3,16 +3,17 @@ package contrab
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "github.com/aliyun/alibaba-cloud-sdk-go/services/vod" 5 "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  6 + "github.com/aliyun/aliyun-oss-go-sdk/oss"
6 "github.com/astaxie/beego/orm" 7 "github.com/astaxie/beego/orm"
7 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 8 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
8 "opp/internal/aliyun" 9 "opp/internal/aliyun"
9 "opp/internal/utils" 10 "opp/internal/utils"
10 "opp/models" 11 "opp/models"
11 "opp/protocol" 12 "opp/protocol"
12 - "path/filepath" 13 + "strings"
13 ) 14 )
14 15
15 -const LoopSize = 1 16 +const LoopSize = 20
16 17
17 const ( 18 const (
18 Uploaded = "已上传,跳过" 19 Uploaded = "已上传,跳过"
@@ -31,12 +32,12 @@ func MigrateChanceDataToAliYun() { @@ -31,12 +32,12 @@ func MigrateChanceDataToAliYun() {
31 log.Info("【迁移机会媒体数据】开始迁移") 32 log.Info("【迁移机会媒体数据】开始迁移")
32 defer log.Info("【迁移机会媒体数据】结束迁移") 33 defer log.Info("【迁移机会媒体数据】结束迁移")
33 var ( 34 var (
34 - datas []*models.ChanceData  
35 query = `select * from chance_data order by id limit ?,?` 35 query = `select * from chance_data order by id limit ?,?`
36 pageInfo = protocol.PageInfo{PageSize: LoopSize} 36 pageInfo = protocol.PageInfo{PageSize: LoopSize}
37 ) 37 )
38 o := orm.NewOrm() 38 o := orm.NewOrm()
39 for { 39 for {
  40 + var datas []*models.ChanceData
40 if _, e := o.Raw(query, pageInfo.Offset(), pageInfo.PageSize).QueryRows(&datas); e != nil { 41 if _, e := o.Raw(query, pageInfo.Offset(), pageInfo.PageSize).QueryRows(&datas); e != nil {
41 if e == orm.ErrNoRows { 42 if e == orm.ErrNoRows {
42 break 43 break
@@ -44,21 +45,88 @@ func MigrateChanceDataToAliYun() { @@ -44,21 +45,88 @@ func MigrateChanceDataToAliYun() {
44 log.Error(e) 45 log.Error(e)
45 break 46 break
46 } 47 }
  48 + log.Info("当前页:", pageInfo.PageIndex, "起始:", pageInfo.Offset(), pageInfo.PageSize, " 行数:", len(datas))
  49 + if len(datas) == 0 {
  50 + break
  51 + }
47 52
48 for i := range datas { 53 for i := range datas {
49 data := datas[i] 54 data := datas[i]
50 -  
51 //上传图片 55 //上传图片
52 data.Images = utils.JsonMarsh(uploadImages(data, data.Images)) 56 data.Images = utils.JsonMarsh(uploadImages(data, data.Images))
53 //上传视频 57 //上传视频
  58 + data.Videos = utils.JsonMarsh(uploadVideos(data, data.Videos))
54 //上传音频 59 //上传音频
55 - 60 + //data.Speechs = utils.JsonMarsh(uploadVoice(data, data.Speechs))
56 //更新数据 61 //更新数据
57 o.Update(data) 62 o.Update(data)
58 } 63 }
59 pageInfo.PageIndex += 1 64 pageInfo.PageIndex += 1
60 - break 65 + //break
  66 + }
  67 +}
  68 +
  69 +func uploadVideos(d *models.ChanceData, video string) (rsp []protocol.Video) {
  70 + utils.JsonUnmarshal(video, &rsp)
  71 + if len(rsp) == 0 {
  72 + rsp = make([]protocol.Video, 0)
  73 + return
  74 + }
  75 + for i := range rsp {
  76 + p := rsp[i]
  77 + if len(p.VideoId) > 0 {
  78 + printInfo(d, p.Path, Uploaded)
  79 + continue
  80 + }
  81 + var (
  82 + err error
  83 + auth *aliyun.CreateUploadVideoResponse
  84 + authImage *aliyun.CreateUploadImageResponse
  85 + vodc *vod.Client
  86 + )
  87 + vodc, err = aliyun.DefaultVodClient()
  88 + if err != nil {
  89 + goto ERR
  90 + }
  91 + //上传视频
  92 + auth, err = aliyun.CreateUploadVideo(vodc, &aliyun.CreateUploadVideoRequest{FileName: fixPath(p.Path)})
  93 + if err != nil {
  94 + goto ERR
  95 + }
  96 + err = upload(fixPath(p.Path), auth.UploadAddress, auth.UploadAuth)
  97 + if err != nil {
  98 + goto ERR
  99 + }
  100 +
  101 + //备份路径
  102 + rsp[i].PathBak = p.Path
  103 + rsp[i].VideoId = auth.VideoId
  104 + rsp[i].Path = auth.FileURL
  105 + printInfo(d, fmt.Sprintf("%v -> %v", p.Path, auth.FileURL), UploadSuccess)
  106 +
  107 + //上传封面
  108 + if len(p.Cover.Path) > 0 {
  109 + authImage, err = aliyun.CreateUploadImage(vodc, &aliyun.CreateUploadImageRequest{FileName: fixPath(p.Cover.Path)})
  110 + if err != nil {
  111 + goto ERR
  112 + }
  113 + err = upload(fixPath(p.Cover.Path), authImage.UploadAddress, authImage.UploadAuth)
  114 + if err != nil {
  115 + goto ERR
  116 + }
  117 + printInfo(d, fmt.Sprintf("cover %v -> %v", p.Cover.Path, authImage.ImageURL), UploadSuccess)
  118 + rsp[i].Cover.Path = authImage.ImageURL
  119 + rsp[i].Cover.ImageId = authImage.ImageId
  120 + }
  121 + continue
  122 + ERR:
  123 + {
  124 + log.Error("ali vod error:", err)
  125 + printInfo(d, p.Path, UploadFail)
  126 + continue
  127 + }
61 } 128 }
  129 + return
62 } 130 }
63 131
64 //上传图片 132 //上传图片
@@ -74,40 +142,115 @@ func uploadImages(d *models.ChanceData, img string) (rsp []protocol.Picture) { @@ -74,40 +142,115 @@ func uploadImages(d *models.ChanceData, img string) (rsp []protocol.Picture) {
74 printInfo(d, p.Path, Uploaded) 142 printInfo(d, p.Path, Uploaded)
75 continue 143 continue
76 } 144 }
77 -  
78 var ( 145 var (
79 - err error  
80 - vodc *vod.Client  
81 - imageRsp *aliyun.CreateUploadImageResponse  
82 - client *aliyun.OSSClient 146 + err error
  147 + auth *aliyun.CreateUploadImageResponse
  148 + vodc *vod.Client
83 ) 149 )
84 - vodc, _ = aliyun.DefaultVodClient()  
85 - list := filepath.SplitList(p.Path)  
86 - if len(list) == 0 {  
87 - err = fmt.Errorf("路径无效:%v", p.Path) 150 + vodc, err = aliyun.DefaultVodClient()
  151 + if err != nil {
  152 + goto ERR
  153 + }
  154 + auth, err = aliyun.CreateUploadImage(vodc, &aliyun.CreateUploadImageRequest{FileName: fixPath(p.Path)})
  155 + if err != nil {
88 goto ERR 156 goto ERR
89 } 157 }
  158 + err = upload(fixPath(p.Path), auth.UploadAddress, auth.UploadAuth)
90 if err != nil { 159 if err != nil {
91 goto ERR 160 goto ERR
92 } 161 }
93 - imageRsp, err = aliyun.CreateUploadImage(vodc, &aliyun.CreateUploadImageRequest{FileName: list[len(list)-1]}) 162 +
  163 + //备份路径
  164 + rsp[i].PathBak = p.Path
  165 + rsp[i].ImageId = auth.ImageId
  166 + rsp[i].Path = auth.ImageURL
  167 + printInfo(d, fmt.Sprintf("%v -> %v", p.Path, auth.ImageURL), UploadSuccess)
  168 + continue
  169 + ERR:
  170 + {
  171 + log.Error("ali vod error:", err)
  172 + printInfo(d, p.Path, UploadFail)
  173 + continue
  174 + }
  175 + }
  176 + return
  177 +}
  178 +
  179 +func fixPath(path string) string {
  180 + if strings.Index(path, "https:///") >= 0 {
  181 + return strings.Replace(path, "https:///", "https://", 1)
  182 + }
  183 + if strings.Index(path, "https://") >= 0 {
  184 + return strings.Replace(path, "https:///", "https://", 1)
  185 + }
  186 + if strings.Index(path, "https:/") >= 0 {
  187 + return strings.Replace(path, "https:/", "https://", 1)
  188 + }
  189 +
  190 + if strings.Index(path, "http:///") >= 0 {
  191 + return strings.Replace(path, "http:///", "http://", 1)
  192 + }
  193 + if strings.Index(path, "http://") >= 0 {
  194 + return strings.Replace(path, "http:///", "http://", 1)
  195 + }
  196 + if strings.Index(path, "http:/") >= 0 {
  197 + return strings.Replace(path, "http:/", "http://", 1)
  198 + }
  199 + return path
  200 +}
  201 +
  202 +//上传文件
  203 +func upload(path string, upAddress, upAuth string, options ...oss.Option) (err error) {
  204 + var (
  205 + client *aliyun.OSSClient
  206 + )
  207 + client, err = aliyun.NewStsOSSClient(upAddress, upAuth)
  208 + if err != nil {
  209 + return
  210 + }
  211 + err = client.PutObjectByUrlDefault(path, options...)
  212 + if err != nil {
  213 + return
  214 + }
  215 + return
  216 +}
  217 +
  218 +//上班媒体数据
  219 +func uploadVoice(d *models.ChanceData, data string) (rsp []protocol.Speech) {
  220 + utils.JsonUnmarshal(data, &rsp)
  221 + if len(rsp) == 0 {
  222 + rsp = make([]protocol.Speech, 0)
  223 + return
  224 + }
  225 + for i := range rsp {
  226 + p := rsp[i]
  227 + if len(p.VideoId) > 0 {
  228 + printInfo(d, p.Path, Uploaded)
  229 + continue
  230 + }
  231 + var (
  232 + err error
  233 + auth *aliyun.CreateUploadVideoResponse
  234 + vodc *vod.Client
  235 + )
  236 + vodc, err = aliyun.DefaultVodClient()
94 if err != nil { 237 if err != nil {
95 goto ERR 238 goto ERR
96 } 239 }
97 - client, err = aliyun.NewStsOSSClient(imageRsp.UploadAddress, imageRsp.UploadAuth) 240 + auth, err = aliyun.CreateUploadVideo(vodc, &aliyun.CreateUploadVideoRequest{FileName: p.Path})
98 if err != nil { 241 if err != nil {
99 goto ERR 242 goto ERR
100 } 243 }
101 - err = client.PutObjectByUrlDefault(p.Path) 244 + err = upload(p.Path, auth.UploadAddress, auth.UploadAuth, oss.ContentType("audio/mpeg"))
102 if err != nil { 245 if err != nil {
103 goto ERR 246 goto ERR
104 } 247 }
105 248
106 //备份路径 249 //备份路径
107 rsp[i].PathBak = p.Path 250 rsp[i].PathBak = p.Path
108 - rsp[i].ImageId = imageRsp.ImageId  
109 - rsp[i].Path = imageRsp.ImageURL  
110 - printInfo(d, p.Path, UploadSuccess) 251 + rsp[i].VideoId = auth.VideoId
  252 + rsp[i].Path = auth.FileURL
  253 + printInfo(d, fmt.Sprintf("%v -> %v", p.Path, auth.FileURL), UploadSuccess)
111 continue 254 continue
112 ERR: 255 ERR:
113 { 256 {
@@ -119,12 +262,6 @@ func uploadImages(d *models.ChanceData, img string) (rsp []protocol.Picture) { @@ -119,12 +262,6 @@ func uploadImages(d *models.ChanceData, img string) (rsp []protocol.Picture) {
119 return 262 return
120 } 263 }
121 264
122 -//上班媒体数据  
123 -func uploadVoice(d *models.ChanceData, url string) (rp []protocol.Speech) {  
124 - //  
125 - return nil  
126 -}  
127 -  
128 type ChanceDataExtend struct { 265 type ChanceDataExtend struct {
129 Id int64 `json:"id"` 266 Id int64 `json:"id"`
130 ChanceId int64 `json:"chance_id"` 267 ChanceId int64 `json:"chance_id"`
@@ -50,3 +50,22 @@ func Test_MyGetURLUploadInfos(t *testing.T) { @@ -50,3 +50,22 @@ func Test_MyGetURLUploadInfos(t *testing.T) {
50 fmt.Printf("%s: %s %s\n", uploadInfo.UploadURL, uploadInfo.Status, uploadInfo.MediaId) 50 fmt.Printf("%s: %s %s\n", uploadInfo.UploadURL, uploadInfo.Status, uploadInfo.MediaId)
51 } 51 }
52 } 52 }
  53 +
  54 +func Test_FixPath(t *testing.T) {
  55 + input := []struct {
  56 + Input string
  57 + Except string
  58 + }{
  59 + {Input: "https://123.jpg", Except: "https://123.jpg"},
  60 + {Input: "https:///123.jpg", Except: "https://123.jpg"},
  61 + {Input: "https:/123.jpg", Except: "https://123.jpg"},
  62 + {Input: "http://123.jpg", Except: "http://123.jpg"},
  63 + {Input: "http:///123.jpg", Except: "http://123.jpg"},
  64 + {Input: "http:/123.jpg", Except: "http://123.jpg"},
  65 + }
  66 + for i := 0; i < len(input); i++ {
  67 + if !strings.EqualFold(fixPath(input[i].Input), input[i].Except) {
  68 + t.Fatal("not equal input:", input[i].Input, "except:", input[i].Except, "out:", fixPath(input[i].Input))
  69 + }
  70 + }
  71 +}
@@ -180,6 +180,7 @@ func CreateUploadImage(header *protocol.RequestHeader, request *aliyun.CreateUpl @@ -180,6 +180,7 @@ func CreateUploadImage(header *protocol.RequestHeader, request *aliyun.CreateUpl
180 request.FileName = aliyun.DefaultImageFileName 180 request.FileName = aliyun.DefaultImageFileName
181 } 181 }
182 rsp, err = aliyun.CreateUploadImage(client, request) 182 rsp, err = aliyun.CreateUploadImage(client, request)
  183 + rsp.FileURL = rsp.ImageURL
183 if err != nil { 184 if err != nil {
184 log.Error(err) 185 log.Error(err)
185 } 186 }