Commit 29ab7969 by Li Feifei

feat:webp: 解析转换图片为webp

parent f90a3497
Pipeline #17114 failed with stages
in 4 seconds
......@@ -3,8 +3,15 @@ module ppt_server
go 1.16
require (
github.com/aliyun/aliyun-oss-go-sdk v2.1.9+incompatible // indirect
github.com/gogf/gf v1.16.4 // indirect
github.com/xxjwxc/gowp v0.0.0-20210520113007-57eb4693b12d // indirect
github.com/aliyun/aliyun-oss-go-sdk v2.1.9+incompatible
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/chai2010/webp v1.1.0
github.com/gogf/gf v1.16.6
github.com/gogf/mysql v1.6.1-0.20210603073548-16164ae25579 // indirect
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/xxjwxc/gowp v0.0.0-20210520113007-57eb4693b12d
gitlab.eoffcn.com/cloud/goforest v0.3.4 // indirect
go.opentelemetry.io/otel/metric v0.19.0 // indirect
golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6 // indirect
)
......@@ -3,11 +3,11 @@ package file
import (
"fmt"
"mime"
"net/http"
"path"
"strings"
"github.com/gogf/gf/encoding/gurl"
"github.com/gogf/gf/errors/gcode"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/text/gstr"
......@@ -32,7 +32,7 @@ func parseURL(ossFileLink string) (*parser, error) {
slicePath := gstr.SplitAndTrim(parseURLPath["path"], "/", "/")
if len(slicePath) < 4 {
return nil, gerror.NewCode(http.StatusBadRequest, "上传URL格式不正确")
return nil, gerror.NewCode(gcode.CodeInvalidRequest, "上传URL格式不正确")
}
f := &parser{
ext: path.Ext(ossFileLink),
......
......@@ -8,7 +8,6 @@ import (
"image"
"image/jpeg"
"image/png"
"io/ioutil"
"os"
"path"
"regexp"
......@@ -18,6 +17,7 @@ import (
"ppt_server/library/http"
"ppt_server/library/oss"
"github.com/chai2010/webp"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gtime"
......@@ -45,7 +45,6 @@ const (
var reFileNameCompile, _ = regexp.Compile(`p([\d]+)[.].*`)
var reTokenPattern = `<meta name="csrf-token" content="([^"]+)">`
func Upload(r model.FileUploadRequest, errChan chan error,
ok chan bool) (file *ObtainFile, err error) {
......@@ -103,49 +102,40 @@ func (f *ObtainFile) taskJob(file *zip.File) ([]interface{}, error) {
if err != nil {
return nil, err
}
defer fd.Close()
pictureBytes, _ := ioutil.ReadAll(fd)
// 获取图片信息reader
imageReader := ioutil.NopCloser(bytes.NewBuffer(pictureBytes))
defer imageReader.Close()
// 上传图片reader
fileReader := ioutil.NopCloser(bytes.NewBuffer(pictureBytes))
defer fileReader.Close()
var img image.Image
fileSuffix := path.Ext(file.Name) //获取文件后缀
switch fileSuffix {
case ".png":
img, err = png.Decode(imageReader)
img, err = png.Decode(fd)
case ".jpeg":
img, err = jpeg.Decode(imageReader)
img, err = jpeg.Decode(fd)
default:
return nil, gerror.New("image ext is not found")
}
if err != nil {
return nil, err
}
replaceName := fmt.Sprintf("%05s", reFileNameCompile.ReplaceAllString(file.Name, "$1")) + fileSuffix
var buf bytes.Buffer
if err = webp.Encode(&buf, img, &webp.Options{Lossless: true}); err != nil {
return nil, err
}
replaceName := fmt.Sprintf("%05s", reFileNameCompile.ReplaceAllString(file.Name, "$1")) + ".webp"
info := img.Bounds()
imageInfo := []interface{}{replaceName, info.Max.X, info.Max.Y}
return imageInfo, oss.Upload(f.parser.splitUploadPath()+"images/"+replaceName, fileReader)
return imageInfo, oss.Upload(f.parser.splitUploadPath()+"images/"+replaceName, bytes.NewReader(buf.Bytes()))
}
func (f *ObtainFile) task(file *zip.File) error {
var (
err error
imageInfo []interface{}
)
func (f *ObtainFile) task(file *zip.File) (err error) {
for i := 0; i < 3; i++ {
if imageInfo, err = f.taskJob(file); err == nil {
if imageInfo, err := f.taskJob(file); err == nil {
f.packed.ImageInfos = append(f.packed.ImageInfos, imageInfo)
break
}
}
return err
return
}
func (f *ObtainFile) uploadPicture() error {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment