Commit d8714078 authored by 刘基明's avatar 刘基明

内容检验fix

parent e38879ab
......@@ -6,6 +6,7 @@ import com.tanpu.biz.common.enums.RelTypeEnum;
import com.tanpu.biz.common.enums.community.CollectionTypeEnum;
import com.tanpu.biz.common.enums.community.ReportTypeEnum;
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.constant.ErrorCodeConstant;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.community.api.beans.qo.ESThemeQo;
......@@ -18,7 +19,10 @@ import com.tanpu.community.api.beans.resp.CreateThemeResp;
import com.tanpu.community.api.beans.resp.ThemeFullSearchResp;
import com.tanpu.community.api.beans.resp.ThemeListResp;
import com.tanpu.community.api.beans.vo.feign.fatools.UserInfoResp;
import com.tanpu.community.api.enums.*;
import com.tanpu.community.api.enums.BlockTypeEnum;
import com.tanpu.community.api.enums.OperationTypeEnum;
import com.tanpu.community.api.enums.ThemeListTypeEnum;
import com.tanpu.community.api.enums.ThemeTypeEnum;
import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.dao.entity.community.*;
import com.tanpu.community.feign.fatools.FeignClientForFatools;
......@@ -39,7 +43,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
......@@ -144,12 +147,9 @@ public class ThemeManager {
ThemeEntity themeEntity = new ThemeEntity();
BeanUtils.copyProperties(req, themeEntity);
themeEntity.setAuthorId(userId);
themeEntity.setContent(JsonUtil.toJson(req.getContent()));
// 腾讯云敏感词校验
// TODO 图片校验
checkContent(themeEntity.getContent());
checkContent(req);
themeEntity.setContent(JsonUtil.toJson(req.getContent()));
//附件校验
checkAttachment(req.getContent());
......@@ -493,22 +493,28 @@ public class ThemeManager {
/**
* 腾讯云-内容检测
*
* @param content
* @param req
*/
private void checkContent(String content) {
private void checkContent(CreateThemeReq req) {
StringBuilder sb = new StringBuilder();
for (ThemeContentReq themeContentReq : req.getContent()) {
if (RelTypeEnum.TEXT.type.equals(themeContentReq.getType())){
sb.append(themeContentReq.getValue());
}
}
String content = sb.toString();
// 腾讯云接口最多支持5000文字校验,超过5000执行2次
// 检查内容是否涉黄违法
CommonResp commonResp = TencentcloudUtils.textModeration(content.length() > 5000 ? content.substring(0, 5000) : content);
if (!commonResp.isSuccess()) {
throw new BizException("内容校验失败,请检查内容后重新发送");
boolean b = TencentcloudUtils.textModeration(content.length() > 5000 ? content.substring(0, 5000) : content);
if (!b) {
throw new BizException(ErrorCodeConstant.CONTENT_ILLEGAL);
}
if (content.length() > 5000) {
CommonResp commonResp2 = TencentcloudUtils.textModeration(content.substring(5000, content.length()));
if (!commonResp2.isSuccess()) {
throw new BizException("内容校验失败,请检查内容后重新发送");
boolean result = TencentcloudUtils.textModeration(content.substring(5000, content.length()));
if (!result) {
throw new BizException(ErrorCodeConstant.CONTENT_ILLEGAL);
}
}
return;
}
......
......@@ -66,10 +66,7 @@ public class ConvertUtil {
// 抽取文本内容
List<ThemeContentQo> themeContentQos = JsonUtil.toBean(entity.getContent(), new TypeReference<List<ThemeContentQo>>() {});
StringBuilder sb = new StringBuilder();
themeContentQos.stream().filter(q -> {
// todo enum
return q.getType().equals("108");
}).forEach(q -> {
themeContentQos.stream().filter(q -> RelTypeEnum.TEXT.type.equals(q.getType())).forEach(q -> {
sb.append(q.getValue());
});
qo.textContent = sb.toString();
......
......@@ -79,7 +79,7 @@ public class TencentcloudUtils {
* @param text
* @return
*/
public static CommonResp textModeration(String text) {
public static boolean textModeration(String text) {
TextModerationRequest req = new TextModerationRequest();
req.setContent(Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8)));
TextModerationResponse res = null;
......@@ -87,16 +87,16 @@ public class TencentcloudUtils {
res = getTmsClient().TextModeration(req);
// suggestion Block 不通过
if (res.getSuggestion().equals("Block")) {
// TODO: 2021/7/20
return CommonResp.error();
return false;
// return CommonResp.error(CommonResp.CONTENT_ILLEGAL, getTextLabel(res.getLabel(), res.getKeywords()));
}
} catch (TencentCloudSDKException e) {
log.error("调用腾文本内容安全异常");
e.printStackTrace();
return CommonResp.failed("文本检查异常");
//调用失败时,不影响用户发布主题
return true;
}
return CommonResp.success();
return true;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment