yidun送检功能
This commit is contained in:
@@ -92,6 +92,79 @@ func (s *ImageDetectionService) DetectImage(ctx context.Context, imageURL, dataI
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// DetectImageSync 同步检测图片,提交并直接返回检测结果
|
||||
// 适用于轮询模式(无回调),提交时实时返回结果,无需额外查询
|
||||
func (s *ImageDetectionService) DetectImageSync(ctx context.Context, imageURL, dataID string) (*ImageResult, error) {
|
||||
if DefaultClients == nil || DefaultClients.ImageClient == nil {
|
||||
return nil, fmt.Errorf("易盾图片检测客户端未初始化")
|
||||
}
|
||||
|
||||
if imageURL == "" {
|
||||
return nil, fmt.Errorf("图片URL不能为空")
|
||||
}
|
||||
|
||||
businessId := g.Cfg().MustGet(ctx, "yidun.image.business_id").String()
|
||||
g.Log().Infof(ctx, "图片同步检测, url: %s, business_id: %s", imageURL, businessId)
|
||||
|
||||
// 创建同步检测请求
|
||||
request := check.NewImageV5CheckRequest(businessId)
|
||||
imageBean := check.NewImageBeanRequest()
|
||||
imageBean.SetData(imageURL)
|
||||
imageBean.SetName(dataID)
|
||||
imageBean.SetType(1) // 1: 图片URL
|
||||
request.SetImages([]check.ImageBeanRequest{*imageBean})
|
||||
|
||||
// 调用同步检测API
|
||||
response, err := DefaultClients.ImageClient.ImageSyncCheck(request)
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "图片同步检测失败: %v", err)
|
||||
return nil, fmt.Errorf("图片同步检测失败: %w", err)
|
||||
}
|
||||
|
||||
if response.GetCode() != 200 {
|
||||
g.Log().Errorf(ctx, "图片同步检测API错误: code=%d, msg=%s", response.GetCode(), response.GetMsg())
|
||||
return nil, fmt.Errorf("图片同步检测API错误: code=%d, msg=%s", response.GetCode(), response.GetMsg())
|
||||
}
|
||||
|
||||
if response.Result == nil || len(*response.Result) == 0 {
|
||||
g.Log().Warningf(ctx, "图片同步检测结果为空, url: %s", imageURL)
|
||||
return nil, ErrImageResultNotFound
|
||||
}
|
||||
|
||||
// 提取结果
|
||||
detail := (*response.Result)[0]
|
||||
result := &ImageResult{
|
||||
TaskID: dataID,
|
||||
Name: dataID,
|
||||
Url: imageURL,
|
||||
}
|
||||
if detail.Antispam != nil {
|
||||
if detail.Antispam.TaskId != nil {
|
||||
result.TaskID = *detail.Antispam.TaskId
|
||||
}
|
||||
if detail.Antispam.Status != nil {
|
||||
result.Status = *detail.Antispam.Status
|
||||
}
|
||||
if detail.Antispam.Suggestion != nil {
|
||||
result.Suggestion = *detail.Antispam.Suggestion
|
||||
}
|
||||
if detail.Antispam.Label != nil {
|
||||
result.Label = *detail.Antispam.Label
|
||||
}
|
||||
if detail.Antispam.ResultType != nil {
|
||||
result.ResultType = *detail.Antispam.ResultType
|
||||
}
|
||||
if detail.Antispam.CensorTime != nil {
|
||||
result.CensorTime = *detail.Antispam.CensorTime
|
||||
}
|
||||
result.Antispam = detail.Antispam
|
||||
}
|
||||
|
||||
g.Log().Infof(ctx, "图片同步检测完成, taskID: %s, suggestion: %d, status: %d",
|
||||
result.TaskID, result.Suggestion, result.Status)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ImageResult 图片检测完整结果
|
||||
type ImageResult struct {
|
||||
TaskID string `json:"taskId"` // 任务ID
|
||||
@@ -144,7 +217,7 @@ func (s *ImageDetectionService) GetImageResult(ctx context.Context, taskID strin
|
||||
|
||||
if response.Result == nil || len(*response.Result) == 0 {
|
||||
g.Log().Warningf(ctx, "未找到图片检测结果, taskID: %s", taskID)
|
||||
return nil, ErrImageStillProcessing
|
||||
return nil, ErrImageResultNotFound
|
||||
}
|
||||
|
||||
// 查找指定taskID的结果
|
||||
|
||||
Reference in New Issue
Block a user