...
|
...
|
@@ -3,13 +3,13 @@ package contrab |
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
|
|
|
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
|
|
|
"opp/internal/aliyun"
|
|
|
"opp/internal/utils"
|
|
|
"opp/models"
|
|
|
"opp/protocol"
|
|
|
"path/filepath"
|
|
|
)
|
|
|
|
|
|
const LoopSize = 1
|
...
|
...
|
@@ -51,8 +51,9 @@ func MigrateChanceDataToAliYun() { |
|
|
//上传图片
|
|
|
data.Images = utils.JsonMarsh(uploadImages(data, data.Images))
|
|
|
//上传视频
|
|
|
data.Videos = utils.JsonMarsh(uploadVideos(data, data.Videos))
|
|
|
//上传音频
|
|
|
|
|
|
data.Speechs = utils.JsonMarsh(uploadVoice(data, data.Speechs))
|
|
|
//更新数据
|
|
|
o.Update(data)
|
|
|
}
|
...
|
...
|
@@ -61,6 +62,69 @@ func MigrateChanceDataToAliYun() { |
|
|
}
|
|
|
}
|
|
|
|
|
|
func uploadVideos(d *models.ChanceData, video string) (rsp []protocol.Video) {
|
|
|
utils.JsonUnmarshal(video, &rsp)
|
|
|
if len(rsp) == 0 {
|
|
|
rsp = make([]protocol.Video, 0)
|
|
|
return
|
|
|
}
|
|
|
for i := range rsp {
|
|
|
p := rsp[i]
|
|
|
if len(p.VideoId) > 0 {
|
|
|
printInfo(d, p.Path, Uploaded)
|
|
|
continue
|
|
|
}
|
|
|
var (
|
|
|
err error
|
|
|
auth *aliyun.CreateUploadVideoResponse
|
|
|
authImage *aliyun.CreateUploadImageResponse
|
|
|
vodc *vod.Client
|
|
|
)
|
|
|
vodc, err = aliyun.DefaultVodClient()
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
//上传视频
|
|
|
auth, err = aliyun.CreateUploadVideo(vodc, &aliyun.CreateUploadVideoRequest{FileName: p.Path})
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
err = upload(p.Path, auth.UploadAddress, auth.UploadAuth)
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
|
|
|
//备份路径
|
|
|
rsp[i].PathBak = p.Path
|
|
|
rsp[i].VideoId = auth.VideoId
|
|
|
rsp[i].Path = auth.FileURL
|
|
|
printInfo(d, fmt.Sprintf("%v -> %v", p.Path, auth.FileURL), UploadSuccess)
|
|
|
|
|
|
//上传封面
|
|
|
if len(p.Cover.Path) > 0 {
|
|
|
authImage, err = aliyun.CreateUploadImage(vodc, &aliyun.CreateUploadImageRequest{FileName: p.Cover.Path})
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
err = upload(p.Cover.Path, authImage.UploadAddress, authImage.UploadAuth)
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
printInfo(d, fmt.Sprintf("cover %v -> %v", p.Cover.Path, authImage.ImageURL), UploadSuccess)
|
|
|
rsp[i].Cover.Path = authImage.ImageURL
|
|
|
rsp[i].Cover.ImageId = authImage.ImageId
|
|
|
}
|
|
|
continue
|
|
|
ERR:
|
|
|
{
|
|
|
log.Error("ali vod error:", err)
|
|
|
printInfo(d, p.Path, UploadFail)
|
|
|
continue
|
|
|
}
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//上传图片
|
|
|
func uploadImages(d *models.ChanceData, img string) (rsp []protocol.Picture) {
|
|
|
utils.JsonUnmarshal(img, &rsp)
|
...
|
...
|
@@ -74,40 +138,92 @@ func uploadImages(d *models.ChanceData, img string) (rsp []protocol.Picture) { |
|
|
printInfo(d, p.Path, Uploaded)
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
var (
|
|
|
err error
|
|
|
vodc *vod.Client
|
|
|
imageRsp *aliyun.CreateUploadImageResponse
|
|
|
client *aliyun.OSSClient
|
|
|
err error
|
|
|
auth *aliyun.CreateUploadImageResponse
|
|
|
vodc *vod.Client
|
|
|
)
|
|
|
vodc, _ = aliyun.DefaultVodClient()
|
|
|
list := filepath.SplitList(p.Path)
|
|
|
if len(list) == 0 {
|
|
|
err = fmt.Errorf("路径无效:%v", p.Path)
|
|
|
vodc, err = aliyun.DefaultVodClient()
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
auth, err = aliyun.CreateUploadImage(vodc, &aliyun.CreateUploadImageRequest{FileName: p.Path})
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
imageRsp, err = aliyun.CreateUploadImage(vodc, &aliyun.CreateUploadImageRequest{FileName: list[len(list)-1]})
|
|
|
err = upload(p.Path, auth.UploadAddress, auth.UploadAuth)
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
client, err = aliyun.NewStsOSSClient(imageRsp.UploadAddress, imageRsp.UploadAuth)
|
|
|
|
|
|
//备份路径
|
|
|
rsp[i].PathBak = p.Path
|
|
|
rsp[i].ImageId = auth.ImageId
|
|
|
rsp[i].Path = auth.ImageURL
|
|
|
printInfo(d, fmt.Sprintf("%v -> %v", p.Path, auth.ImageURL), UploadSuccess)
|
|
|
continue
|
|
|
ERR:
|
|
|
{
|
|
|
log.Error("ali vod error:", err)
|
|
|
printInfo(d, p.Path, UploadFail)
|
|
|
continue
|
|
|
}
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//上传文件
|
|
|
func upload(path string, upAddress, upAuth string, options ...oss.Option) (err error) {
|
|
|
var (
|
|
|
client *aliyun.OSSClient
|
|
|
)
|
|
|
client, err = aliyun.NewStsOSSClient(upAddress, upAuth)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
err = client.PutObjectByUrlDefault(path, options...)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//上班媒体数据
|
|
|
func uploadVoice(d *models.ChanceData, data string) (rsp []protocol.Speech) {
|
|
|
utils.JsonUnmarshal(data, &rsp)
|
|
|
if len(rsp) == 0 {
|
|
|
rsp = make([]protocol.Speech, 0)
|
|
|
return
|
|
|
}
|
|
|
for i := range rsp {
|
|
|
p := rsp[i]
|
|
|
if len(p.VideoId) > 0 {
|
|
|
printInfo(d, p.Path, Uploaded)
|
|
|
continue
|
|
|
}
|
|
|
var (
|
|
|
err error
|
|
|
auth *aliyun.CreateUploadVideoResponse
|
|
|
vodc *vod.Client
|
|
|
)
|
|
|
vodc, err = aliyun.DefaultVodClient()
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
auth, err = aliyun.CreateUploadVideo(vodc, &aliyun.CreateUploadVideoRequest{FileName: p.Path})
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
err = client.PutObjectByUrlDefault(p.Path)
|
|
|
err = upload(p.Path, auth.UploadAddress, auth.UploadAuth, oss.ContentType("audio/mpeg"))
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
|
|
|
//备份路径
|
|
|
rsp[i].PathBak = p.Path
|
|
|
rsp[i].ImageId = imageRsp.ImageId
|
|
|
rsp[i].Path = imageRsp.ImageURL
|
|
|
printInfo(d, p.Path, UploadSuccess)
|
|
|
rsp[i].VideoId = auth.VideoId
|
|
|
rsp[i].Path = auth.FileURL
|
|
|
printInfo(d, fmt.Sprintf("%v -> %v", p.Path, auth.FileURL), UploadSuccess)
|
|
|
continue
|
|
|
ERR:
|
|
|
{
|
...
|
...
|
@@ -119,12 +235,6 @@ func uploadImages(d *models.ChanceData, img string) (rsp []protocol.Picture) { |
|
|
return
|
|
|
}
|
|
|
|
|
|
//上班媒体数据
|
|
|
func uploadVoice(d *models.ChanceData, url string) (rp []protocol.Speech) {
|
|
|
//
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
type ChanceDataExtend struct {
|
|
|
Id int64 `json:"id"`
|
|
|
ChanceId int64 `json:"chance_id"`
|
...
|
...
|
|