From 53f911104651335a81cfb532612adb95ba6e6701 Mon Sep 17 00:00:00 2001 From: wangjian Date: Thu, 18 May 2023 10:59:09 +0800 Subject: [PATCH] =?UTF-8?q?bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/service/index.go | 24 ++++++++++++++---------- pkg/utils/image.go | 19 ++++++++++++++++++- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/internal/service/index.go b/internal/service/index.go index 4b6774d..5c5d02a 100644 --- a/internal/service/index.go +++ b/internal/service/index.go @@ -72,17 +72,19 @@ func (rp *repo) GetList(ctx context.Context, req proto.ListRequest) (rsp *proto. if err != nil { goto ReturnPoint } - list := make([]proto.FileBaseInfo, len(files)) - for k, v := range files { + list := make([]proto.FileBaseInfo, 0) + for _, v := range files { info, _ := v.Info() - item := proto.FileBaseInfo{ - Name: v.Name(), - Path: path.Join(req.Path, v.Name()), - IsDir: v.IsDir(), - Size: info.Size(), - ModTime: info.ModTime().Unix(), + if utils.IsImage(v.Name()) || v.IsDir() { + item := proto.FileBaseInfo{ + Name: v.Name(), + Path: path.Join(req.Path, v.Name()), + IsDir: v.IsDir(), + Size: info.Size(), + ModTime: info.ModTime().Unix(), + } + list = append(list, item) } - list[k] = item } rsp.Code = http.StatusOK @@ -127,13 +129,15 @@ func (rp *repo) GetInfo(ctx context.Context, req proto.ListRequest) (rsp *proto. goto ReturnPoint } buff := utils.ReadFile(req.Path) + img := utils.BuffToImage(buff) + buf := utils.ImageToBuff(img, "jpeg") res := new(proto.FileContent) res.Name = fileInfo.Name() res.IsDir = false res.Path = req.Path res.Size = fileInfo.Size() res.ModTime = fileInfo.ModTime().Unix() - res.ContentBase = base64.StdEncoding.EncodeToString(buff) + res.ContentBase = "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(buf.Bytes()) b, ok := global.FileLabelList[req.Path] if ok { if b { diff --git a/pkg/utils/image.go b/pkg/utils/image.go index 5c7975c..3dd501e 100644 --- a/pkg/utils/image.go +++ b/pkg/utils/image.go @@ -12,6 +12,8 @@ import ( "image/jpeg" "image/png" "math" + "path" + "strings" ) func BuffToImage(in []byte) image.Image { @@ -153,7 +155,7 @@ func ImageToBuff(img image.Image, imgType string) *bytes.Buffer { case "png": imgType = "png" _ = png.Encode(buff, img) - case "tiff": + case "tiff", "tif": imgType = "tiff" _ = tiff.Encode(buff, img, nil) default: @@ -214,3 +216,18 @@ func width(i image.Image) int { func height(i image.Image) int { return i.Bounds().Max.Y - i.Bounds().Min.Y } + +func IsImage(fileName string) bool { + fileType := path.Ext(fileName) + return InSlice([]string{".png", ".jpg", ".jpeg", ".bmp", ".tif", ".tiff"}, strings.ToLower(fileType)) +} + +// InSlice 判断字符串是否在 slice 中。 +func InSlice(items []string, item string) bool { + for _, eachItem := range items { + if eachItem == item { + return true + } + } + return false +}