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,9 +72,10 @@ 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()
if utils.IsImage(v.Name()) || v.IsDir() {
item := proto.FileBaseInfo{
Name: v.Name(),
Path: path.Join(req.Path, v.Name()),
@ -82,7 +83,8 @@ func (rp *repo) GetList(ctx context.Context, req proto.ListRequest) (rsp *proto.
Size: info.Size(),
ModTime: info.ModTime().Unix(),
}
list[k] = item
list = append(list, 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 {

View File

@ -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
}