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.
348 lines
9.6 KiB
348 lines
9.6 KiB
package biz
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"errors"
|
|
"fm-upload/internal/data/model"
|
|
"fm-upload/internal/data/model/request"
|
|
"fm-upload/utils"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
)
|
|
|
|
type ImageRepo interface {
|
|
ImageDeal(ctx context.Context, tmpfile string, width uint, height uint, x *model.ImageParams, ofmt, fileM, fileId, bucket string) (ret *model.MyPutRet, resUrl, errd string, code int, err error)
|
|
}
|
|
|
|
type ImageUsecase struct {
|
|
irepo ImageRepo
|
|
srepo StorageRepo
|
|
log *log.Helper
|
|
}
|
|
|
|
// NewGreeterUsecase new a Greeter usecase.
|
|
func NewImageUsecase(irepo ImageRepo, srepo StorageRepo, logger log.Logger) *ImageUsecase {
|
|
return &ImageUsecase{irepo: irepo, srepo: srepo, log: log.NewHelper(logger)}
|
|
}
|
|
|
|
func (ic *ImageUsecase) ImagesController(ctx context.Context, x *request.CompressImagesRequest, p []model.ImageParams, bucket string) (*request.ImagesCompressResponse, error) {
|
|
var (
|
|
httpClient *http.Client
|
|
err error
|
|
)
|
|
tr := &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
}
|
|
httpClient = &http.Client{Transport: tr}
|
|
|
|
// mw := imagick.NewMagickWand()
|
|
// defer mw.Destroy()
|
|
var imageBytes []byte
|
|
tmpfile := "/tmp/" + utils.GetUUID()
|
|
defer func() {
|
|
_ = os.Remove(tmpfile)
|
|
}()
|
|
// var file io.reader
|
|
if x.ImageUrl != "" {
|
|
resp, err1 := httpClient.Get(x.ImageUrl)
|
|
if err1 != nil {
|
|
return nil, errors.New("访问url出错")
|
|
}
|
|
defer resp.Body.Close()
|
|
imageBytes, err = io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, errors.New("访问url出错")
|
|
}
|
|
err = os.WriteFile(tmpfile, imageBytes, 0777)
|
|
if err != nil {
|
|
return nil, errors.New("image process error")
|
|
}
|
|
// file = bytes.NewReader(imageBytes)
|
|
} else {
|
|
file, _ := x.File.Open()
|
|
err = utils.Tofile(&file, tmpfile)
|
|
if err != nil {
|
|
return nil, errors.New("image process error")
|
|
}
|
|
imageBytes = make([]byte, x.File.Size)
|
|
_, err := io.ReadFull(file, imageBytes)
|
|
if err != nil {
|
|
return nil, errors.New("imager file error")
|
|
}
|
|
}
|
|
cmdString := "magick identify " + tmpfile
|
|
t1, err := utils.RunCommand(cmdString, nil, true)
|
|
if err != nil {
|
|
return nil, errors.New("image process error")
|
|
}
|
|
ts := strings.Split(string(t1), " ")
|
|
ofmt := ts[1]
|
|
re := regexp.MustCompile(`(\d+)x(\d+)`)
|
|
t3 := re.FindStringSubmatch(ts[2])
|
|
t4, _ := strconv.Atoi(t3[1])
|
|
width := uint(t4)
|
|
t4, _ = strconv.Atoi(t3[2])
|
|
height := uint(t4)
|
|
// fmt.Println(width, height)
|
|
var fileId, fileM, originkey string
|
|
x.OriginImageName = strings.ReplaceAll(x.OriginImageName, " ", "")
|
|
if x.ImageUrl == "" && x.OriginImageName != "" {
|
|
re := regexp.MustCompile("[\u4e00-\u9fa5_\\-a-zA-Z0-9\\.]+")
|
|
t := re.FindAllString(x.OriginImageName, -1)
|
|
fileM = "image/" + time.Now().Format("20060102") + "/"
|
|
originkey = fileM + strings.Join(t, "")
|
|
|
|
re, _ = regexp.Compile(`\.(\w{3,4})$`)
|
|
t2 := re.FindStringIndex(originkey)
|
|
if t2 != nil {
|
|
fileId = originkey[:t2[0]]
|
|
} else {
|
|
fileId = originkey
|
|
}
|
|
} else {
|
|
if x.ImageUrl != "" {
|
|
re := regexp.MustCompile(`\.com/(.+)`)
|
|
t := re.FindStringSubmatch(x.ImageUrl)
|
|
if len(t) > 0 {
|
|
fileId = t[1]
|
|
re = regexp.MustCompile(`\.\w{3,4}$`)
|
|
t2 := re.FindStringIndex(fileId)
|
|
if len(t2) > 0 {
|
|
fileId = fileId[:t2[0]]
|
|
}
|
|
re, _ = regexp.Compile(`(.+/)[^/]+$`)
|
|
t3 := re.FindStringSubmatch(fileId)
|
|
fileM = t3[1]
|
|
}
|
|
}
|
|
if fileId == "" {
|
|
fileM = "image/" + time.Now().Format("20060102") + "/"
|
|
fileId = fileM + utils.GetUUID()
|
|
}
|
|
originkey = fileId + "." + strings.ToLower(ofmt)
|
|
}
|
|
resData := request.ImagesCompressResponse{}
|
|
if x.ImageUrl != "" {
|
|
resData.OriginImageUrl = x.ImageUrl
|
|
} else {
|
|
resData.UploadFileName = x.File.Filename
|
|
}
|
|
if !x.IgnoreOriginImage && x.ImageUrl == "" {
|
|
ret, err := ic.srepo.SaveFileToHw(ctx, tmpfile, originkey, bucket)
|
|
if err != nil {
|
|
return nil, errors.New("error upload to huaweiyun")
|
|
}
|
|
resData.OriginImageUrl = ret.ReturnPrefix + originkey
|
|
}
|
|
resData.OriginWidth = width
|
|
resData.OriginHeight = height
|
|
resData.OriginFileSize = len(imageBytes)
|
|
// fs := float32(len(imageBytes)) / 1024
|
|
// if fs < 1024 {
|
|
// resData["originFileSize"] = fmt.Sprintf("%.2fk", fs)
|
|
// } else {
|
|
// resData["originFileSize"] = fmt.Sprintf("%.2fm", fs/1024)
|
|
// }
|
|
t := make([]map[string]string, len(p))
|
|
for i, y := range p {
|
|
if i > 0 {
|
|
_ = os.Remove(tmpfile)
|
|
_ = os.WriteFile(tmpfile, imageBytes, 0777)
|
|
}
|
|
v := map[string]string{}
|
|
ret, u, _, errcode, err := ic.irepo.ImageDeal(ctx, tmpfile, width, height, &y, ofmt, fileM, fileId, bucket)
|
|
if errcode > 0 {
|
|
return nil, err
|
|
}
|
|
v["url"] = u
|
|
v["fileSize"] = fmt.Sprintf("%d", ret.Fsize)
|
|
// fs := float32(ret.Fsize) / 1024
|
|
// if fs < 1024 {
|
|
// v["fileSize"] = fmt.Sprintf("%.2fk", fs)
|
|
// } else {
|
|
// v["fileSize"] = fmt.Sprintf("%.2fm", fs/1024)
|
|
// }
|
|
v["width"] = fmt.Sprintf("%d", ret.Width)
|
|
v["height"] = fmt.Sprintf("%d", ret.Height)
|
|
t[i] = v
|
|
}
|
|
resData.ProcessImages = t
|
|
return &resData, nil
|
|
}
|
|
|
|
func (ic *ImageUsecase) ImageController(ctx context.Context, x *request.CompressImageRequest, bucket string) (*model.ImageCompressResponse, error) {
|
|
var (
|
|
httpClient *http.Client
|
|
err error
|
|
)
|
|
tr := &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
}
|
|
httpClient = &http.Client{Transport: tr}
|
|
// mw := imagick.NewMagickWand()
|
|
// defer mw.Destroy()
|
|
var imageBytes []byte
|
|
tmpfile := "/tmp/" + utils.GetUUID()
|
|
defer func() {
|
|
_ = os.Remove(tmpfile)
|
|
}()
|
|
// var file io.reader
|
|
if x.ImageUrl != "" {
|
|
resp, err1 := httpClient.Get(x.ImageUrl)
|
|
if err1 != nil {
|
|
return nil, errors.New("访问url出错")
|
|
}
|
|
defer resp.Body.Close()
|
|
imageBytes, err = ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, errors.New("访问url出错")
|
|
}
|
|
err = os.WriteFile(tmpfile, imageBytes, 0666)
|
|
if err != nil {
|
|
return nil, errors.New("image process error")
|
|
}
|
|
// file = bytes.NewReader(imageBytes)
|
|
} else {
|
|
file, _ := x.File.Open()
|
|
err = utils.Tofile(&file, tmpfile)
|
|
if err != nil {
|
|
return nil, errors.New("image process error")
|
|
}
|
|
// if x.Format == "webp" {
|
|
// imageBytes = make([]byte, x.File.Size)
|
|
// _, err := io.ReadFull(file, imageBytes)
|
|
// if err != nil {
|
|
// logjson["resData"] = "imager file error"
|
|
// logjson["error"] = err
|
|
// logjson["resStatus"] = 210
|
|
// routes.PrintError(logjson, true)
|
|
// response.ResponseWithError(c, 210, nil, "imager file error", err)
|
|
// return
|
|
// }
|
|
// }
|
|
}
|
|
//
|
|
cmdString := "magick identify " + tmpfile
|
|
t1, err := utils.RunCommand(cmdString, nil, true)
|
|
if err != nil {
|
|
return nil, errors.New("image process error")
|
|
}
|
|
ts := strings.Split(string(t1), " ")
|
|
ofmt := ts[1]
|
|
re := regexp.MustCompile(`(\d+)x(\d+)`)
|
|
t3 := re.FindStringSubmatch(ts[2])
|
|
t4, _ := strconv.Atoi(t3[1])
|
|
width := uint(t4)
|
|
t4, _ = strconv.Atoi(t3[2])
|
|
height := uint(t4)
|
|
|
|
var fileId, fileM, originkey string
|
|
x.OriginImageName = strings.ReplaceAll(x.OriginImageName, " ", "")
|
|
if x.ImageUrl == "" && x.OriginImageName != "" {
|
|
re := regexp.MustCompile("[\u4e00-\u9fa5_\\-a-zA-Z0-9\\.]+")
|
|
t := re.FindAllString(x.OriginImageName, -1)
|
|
fileM = "image/" + time.Now().Format("20060102") + "/"
|
|
originkey = fileM + strings.Join(t, "")
|
|
|
|
re, _ = regexp.Compile(`\.(\w{3,4})$`)
|
|
t2 := re.FindStringIndex(originkey)
|
|
if t2 != nil {
|
|
fileId = originkey[:t2[0]]
|
|
} else {
|
|
fileId = originkey
|
|
}
|
|
} else {
|
|
if x.ImageUrl != "" {
|
|
re := regexp.MustCompile(`\.com/(.+)`)
|
|
t := re.FindStringSubmatch(x.ImageUrl)
|
|
if len(t) > 0 {
|
|
originkey = t[1]
|
|
fileId = originkey
|
|
re = regexp.MustCompile(`\.\w{3,4}$`)
|
|
t2 := re.FindStringIndex(fileId)
|
|
if len(t2) > 0 {
|
|
fileId = fileId[:t2[0]]
|
|
}
|
|
re, _ = regexp.Compile(`(.+/)[^/]+$`)
|
|
t3 := re.FindStringSubmatch(fileId)
|
|
fileM = t3[1]
|
|
}
|
|
// fileEnd = filepath.Ext(x.ImageUrl)
|
|
}
|
|
if fileId == "" {
|
|
fileM = "image/" + time.Now().Format("20060102") + "/"
|
|
fileId = fileM + utils.GetUUID()
|
|
}
|
|
if originkey == "" {
|
|
originkey = fileId + "." + strings.ToLower(ofmt)
|
|
}
|
|
}
|
|
resData := model.ImageCompressResponse{}
|
|
if x.ImageUrl != "" {
|
|
resData.OriginImageUrl = x.ImageUrl
|
|
} else {
|
|
resData.UploadFileName = x.File.Filename
|
|
}
|
|
if !x.IgnoreOriginImage && x.ImageUrl == "" {
|
|
ret, err := ic.srepo.SaveFileToHw(ctx, tmpfile, originkey, bucket)
|
|
if err != nil {
|
|
return nil, errors.New("error upload to huaweiyun")
|
|
}
|
|
resData.OriginImageUrl = ret.ReturnPrefix + originkey
|
|
}
|
|
// fs := float32(0)
|
|
// if len(imageBytes) > 0 {
|
|
// fs = float32(len(imageBytes)) / 1024
|
|
// } else {
|
|
// fs = float32(x.File.Size) / 1024
|
|
// }
|
|
// if fs < 1024 {
|
|
// resData["originFileSize"] = fmt.Sprintf("%.2fk", fs)
|
|
// } else {
|
|
// resData["originFileSize"] = fmt.Sprintf("%.2fm", fs/1024)
|
|
// }
|
|
if len(imageBytes) > 0 {
|
|
resData.OriginFileSize = len(imageBytes)
|
|
} else {
|
|
resData.OriginFileSize = int(x.File.Size)
|
|
}
|
|
|
|
resData.OriginWidth = width
|
|
resData.OriginHeight = height
|
|
if x.OverwriteOriginImage {
|
|
bucket = bucket + ":" + originkey
|
|
} else if x.OverwriteProcessImage {
|
|
bucket = bucket + ":"
|
|
}
|
|
ret, u, _, errcode, err := ic.irepo.ImageDeal(ctx, tmpfile, width, height, &x.ImageParams, ofmt, fileM, fileId, bucket)
|
|
if errcode > 0 {
|
|
return nil, errors.New(err.Error())
|
|
}
|
|
if u == "" {
|
|
u = resData.OriginImageUrl
|
|
}
|
|
t5 := map[string]string{
|
|
"url": u,
|
|
"width": fmt.Sprintf("%d", ret.Width),
|
|
"height": fmt.Sprintf("%d", ret.Height),
|
|
}
|
|
t5["fileSize"] = fmt.Sprintf("%d", ret.Fsize)
|
|
// fs = float32(ret.Fsize) / 1024
|
|
// if fs < 1024 {
|
|
// t5["fileSize"] = fmt.Sprintf("%.2fk", fs)
|
|
// } else {
|
|
// t5["fileSize"] = fmt.Sprintf("%.2fm", fs/1024)
|
|
// }
|
|
resData.ProcessImage = t5
|
|
return &resData, nil
|
|
}
|
|
|