|
|
@ -4,10 +4,7 @@ import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.aliyuncs.AcsRequest; |
|
|
|
import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest; |
|
|
|
import com.aliyuncs.green.model.v20180509.TextScanRequest; |
|
|
|
import com.aliyuncs.green.model.v20180509.VoiceAsyncScanRequest; |
|
|
|
import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest; |
|
|
|
import com.aliyuncs.green.model.v20180509.*; |
|
|
|
import com.aliyuncs.http.FormatType; |
|
|
|
import com.aliyuncs.http.HttpResponse; |
|
|
|
import com.epmet.commons.tools.constant.NumConstant; |
|
|
@ -20,7 +17,10 @@ import com.epmet.openapi.scan.common.exception.ExecuteHttpException; |
|
|
|
import com.epmet.openapi.scan.common.redis.RedisKeys; |
|
|
|
import com.epmet.openapi.scan.common.util.IAcsClientUtil; |
|
|
|
import com.epmet.openapi.scan.support.param.*; |
|
|
|
import com.epmet.openapi.scan.support.param.video.VideoAsyncScanParam; |
|
|
|
import com.epmet.openapi.scan.support.param.video.VideoAsyncScanTask; |
|
|
|
import com.epmet.openapi.scan.support.result.*; |
|
|
|
import com.epmet.openapi.scan.support.result.video.*; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.collections4.ListUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
@ -504,4 +504,231 @@ public class ScanServiceImpl implements ScanService { |
|
|
|
return getResultsRequest; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* desc:视频检测-异步检测 |
|
|
|
* |
|
|
|
* @param videoAsyncScanParam |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public Result<VideoAsyncScanTaskResultDTO> videoAsyncScan(VideoAsyncScanParam videoAsyncScanParam) { |
|
|
|
//一次至多提交100个检测对象
|
|
|
|
List<VideoAsyncScanTask> videoTasks = videoAsyncScanParam.getTasks(); |
|
|
|
if (CollectionUtils.isEmpty(videoTasks) || videoTasks.size() > SysConstant.MAX_TASK_SIZE) { |
|
|
|
return new Result<VideoAsyncScanTaskResultDTO>().error(SysResponseEnum.SCAN_TASK_LIST_PARAM_ERROR.getCode(), SysResponseEnum.SCAN_TASK_LIST_PARAM_ERROR.getMsg().concat(SysConstant.MAX_TASK_SIZE.toString())); |
|
|
|
} |
|
|
|
//默认参数赋值
|
|
|
|
videoAsyncScanParam.setScenes(VideoSceneEnum.getVideoSceneList()); |
|
|
|
videoAsyncScanParam.setBizType(bizType); |
|
|
|
videoAsyncScanParam.setAudioScenes(VoiceSceneEnum.getVoiceSceneList()); |
|
|
|
videoAsyncScanParam.setSeed(UUID.randomUUID().toString().replace("-", "")); |
|
|
|
//API文档没写限制多少最大多少,应该与图片一致
|
|
|
|
if (videoTasks.size() <= SysConstant.MAX_SCAN_IMG_TASK_SIZE) { |
|
|
|
return doScanVideo(videoAsyncScanParam); |
|
|
|
} |
|
|
|
log.info("videoAsyncScan tasks size:{} over 10", videoTasks.size()); |
|
|
|
//分组调用,一次提交10个
|
|
|
|
List<List<VideoAsyncScanTask>> partition = ListUtils.partition(videoTasks, SysConstant.MAX_SCAN_IMG_TASK_SIZE); |
|
|
|
VideoAsyncScanTaskResultDTO finalResult = new VideoAsyncScanTaskResultDTO(); |
|
|
|
for (List<VideoAsyncScanTask> tasks : partition) { |
|
|
|
VideoAsyncScanParam videParam = new VideoAsyncScanParam(); |
|
|
|
videParam.setBizType(videoAsyncScanParam.getBizType()); |
|
|
|
videParam.setScenes(videoAsyncScanParam.getScenes()); |
|
|
|
videParam.setTasks(tasks); |
|
|
|
videParam.setCallback(videoAsyncScanParam.getCallback()); |
|
|
|
videParam.setSeed(videoAsyncScanParam.getSeed()); |
|
|
|
videParam.setAudioScenes(videoAsyncScanParam.getAudioScenes()); |
|
|
|
Result<VideoAsyncScanTaskResultDTO> partResult = doScanVideo(videParam); |
|
|
|
try { |
|
|
|
Thread.sleep(5L); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
log.error("videoAsyncScan InterruptedException"); |
|
|
|
} |
|
|
|
if (partResult.success()) { |
|
|
|
VideoAsyncScanTaskResultDTO data = partResult.getData(); |
|
|
|
finalResult.setSeed(data.getSeed()); |
|
|
|
finalResult.getSuccessTasks().addAll(data.getSuccessTasks()); |
|
|
|
finalResult.getFailTasks().addAll(data.getFailTasks()); |
|
|
|
} else { |
|
|
|
return partResult; |
|
|
|
} |
|
|
|
} |
|
|
|
return new Result<VideoAsyncScanTaskResultDTO>().ok(finalResult); |
|
|
|
} |
|
|
|
|
|
|
|
private Result<VideoAsyncScanTaskResultDTO> doScanVideo(VideoAsyncScanParam videoAsyncScanParam) { |
|
|
|
VideoAsyncScanRequest videoAsyncScanRequest = getVideoAsyncScanRequest(); |
|
|
|
try { |
|
|
|
videoAsyncScanRequest.setHttpContent(JSON.toJSONString(videoAsyncScanParam).getBytes(SysConstant.UTF8), SysConstant.UTF8, FormatType.JSON); |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
log.error("doScanVideo parse param exception", e); |
|
|
|
return new Result<VideoAsyncScanTaskResultDTO>().error(SysResponseEnum.SCAN_PARAM_ERROR.getCode(), SysResponseEnum.SCAN_PARAM_ERROR.getMsg()); |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
VideoAsyncScanTaskResultDTO scanResult = executeAsyncVideo(videoAsyncScanRequest); |
|
|
|
scanResult.setSeed(videoAsyncScanParam.getSeed()); |
|
|
|
return new Result<VideoAsyncScanTaskResultDTO>().ok(scanResult); |
|
|
|
} catch (ExecuteHttpException e) { |
|
|
|
log.error("doScanVideo execute exception,param:{},fail msg:{}", JSON.toJSONString(videoAsyncScanParam), e.getMsg()); |
|
|
|
return new Result<VideoAsyncScanTaskResultDTO>().error(e.getCode(), e.getMsg()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private VideoAsyncScanRequest getVideoAsyncScanRequest() { |
|
|
|
VideoAsyncScanRequest videoAsyncScanRequest = new VideoAsyncScanRequest(); |
|
|
|
videoAsyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
|
|
|
|
videoAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
|
|
|
|
/** |
|
|
|
* 请务必设置超时时间。 |
|
|
|
*/ |
|
|
|
videoAsyncScanRequest.setConnectTimeout(3000); |
|
|
|
videoAsyncScanRequest.setReadTimeout(6000); |
|
|
|
return videoAsyncScanRequest; |
|
|
|
} |
|
|
|
|
|
|
|
private VideoAsyncScanTaskResultDTO executeAsyncVideo(VideoAsyncScanRequest videoAsyncScanRequest) { |
|
|
|
try { |
|
|
|
HttpResponse httpResponse = IAcsClientUtil.getIAcsClient().doAction(videoAsyncScanRequest); |
|
|
|
if (httpResponse.isSuccess()) { |
|
|
|
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); |
|
|
|
if (HttpStatus.SC_OK == scrResponse.getInteger(SysConstant.CODE)) { |
|
|
|
//获取data列表
|
|
|
|
JSONArray dataResults = scrResponse.getJSONArray(SysConstant.DATA); |
|
|
|
List<VideoAsyncScanTaskDataDTO> dataList = dataResults.toJavaList(VideoAsyncScanTaskDataDTO.class); |
|
|
|
VideoAsyncScanTaskResultDTO result=new VideoAsyncScanTaskResultDTO(); |
|
|
|
dataList.forEach(data->{ |
|
|
|
if (HttpStatus.SC_OK == data.getCode()) { |
|
|
|
result.getSuccessTasks().add(data); |
|
|
|
} else { |
|
|
|
result.getFailTasks().add(data); |
|
|
|
} |
|
|
|
}); |
|
|
|
return result; |
|
|
|
} else { |
|
|
|
log.warn("executeAsyncVideo detect not success. code:{}", scrResponse.getInteger(SysConstant.CODE)); |
|
|
|
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getCode(), |
|
|
|
SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getMsg() + ",status:" + httpResponse.getStatus()); |
|
|
|
} |
|
|
|
} else { |
|
|
|
log.warn("executeAsyncVideo response status is not success. httpResponse:{}", JSON.toJSONString(httpResponse)); |
|
|
|
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getCode(), |
|
|
|
SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getMsg() + ",status:" + httpResponse.getStatus()); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("executeAsyncVideo exception IAcsClientUtil do action exception", e); |
|
|
|
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getCode(), SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getMsg()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param taskIds |
|
|
|
* @author yinzuomei |
|
|
|
* @description 视频异步检测结果查询接口 |
|
|
|
* @Date 2020/12/29 16:10 |
|
|
|
**/ |
|
|
|
@Override |
|
|
|
public Result<VideoResultDTO> videoResults(List<String> taskIds) { |
|
|
|
VideoAsyncScanResultsRequest videoAsyncScanResultsRequest = new VideoAsyncScanResultsRequest(); |
|
|
|
videoAsyncScanResultsRequest.setAcceptFormat(FormatType.JSON); |
|
|
|
videoAsyncScanResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST); |
|
|
|
videoAsyncScanResultsRequest.setConnectTimeout(3000); |
|
|
|
videoAsyncScanResultsRequest.setReadTimeout(6000); |
|
|
|
try { |
|
|
|
videoAsyncScanResultsRequest.setHttpContent(JSON.toJSONString(taskIds).getBytes("UTF-8"), "UTF-8", FormatType.JSON); |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
log.error("videoResults parse param exception", e); |
|
|
|
return new Result<VideoResultDTO>().error(SysResponseEnum.SCAN_PARAM_ERROR.getCode(), SysResponseEnum.SCAN_PARAM_ERROR.getMsg()); |
|
|
|
} |
|
|
|
try { |
|
|
|
HttpResponse httpResponse = IAcsClientUtil.getIAcsClient().doAction(videoAsyncScanResultsRequest); |
|
|
|
if (httpResponse.isSuccess()) { |
|
|
|
JSONObject responseObject = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); |
|
|
|
log.info("查询视频检测结果返参"+JSON.toJSONString(responseObject)); |
|
|
|
if (HttpStatus.SC_OK == responseObject.getInteger(SysConstant.CODE)) { |
|
|
|
//获取data列表
|
|
|
|
JSONArray dataResults = responseObject.getJSONArray(SysConstant.DATA); |
|
|
|
List<VideoScanOriginalResultDTO> resultList = dataResults.toJavaList(VideoScanOriginalResultDTO.class); |
|
|
|
//解析数据
|
|
|
|
VideoResultDTO resultDTO = processVideoResults(resultList); |
|
|
|
//成功返回
|
|
|
|
return new Result<VideoResultDTO>().ok(resultDTO); |
|
|
|
} else { |
|
|
|
log.warn("查询视频检测结果,接口返回code=" + responseObject.getInteger(SysConstant.CODE)); |
|
|
|
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getCode(), |
|
|
|
SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getMsg() + ",status:" + httpResponse.getStatus()); |
|
|
|
} |
|
|
|
} else { |
|
|
|
log.warn("查询视频检测结果,API返回失败"); |
|
|
|
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getCode(), |
|
|
|
SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getMsg() + ",status:" + httpResponse.getStatus()); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("videoResults exception ", e); |
|
|
|
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getCode(), SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getMsg()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @author yinzuomei |
|
|
|
* @description |
|
|
|
**/ |
|
|
|
private VideoResultDTO processVideoResults(List<VideoScanOriginalResultDTO> resultList) { |
|
|
|
VideoResultDTO videoResultDTO = new VideoResultDTO(); |
|
|
|
resultList.forEach(result -> { |
|
|
|
result.setCodeDesc(CommonErrorCodeEnum.getErrorMsg(result.getCode())); |
|
|
|
if (result.getCode().equals(CommonErrorCodeEnum.PROCESSING.getCode())) { |
|
|
|
//任务正在检测中,继续轮询
|
|
|
|
} else if (result.getCode().equals(CommonErrorCodeEnum.OK.getCode())) { |
|
|
|
//成功=>分析结果
|
|
|
|
boolean videoPassFlag = getVideoFlag(result.getResults()); |
|
|
|
boolean voicePassFlag = getVoiceFlag(result.getAudioScanResults()); |
|
|
|
if (videoPassFlag && voicePassFlag) { |
|
|
|
videoResultDTO.getPassDataIds().add(result.getDataId()); |
|
|
|
videoResultDTO.getPassTaskIds().add(result.getTaskId()); |
|
|
|
} else { |
|
|
|
videoResultDTO.getNoPassDataIds().add(result.getDataId()); |
|
|
|
videoResultDTO.getNoPassTaskIds().add(result.getTaskId()); |
|
|
|
} |
|
|
|
} else { |
|
|
|
//检测结果走丢了.... (*^▽^*) 默认失败
|
|
|
|
videoResultDTO.getNoPassDataIds().add(result.getDataId()); |
|
|
|
videoResultDTO.getNoPassTaskIds().add(result.getTaskId()); |
|
|
|
} |
|
|
|
}); |
|
|
|
videoResultDTO.setDetails(resultList); |
|
|
|
return videoResultDTO; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @return boolean |
|
|
|
* @author yinzuomei |
|
|
|
* @description 视频检测结果判断 |
|
|
|
**/ |
|
|
|
private boolean getVideoFlag(List<VideoScanOriginDetail> results) { |
|
|
|
for(VideoScanOriginDetail videoRes:results){ |
|
|
|
if (!SuggestionEnum.PASS.getCode().equals(videoRes.getSuggestion())) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @return boolean true:内容通过; 建议为内容违规或者需要人工审核的统一视为不通过,返回false |
|
|
|
* @author yinzuomei |
|
|
|
* @description 返回视频语音检测结果 |
|
|
|
**/ |
|
|
|
private boolean getVoiceFlag(List<VoiceAsyncScanResultDTO> audioScanResults) { |
|
|
|
if (CollectionUtils.isEmpty(audioScanResults)) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
for(VoiceAsyncScanResultDTO m:audioScanResults){ |
|
|
|
//人工审核或者内容违规,统一视为不通过
|
|
|
|
if (!SuggestionEnum.PASS.getCode().equals(m.getSuggestion())) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|