...
|
...
|
@@ -3,16 +3,17 @@ 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"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
const LoopSize = 1
|
|
|
const LoopSize = 20
|
|
|
|
|
|
const (
|
|
|
Uploaded = "已上传,跳过"
|
...
|
...
|
@@ -31,12 +32,12 @@ func MigrateChanceDataToAliYun() { |
|
|
log.Info("【迁移机会媒体数据】开始迁移")
|
|
|
defer log.Info("【迁移机会媒体数据】结束迁移")
|
|
|
var (
|
|
|
datas []*models.ChanceData
|
|
|
query = `select * from chance_data order by id limit ?,?`
|
|
|
pageInfo = protocol.PageInfo{PageSize: LoopSize}
|
|
|
)
|
|
|
o := orm.NewOrm()
|
|
|
for {
|
|
|
var datas []*models.ChanceData
|
|
|
if _, e := o.Raw(query, pageInfo.Offset(), pageInfo.PageSize).QueryRows(&datas); e != nil {
|
|
|
if e == orm.ErrNoRows {
|
|
|
break
|
...
|
...
|
@@ -44,23 +45,90 @@ func MigrateChanceDataToAliYun() { |
|
|
log.Error(e)
|
|
|
break
|
|
|
}
|
|
|
log.Info("当前页:", pageInfo.PageIndex, "起始:", pageInfo.Offset(), pageInfo.PageSize, " 行数:", len(datas))
|
|
|
if len(datas) == 0 {
|
|
|
break
|
|
|
}
|
|
|
|
|
|
for i := range datas {
|
|
|
data := datas[i]
|
|
|
|
|
|
//上传图片
|
|
|
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)
|
|
|
}
|
|
|
pageInfo.PageIndex += 1
|
|
|
break
|
|
|
//break
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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: fixPath(p.Path)})
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
err = upload(fixPath(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: fixPath(p.Cover.Path)})
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
err = upload(fixPath(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 +142,115 @@ func uploadImages(d *models.ChanceData, img string) (rsp []protocol.Picture) { |
|
|
printInfo(d, p.Path, Uploaded)
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
var (
|
|
|
err error
|
|
|
auth *aliyun.CreateUploadImageResponse
|
|
|
vodc *vod.Client
|
|
|
imageRsp *aliyun.CreateUploadImageResponse
|
|
|
client *aliyun.OSSClient
|
|
|
)
|
|
|
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: fixPath(p.Path)})
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
err = upload(fixPath(p.Path), auth.UploadAddress, auth.UploadAuth)
|
|
|
if err != nil {
|
|
|
goto ERR
|
|
|
}
|
|
|
imageRsp, err = aliyun.CreateUploadImage(vodc, &aliyun.CreateUploadImageRequest{FileName: list[len(list)-1]})
|
|
|
|
|
|
//备份路径
|
|
|
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 fixPath(path string) string {
|
|
|
if strings.Index(path, "https:///") >= 0 {
|
|
|
return strings.Replace(path, "https:///", "https://", 1)
|
|
|
}
|
|
|
if strings.Index(path, "https://") >= 0 {
|
|
|
return strings.Replace(path, "https:///", "https://", 1)
|
|
|
}
|
|
|
if strings.Index(path, "https:/") >= 0 {
|
|
|
return strings.Replace(path, "https:/", "https://", 1)
|
|
|
}
|
|
|
|
|
|
if strings.Index(path, "http:///") >= 0 {
|
|
|
return strings.Replace(path, "http:///", "http://", 1)
|
|
|
}
|
|
|
if strings.Index(path, "http://") >= 0 {
|
|
|
return strings.Replace(path, "http:///", "http://", 1)
|
|
|
}
|
|
|
if strings.Index(path, "http:/") >= 0 {
|
|
|
return strings.Replace(path, "http:/", "http://", 1)
|
|
|
}
|
|
|
return path
|
|
|
}
|
|
|
|
|
|
//上传文件
|
|
|
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
|
|
|
}
|
|
|
client, err = aliyun.NewStsOSSClient(imageRsp.UploadAddress, imageRsp.UploadAuth)
|
|
|
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 +262,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"`
|
...
|
...
|
|