bug修复

This commit is contained in:
wangjian 2023-05-18 10:59:09 +08:00
parent 15917e926e
commit 53f9111046
2 changed files with 32 additions and 11 deletions

View File

@ -72,17 +72,19 @@ func (rp *repo) GetList(ctx context.Context, req proto.ListRequest) (rsp *proto.
if err != nil { if err != nil {
goto ReturnPoint goto ReturnPoint
} }
list := make([]proto.FileBaseInfo, len(files)) list := make([]proto.FileBaseInfo, 0)
for k, v := range files { for _, v := range files {
info, _ := v.Info() info, _ := v.Info()
item := proto.FileBaseInfo{ if utils.IsImage(v.Name()) || v.IsDir() {
Name: v.Name(), item := proto.FileBaseInfo{
Path: path.Join(req.Path, v.Name()), Name: v.Name(),
IsDir: v.IsDir(), Path: path.Join(req.Path, v.Name()),
Size: info.Size(), IsDir: v.IsDir(),
ModTime: info.ModTime().Unix(), Size: info.Size(),
ModTime: info.ModTime().Unix(),
}
list = append(list, item)
} }
list[k] = item
} }
rsp.Code = http.StatusOK rsp.Code = http.StatusOK
@ -127,13 +129,15 @@ func (rp *repo) GetInfo(ctx context.Context, req proto.ListRequest) (rsp *proto.
goto ReturnPoint goto ReturnPoint
} }
buff := utils.ReadFile(req.Path) buff := utils.ReadFile(req.Path)
img := utils.BuffToImage(buff)
buf := utils.ImageToBuff(img, "jpeg")
res := new(proto.FileContent) res := new(proto.FileContent)
res.Name = fileInfo.Name() res.Name = fileInfo.Name()
res.IsDir = false res.IsDir = false
res.Path = req.Path res.Path = req.Path
res.Size = fileInfo.Size() res.Size = fileInfo.Size()
res.ModTime = fileInfo.ModTime().Unix() 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] b, ok := global.FileLabelList[req.Path]
if ok { if ok {
if b { if b {

View File

@ -12,6 +12,8 @@ import (
"image/jpeg" "image/jpeg"
"image/png" "image/png"
"math" "math"
"path"
"strings"
) )
func BuffToImage(in []byte) image.Image { func BuffToImage(in []byte) image.Image {
@ -153,7 +155,7 @@ func ImageToBuff(img image.Image, imgType string) *bytes.Buffer {
case "png": case "png":
imgType = "png" imgType = "png"
_ = png.Encode(buff, img) _ = png.Encode(buff, img)
case "tiff": case "tiff", "tif":
imgType = "tiff" imgType = "tiff"
_ = tiff.Encode(buff, img, nil) _ = tiff.Encode(buff, img, nil)
default: default:
@ -214,3 +216,18 @@ func width(i image.Image) int {
func height(i image.Image) int { func height(i image.Image) int {
return i.Bounds().Max.Y - i.Bounds().Min.Y 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
}