221 lines
5.3 KiB
Go
221 lines
5.3 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"git.hpds.cc/Component/logging"
|
|
"hpds_annotation/config"
|
|
"hpds_annotation/global"
|
|
"hpds_annotation/internal/proto"
|
|
"hpds_annotation/mq"
|
|
"hpds_annotation/pkg/utils"
|
|
"hpds_annotation/store"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
type PagingStruct struct {
|
|
List interface{} `json:"list"`
|
|
Total int64 `json:"total"`
|
|
}
|
|
|
|
// FillPaging 填充分页数据
|
|
func FillPaging(count, pageNum, pageSize int64, list interface{}, data *proto.BaseResponse) *proto.BaseResponse {
|
|
_ = fmt.Sprintf("%d, %d", pageNum, pageSize)
|
|
ps := new(PagingStruct)
|
|
ps.List = list
|
|
ps.Total = count
|
|
data.Data = ps
|
|
//data.PageSize = pageSize
|
|
//data.Data = list
|
|
//data.Page = pageNum
|
|
//data.PageCount = tp
|
|
//data.TotalSize = count
|
|
return data
|
|
}
|
|
|
|
type repo struct {
|
|
AppConfig *config.WebConfig
|
|
logger *logging.Logger
|
|
}
|
|
|
|
type EdgeService interface {
|
|
GetList(ctx context.Context, req proto.ListRequest) (rsp *proto.BaseResponse, err error)
|
|
GetInfo(ctx context.Context, req proto.ListRequest) (rsp *proto.BaseResponse, err error)
|
|
LabelSubmit(ctx context.Context, req proto.LabelRequest) (rsp *proto.BaseResponse, err error)
|
|
}
|
|
|
|
func NewEdgeService(logger *logging.Logger) EdgeService {
|
|
return &repo{
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (rp *repo) GetList(ctx context.Context, req proto.ListRequest) (rsp *proto.BaseResponse, err error) {
|
|
rsp = new(proto.BaseResponse)
|
|
select {
|
|
case <-ctx.Done():
|
|
err = fmt.Errorf("超时/取消")
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Message = "超时/取消"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, ctx.Err()
|
|
default:
|
|
if req.IsIncludeSubdirectories {
|
|
|
|
} else {
|
|
var files []os.DirEntry
|
|
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 (rp *repo) GetInfo(ctx context.Context, req proto.ListRequest) (rsp *proto.BaseResponse, err error) {
|
|
rsp = new(proto.BaseResponse)
|
|
select {
|
|
case <-ctx.Done():
|
|
err = fmt.Errorf("超时/取消")
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Message = "超时/取消"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, ctx.Err()
|
|
default:
|
|
var (
|
|
fileInfo os.FileInfo
|
|
)
|
|
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 := 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)
|
|
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
|
|
}
|
|
|
|
func (rp *repo) LabelSubmit(ctx context.Context, req proto.LabelRequest) (rsp *proto.BaseResponse, err error) {
|
|
rsp = new(proto.BaseResponse)
|
|
select {
|
|
case <-ctx.Done():
|
|
err = fmt.Errorf("超时/取消")
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Message = "超时/取消"
|
|
rsp.Err = ctx.Err()
|
|
return rsp, ctx.Err()
|
|
default:
|
|
list := make([]proto.FileContent, len(req.FileList))
|
|
var (
|
|
fileInfo os.FileInfo
|
|
)
|
|
for k, v := range req.FileList {
|
|
global.FileLabelList[v] = req.LabelStatus
|
|
fileInfo, err = os.Stat(v)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
buff := utils.ReadFile(v)
|
|
status := 0
|
|
if req.LabelStatus {
|
|
status = 1
|
|
} else {
|
|
status = 2
|
|
}
|
|
dstContent := store.Compress(buff)
|
|
list[k] = proto.FileContent{
|
|
FileBaseInfo: proto.FileBaseInfo{
|
|
Name: fileInfo.Name(),
|
|
Path: v,
|
|
Size: fileInfo.Size(),
|
|
ModTime: fileInfo.ModTime().Unix(),
|
|
},
|
|
ContentBase: base64.StdEncoding.EncodeToString(dstContent),
|
|
LabelStatus: status,
|
|
}
|
|
}
|
|
go mq.SendLabelData(list, rp.logger)
|
|
rsp.Code = http.StatusOK
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
rsp.Message = "成功"
|
|
rsp.Err = err
|
|
ReturnPoint:
|
|
if err != nil {
|
|
rsp.Code = http.StatusInternalServerError
|
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
|
rsp.Err = err
|
|
rsp.Message = "失败"
|
|
}
|
|
return rsp, err
|
|
}
|
|
}
|