annotation/mq/handler.go

104 lines
2.1 KiB
Go
Raw Normal View History

2023-05-12 16:53:21 +08:00
package mq
import (
"encoding/base64"
"fmt"
"hpds_annotation/global"
"hpds_annotation/internal/proto"
"hpds_annotation/pkg/utils"
"net/http"
"os"
"path"
)
func GetList(req proto.ListRequest) (rsp *proto.BaseResponse, err error) {
rsp = new(proto.BaseResponse)
var (
files []os.DirEntry
list []proto.FileBaseInfo
)
files, err = os.ReadDir(req.Path)
if err != nil {
goto ReturnPoint
}
list = make([]proto.FileBaseInfo, len(files))
for k, 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(),
}
list[k] = item
}
rsp.Code = http.StatusOK
rsp.Status = http.StatusText(http.StatusOK)
rsp.Message = "成功"
rsp.Data = list
rsp.Err = err
return rsp, err
ReturnPoint:
if err != nil {
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Err = err
rsp.Message = "失败"
}
return rsp, err
}
func GetFileInfo(req proto.ListRequest) (rsp *proto.BaseResponse, err error) {
rsp = new(proto.BaseResponse)
var (
fileInfo os.FileInfo
b, ok bool
buff []byte
)
res := new(proto.FileContent)
fileInfo, err = os.Stat(req.Path)
if err != nil {
goto ReturnPoint
}
if fileInfo.IsDir() {
err = fmt.Errorf("不能获取文件夹的详细信息")
goto ReturnPoint
}
buff = utils.ReadFile(req.Path)
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)
b, ok = global.FileLabelList[req.Path]
if ok {
if b {
res.LabelStatus = 1
} else {
res.LabelStatus = 2
}
} else {
res.LabelStatus = 0
}
rsp.Code = http.StatusOK
rsp.Status = http.StatusText(http.StatusOK)
rsp.Message = "成功"
rsp.Data = res
rsp.Err = err
return rsp, err
ReturnPoint:
if err != nil {
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Err = err
rsp.Message = "失败"
}
return rsp, err
}