138 lines
4.0 KiB
Go
138 lines
4.0 KiB
Go
package qiniu
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/base64"
|
|
"strings"
|
|
|
|
"github.com/anxpp/beego"
|
|
"github.com/anxpp/beego/logs"
|
|
"github.com/qiniu/go-sdk/v7/auth/qbox"
|
|
"github.com/qiniu/go-sdk/v7/storage"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func getKey() string {
|
|
return viper.GetString("qiniu.key")
|
|
}
|
|
|
|
func getSecret() string {
|
|
return viper.GetString("qiniu.secret")
|
|
}
|
|
|
|
// 获取上传图片的token
|
|
func GenerateUploadPhotoToken(expireSeconds uint64) (token string) {
|
|
bucket := viper.GetString("qiniu.bucket.photo")
|
|
putPolicy := storage.PutPolicy{
|
|
Scope: bucket,
|
|
ReturnBody: `{"key":"$(key)","w": $(imageInfo.width), "h": $(imageInfo.height)}`,
|
|
MimeLimit: "image/jpeg;image/png",
|
|
}
|
|
//有效期
|
|
putPolicy.Expires = expireSeconds
|
|
mac := qbox.NewMac(getKey(), getSecret())
|
|
token = putPolicy.UploadToken(mac)
|
|
return
|
|
}
|
|
|
|
// 获取后台上传到七牛的token
|
|
func GenerateOperationUploadToken(expireSeconds uint64) (token string) {
|
|
bucket := viper.GetString("qiniu.bucket.photo")
|
|
putPolicy := storage.PutPolicy{
|
|
Scope: bucket,
|
|
ReturnBody: `{"key":"$(key)"}`,
|
|
MimeLimit: "!application/json;text/plain",
|
|
}
|
|
// 有效期
|
|
putPolicy.Expires = expireSeconds
|
|
mac := qbox.NewMac(getKey(), getSecret())
|
|
token = putPolicy.UploadToken(mac)
|
|
|
|
return
|
|
}
|
|
|
|
// 获取上传音频的token
|
|
func GenerateUploadAudioToken(expireSeconds uint64) (token string) {
|
|
bucket := viper.GetString("qiniu.bucket.media")
|
|
saveMp3Entry := base64.URLEncoding.EncodeToString([]byte(bucket + ":$(key).mp3"))
|
|
//数据处理指令,支持多个指令
|
|
avthumbMp3Fop := "avthumb/mp3/ab/192k|saveas/" + saveMp3Entry
|
|
//连接多个操作指令
|
|
persistentOps := strings.Join([]string{avthumbMp3Fop}, ";")
|
|
pipeline := "FYToMP3Quene"
|
|
putPolicy := storage.PutPolicy{
|
|
Scope: bucket,
|
|
PersistentOps: persistentOps,
|
|
PersistentPipeline: pipeline,
|
|
PersistentNotifyURL: viper.GetString("open.notify.qiniuMusicConvert"),
|
|
}
|
|
putPolicy.Expires = expireSeconds
|
|
// 转码通知
|
|
logs.Info("GenerateUploadAudioToken.notifyUrl: " + viper.GetString("open.notify.qiniuMusicConvert"))
|
|
mac := qbox.NewMac(getKey(), getSecret())
|
|
upToken := putPolicy.UploadToken(mac)
|
|
token = upToken
|
|
return
|
|
}
|
|
|
|
// 获取上传视频的token
|
|
func GenerateUploadVideoToken(expireSeconds uint64) (token string) {
|
|
bucket := viper.GetString("qiniu.bucket.video")
|
|
saveMp4Entry := base64.URLEncoding.EncodeToString([]byte(bucket + ":$(key).mp4"))
|
|
saveJpgEntry := base64.URLEncoding.EncodeToString([]byte(bucket + ":$(key).jpg"))
|
|
//数据处理指令,支持多个指令
|
|
avthumbMp4Fop := "avthumb/mp4/vcodec/libx264/s/1280x1280/autoscale/1/avsmart/1/enhance/0|saveas/" + saveMp4Entry
|
|
vframeJpgFop := "vframe/jpg/offset/1|saveas/" + saveJpgEntry
|
|
//连接多个操作指令
|
|
// persistentOps := strings.Join([]string{avthumbMp4Fop, vframeJpgFop}, ";")
|
|
persistentOps := strings.Join([]string{avthumbMp4Fop, vframeJpgFop}, ";")
|
|
pipeline := "FYVideoToMP4Quene"
|
|
putPolicy := storage.PutPolicy{
|
|
Scope: bucket,
|
|
PersistentOps: persistentOps,
|
|
PersistentPipeline: pipeline,
|
|
}
|
|
putPolicy.Expires = expireSeconds
|
|
|
|
mac := qbox.NewMac(getKey(), getSecret())
|
|
upToken := putPolicy.UploadToken(mac)
|
|
|
|
token = upToken
|
|
return
|
|
}
|
|
|
|
func UploadFile(ctx context.Context, bucket string, fileByte []byte, key string) (fileKey string) {
|
|
putPolicy := storage.PutPolicy{
|
|
Scope: bucket,
|
|
Expires: 600, //10分钟过期
|
|
}
|
|
mac := qbox.NewMac(getKey(), getSecret())
|
|
upToken := putPolicy.UploadToken(mac)
|
|
cfg := storage.Config{}
|
|
// 空间对应的机房
|
|
cfg.Zone = &storage.ZoneHuadong
|
|
// 是否使用https域名
|
|
cfg.UseHTTPS = false
|
|
// 上传是否使用CDN上传加速
|
|
cfg.UseCdnDomains = false
|
|
// 构建表单上传的对象
|
|
formUploader := storage.NewFormUploader(&cfg)
|
|
ret := storage.PutRet{}
|
|
// 可选配置
|
|
putExtra := storage.PutExtra{}
|
|
|
|
dataLen := int64(len(fileByte))
|
|
|
|
err := formUploader.Put(ctx, &ret, upToken, key, bytes.NewReader(fileByte), dataLen, &putExtra)
|
|
if err != nil {
|
|
beego.BeeLogger.Error("上传失败" + err.Error())
|
|
fileKey = ""
|
|
return
|
|
}
|
|
|
|
//上传成功
|
|
fileKey = key
|
|
return
|
|
}
|