You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.4 KiB
133 lines
3.4 KiB
9 months ago
|
package service
|
||
|
|
||
|
import (
|
||
|
"crypto/md5"
|
||
|
"crypto/sha1"
|
||
|
"crypto/sha256"
|
||
|
"encoding/hex"
|
||
|
"encoding/json"
|
||
|
"fm-upload/internal/biz"
|
||
|
"fm-upload/internal/conf"
|
||
|
"fm-upload/internal/data/model"
|
||
|
response "fm-upload/internal/data/model"
|
||
|
"fm-upload/internal/data/model/request"
|
||
|
"hash"
|
||
|
"io"
|
||
|
"path/filepath"
|
||
|
"regexp"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"errors"
|
||
|
|
||
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
const obs = "bkb-obs-dev"
|
||
|
|
||
|
// GreeterService is a greeter service.
|
||
|
type StorageService struct {
|
||
|
conf *conf.System
|
||
|
uc *biz.StorageUsecase
|
||
|
ic *biz.ImageUsecase
|
||
|
}
|
||
|
|
||
|
// NewGreeterService new a greeter service.
|
||
|
func NewStorageService(uc *biz.StorageUsecase, ic *biz.ImageUsecase, conf *conf.System) *StorageService {
|
||
|
return &StorageService{uc: uc, ic: ic, conf: conf}
|
||
|
}
|
||
|
|
||
|
// SayHello implements helloworld.GreeterServer.
|
||
|
func (s *StorageService) Upload(c *gin.Context, in *request.UploadRequest) (*request.UploadResponse, error) {
|
||
|
//文件类型,大小
|
||
|
var oName string
|
||
|
|
||
|
// fmt.Println(x.File.Header)
|
||
|
if in.Type == "image" || in.Type == "avatar" {
|
||
|
if in.File.Size > int64(s.conf.ImageSize)*1048576 {
|
||
|
return nil, errors.New("image file too large")
|
||
|
}
|
||
|
oName = in.Type + "/"
|
||
|
} else if in.Type == "video" {
|
||
|
if in.File.Size > int64(s.conf.VideoSize)*1048576 {
|
||
|
return nil, errors.New("video file too large")
|
||
|
}
|
||
|
oName = "video/"
|
||
|
} else {
|
||
|
if in.File.Size > int64(s.conf.DocSize)*1048576 {
|
||
|
return nil, errors.New("doc file too large")
|
||
|
}
|
||
|
oName = "doc/"
|
||
|
}
|
||
|
//文件名
|
||
|
fileName := strings.ReplaceAll(in.FileName, " ", "") //指定
|
||
|
|
||
|
fileEnd := filepath.Ext(in.File.Filename)
|
||
|
fileId := utils.GetUUID()
|
||
|
var key string
|
||
|
if fileName != "" {
|
||
|
re := regexp.MustCompile("[\u4e00-\u9fa5_\\-a-zA-Z0-9\\.]+")
|
||
|
t := re.FindAllString(fileName, -1)
|
||
|
fileName = strings.Join(t, "")
|
||
|
}
|
||
|
if fileName != "" {
|
||
|
re, _ := regexp.Compile(`\.([^.]*)$`)
|
||
|
t := re.FindStringIndex(fileName)
|
||
|
if t != nil {
|
||
|
fileId = fileName[:t[0]] + "_" + fileId[:10]
|
||
|
fileEnd = fileName[t[0]:]
|
||
|
} else {
|
||
|
fileId = fileName + "_" + fileId[:10]
|
||
|
}
|
||
|
}
|
||
|
key = oName + time.Now().Format("20060102") + "/" + fileId
|
||
|
fileEnd = strings.ToLower(fileEnd)
|
||
|
key += fileEnd
|
||
|
resData := request.UploadResponse{UploadFileName: in.File.Filename,
|
||
|
VframeTime: -1}
|
||
|
func() {
|
||
|
if len(in.Hash) > 0 {
|
||
|
fileHash := ""
|
||
|
var hh hash.Hash
|
||
|
if in.Hash == "sha256" {
|
||
|
hh = sha256.New()
|
||
|
} else if in.Hash == "sha1" {
|
||
|
hh = sha1.New()
|
||
|
} else if in.Hash == "md5" {
|
||
|
hh = md5.New()
|
||
|
} else {
|
||
|
return
|
||
|
}
|
||
|
file, _ := in.File.Open()
|
||
|
if _, err := io.Copy(hh, file); err == nil {
|
||
|
fileHash = hex.EncodeToString(hh.Sum(nil))
|
||
|
}
|
||
|
resData.Hash = fileHash
|
||
|
file.Seek(0, 0)
|
||
|
}
|
||
|
}()
|
||
|
return s.uc.UploadToServer(c, oName, resData, key, in.File, obs)
|
||
|
}
|
||
|
|
||
|
// SayHello implements helloworld.GreeterServer.
|
||
|
func (s *StorageService) Ping(c *gin.Context) {
|
||
|
response.Ok(c)
|
||
|
}
|
||
|
|
||
|
func (s *StorageService) ImagesController(c *gin.Context, in *request.CompressImagesRequest) (*request.ImagesCompressResponse, error) {
|
||
|
var (
|
||
|
err error
|
||
|
p []model.ImageParams
|
||
|
)
|
||
|
err = json.Unmarshal([]byte(in.ProcessParams), &p)
|
||
|
if err != nil {
|
||
|
return nil, errors.New("processParams format error")
|
||
|
}
|
||
|
return s.ic.ImagesController(c, in, p, obs)
|
||
|
}
|
||
|
|
||
|
func (s *StorageService) ImageController(c *gin.Context, in *request.CompressImageRequest) (*model.ImageCompressResponse, error) {
|
||
|
return s.ic.ImageController(c, in, obs)
|
||
|
}
|