98 lines
1.9 KiB
Go
98 lines
1.9 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
err2 "yintai-api/app/common/err"
|
|
"yintai-api/base/global"
|
|
"yintai-api/base/global/storage"
|
|
)
|
|
|
|
type Qiniu struct{}
|
|
|
|
func NewQiniu() *Qiniu {
|
|
return &Qiniu{}
|
|
}
|
|
|
|
func (q Qiniu) Save(savePath, objectKey string) string {
|
|
cdnType := "qiniu"
|
|
err := global.GetStorage(cdnType).Put(savePath, objectKey, nil)
|
|
if err != nil {
|
|
s := fmt.Sprintf("upload file[%s] objeckt[%s] error :%s", savePath, objectKey, err.Error())
|
|
err2.InnerErr(s, err)
|
|
}
|
|
|
|
return objectKey
|
|
}
|
|
|
|
func (q Qiniu) Delete(objectName string) error {
|
|
storage := global.GetStorage("qiniu")
|
|
if storage == nil {
|
|
err2.InnerErr("<file service delete> storage not exists")
|
|
}
|
|
|
|
if err := storage.Del(objectName); err != nil {
|
|
if strings.Contains(err.Error(), "no such file") {
|
|
return nil
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (q Qiniu) QuarterUrl(objectKey string) string {
|
|
return global.GetStorage("qiniu").Url(objectKey, time.Hour/4)
|
|
}
|
|
|
|
func (q Qiniu) ForeverUrl(objectKey string) string {
|
|
return global.GetStorage("qiniu").Url(objectKey, 0)
|
|
}
|
|
|
|
func (q Qiniu) Url(objectKey string, expire time.Duration) string {
|
|
if objectKey == "" {
|
|
return ""
|
|
}
|
|
|
|
return global.GetStorage("qiniu").Url(objectKey, expire)
|
|
}
|
|
|
|
func (q Qiniu) PublicUrl(objectKey string) string {
|
|
return q.Url(objectKey, 0)
|
|
}
|
|
|
|
func (q Qiniu) Stat(objectKey string) (*storage.ObjectInfo, error) {
|
|
return global.GetStorage("qiniu").Stat(objectKey)
|
|
}
|
|
|
|
func (q Qiniu) ObjectKey(s string) string {
|
|
s = strings.TrimSpace(s)
|
|
if len(s) == 0 {
|
|
return ""
|
|
}
|
|
|
|
s, _ = url.QueryUnescape(s)
|
|
|
|
domain := global.GetConfig().Storage.QiniuDomain
|
|
start := strings.Index(s, domain)
|
|
if start == -1 {
|
|
start = 0
|
|
} else {
|
|
start = len(domain) + 1
|
|
}
|
|
end := strings.Index(s, "?")
|
|
if end == -1 {
|
|
end = len(s)
|
|
}
|
|
|
|
p := s[start:end]
|
|
return p
|
|
}
|
|
|
|
func (q Qiniu) UploadToken() string {
|
|
return global.GetStorage().UploadToken()
|
|
}
|