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 ...@@ -3,8 +3,15 @@ module ppt_server
go 1.16 go 1.16
require ( require (
github.com/aliyun/aliyun-oss-go-sdk v2.1.9+incompatible // indirect github.com/aliyun/aliyun-oss-go-sdk v2.1.9+incompatible
github.com/gogf/gf v1.16.4 // indirect github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/xxjwxc/gowp v0.0.0-20210520113007-57eb4693b12d // 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 golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6 // indirect
) )
...@@ -3,11 +3,11 @@ package file ...@@ -3,11 +3,11 @@ package file
import ( import (
"fmt" "fmt"
"mime" "mime"
"net/http"
"path" "path"
"strings" "strings"
"github.com/gogf/gf/encoding/gurl" "github.com/gogf/gf/encoding/gurl"
"github.com/gogf/gf/errors/gcode"
"github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g" "github.com/gogf/gf/frame/g"
"github.com/gogf/gf/text/gstr" "github.com/gogf/gf/text/gstr"
...@@ -32,7 +32,7 @@ func parseURL(ossFileLink string) (*parser, error) { ...@@ -32,7 +32,7 @@ func parseURL(ossFileLink string) (*parser, error) {
slicePath := gstr.SplitAndTrim(parseURLPath["path"], "/", "/") slicePath := gstr.SplitAndTrim(parseURLPath["path"], "/", "/")
if len(slicePath) < 4 { if len(slicePath) < 4 {
return nil, gerror.NewCode(http.StatusBadRequest, "上传URL格式不正确") return nil, gerror.NewCode(gcode.CodeInvalidRequest, "上传URL格式不正确")
} }
f := &parser{ f := &parser{
ext: path.Ext(ossFileLink), ext: path.Ext(ossFileLink),
......
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
"image" "image"
"image/jpeg" "image/jpeg"
"image/png" "image/png"
"io/ioutil"
"os" "os"
"path" "path"
"regexp" "regexp"
...@@ -18,6 +17,7 @@ import ( ...@@ -18,6 +17,7 @@ import (
"ppt_server/library/http" "ppt_server/library/http"
"ppt_server/library/oss" "ppt_server/library/oss"
"github.com/chai2010/webp"
"github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g" "github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gtime" "github.com/gogf/gf/os/gtime"
...@@ -45,7 +45,6 @@ const ( ...@@ -45,7 +45,6 @@ const (
var reFileNameCompile, _ = regexp.Compile(`p([\d]+)[.].*`) var reFileNameCompile, _ = regexp.Compile(`p([\d]+)[.].*`)
var reTokenPattern = `<meta name="csrf-token" content="([^"]+)">` var reTokenPattern = `<meta name="csrf-token" content="([^"]+)">`
func Upload(r model.FileUploadRequest, errChan chan error, func Upload(r model.FileUploadRequest, errChan chan error,
ok chan bool) (file *ObtainFile, err error) { ok chan bool) (file *ObtainFile, err error) {
...@@ -103,49 +102,40 @@ func (f *ObtainFile) taskJob(file *zip.File) ([]interface{}, error) { ...@@ -103,49 +102,40 @@ func (f *ObtainFile) taskJob(file *zip.File) ([]interface{}, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer fd.Close() 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 var img image.Image
fileSuffix := path.Ext(file.Name) //获取文件后缀 fileSuffix := path.Ext(file.Name) //获取文件后缀
switch fileSuffix { switch fileSuffix {
case ".png": case ".png":
img, err = png.Decode(imageReader) img, err = png.Decode(fd)
case ".jpeg": case ".jpeg":
img, err = jpeg.Decode(imageReader) img, err = jpeg.Decode(fd)
default: default:
return nil, gerror.New("image ext is not found") return nil, gerror.New("image ext is not found")
} }
if err != nil { if err != nil {
return nil, err 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() info := img.Bounds()
imageInfo := []interface{}{replaceName, info.Max.X, info.Max.Y} 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 { func (f *ObtainFile) task(file *zip.File) (err error) {
var (
err error
imageInfo []interface{}
)
for i := 0; i < 3; i++ { 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) f.packed.ImageInfos = append(f.packed.ImageInfos, imageInfo)
break break
} }
} }
return err return
} }
func (f *ObtainFile) uploadPicture() error { 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