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" "sort" ) 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) CaptureSubmit(ctx context.Context, req proto.CaptureRequest) (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 count int ) files, err = os.ReadDir(req.Path) if err != nil { goto ReturnPoint } 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()), IsDir: v.IsDir(), Size: info.Size(), ModTime: info.ModTime().Unix(), } list = append(list, item) } } // 从小到大排序(稳定排序) sort.SliceStable(list, func(i, j int) bool { if list[i].Name < list[j].Name { return true } return false }) count = len(list) if len(list) >= int(req.Page*req.Size) { list = list[int((req.Page-1)*req.Size):int(req.Page*req.Size)] } else { list = list[int((req.Page-1)*req.Size):] } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "成功" rsp = FillPaging(int64(count), req.Page, req.Size, list, rsp) 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) 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 = "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(buf.Bytes()) 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, BizType: req.BizType, } } 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 } } func (rp *repo) CaptureSubmit(ctx context.Context, req proto.CaptureRequest) (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.FileTransferInfo, len(req.FileList)) var ( fileInfo os.FileInfo ) for k, v := range req.FileList { fileInfo, err = os.Stat(v) if err != nil { goto ReturnPoint } buff := utils.ReadFile(v) dstContent := store.Compress(buff) list[k] = proto.FileTransferInfo{ FileName: fileInfo.Name(), FilePath: v, DatasetName: req.DatasetName, DataType: req.BizType, FileSize: fileInfo.Size(), File: base64.StdEncoding.EncodeToString(dstContent), IsCompress: true, FileMd5: utils.GetFileMd5(buff), } } go mq.SubmitFileData(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 }