pcm-ac/internal/logic/submitImageInferTaskLogic.go

71 lines
1.8 KiB
Go

package logic
import (
"context"
"errors"
"fmt"
"gitlink.org.cn/JointCloud/pcm-ac/hpcAC"
"gitlink.org.cn/JointCloud/pcm-ac/internal/common"
"gitlink.org.cn/JointCloud/pcm-ac/internal/svc"
"log"
"os"
"github.com/zeromicro/go-zero/core/logx"
)
type SubmitImageInferTaskLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewSubmitImageInferTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SubmitImageInferTaskLogic {
return &SubmitImageInferTaskLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *SubmitImageInferTaskLogic) SubmitImageInferTask(in *hpcAC.SubmitImageInferTaskReq) (*hpcAC.SubmitImageInferTaskResp, error) {
type imageInferResult struct {
Result string `json:"result"`
}
result := &imageInferResult{}
resp := &hpcAC.SubmitImageInferTaskResp{}
detailResp := &hpcAC.GetInstanceDetailResp{}
var detailUrl = common.AiCenterUrlPrefix() + l.svcCtx.Config.ContainerConf.GetInstanceAi + "/" + in.Id + "/detail"
token := common.GetToken()
if token == "" {
log.Println("获取token失败")
return nil, errors.New("获取token失败")
}
//调用详情接口获取accessUrl
reqDetail := common.GetRestyRequest(3)
_, _ = reqDetail.SetHeader("token", token).
SetResult(detailResp).
Get(detailUrl)
// 读取文件数据
file, err := os.Open(in.Path)
if err != nil {
fmt.Println("Error opening file:", err)
return nil, nil
}
defer file.Close()
reqInfer := common.GetRestyRequest(3)
resultTest, _ := reqInfer.
SetHeader("Content-Type", "application/octet-stream").
SetFileReader("file", "dog.jpg", file).
SetResult(result).
Post(detailResp.Data.ContainerPortInfoList[0].AccessUrl + "/image")
fmt.Printf("", resultTest)
resp.Code = "200"
resp.Result = result.Result
resp.Message = "success"
return resp, nil
}