package com.tanpu.community.util; import com.tanpu.common.api.*; import com.tanpu.common.constant.ErrorCodeConstant; import com.tencentcloudapi.cms.v20190321.CmsClient; import com.tencentcloudapi.cms.v20190321.models.ImageModerationRequest; import com.tencentcloudapi.cms.v20190321.models.ImageModerationResponse; import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.exception.TencentCloudSDKException; import com.tencentcloudapi.tms.v20200713.TmsClient; import com.tencentcloudapi.tms.v20200713.models.TextModerationRequest; import com.tencentcloudapi.tms.v20200713.models.TextModerationResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Base64; /** * 腾讯云工具类 */ @Slf4j @Configuration public class TencentcloudUtils { /** * 腾讯云的SecretId */ private static String secretId; /** * 腾讯云的SecretKey */ private static String secretKey; /** * 腾讯云地区 */ private static String region; private static TmsClient tmsClient; private static CmsClient cmsClient; @Value("${tencent.cloud.secretId}") public void setSecretId(String secretId) { TencentcloudUtils.secretId = secretId; } @Value("${tencent.cloud.secretKey}") public void setSecretKey(String secretKey) { TencentcloudUtils.secretKey = secretKey; } @Value("${tencent.cloud.region}") public void setRegion(String region) { TencentcloudUtils.region = region; } public static TmsClient getTmsClient() { if (tmsClient == null) { tmsClient = new TmsClient(new Credential(secretId, secretKey), region); } return tmsClient; } public static CmsClient getCmsClient() { if (cmsClient == null) { cmsClient = new CmsClient(new Credential(secretId, secretKey), region); } return cmsClient; } /** * 文本内容安全 * * @param text * @return */ public static boolean textModeration(String text) { TextModerationRequest req = new TextModerationRequest(); req.setContent(Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8))); TextModerationResponse res = null; try { res = getTmsClient().TextModeration(req); // suggestion Block 不通过 if (res.getSuggestion().equals("Block")) { log.error("文本检验违规关键字:"+res.getKeywords()); return false; } } catch (TencentCloudSDKException e) { log.error("调用腾文本内容安全异常"); e.printStackTrace(); //调用失败时,不影响用户发布主题 return true; } return true; } /** * 图片内容检测 * * @param imageUrl * @return */ public static CommonResp imageModeration(String imageUrl) { ImageModerationRequest req = new ImageModerationRequest(); req.setFileUrl(imageUrl); ImageModerationResponse res = null; try { res = getCmsClient().ImageModeration(req); if (res.getData() != null && res.getData().getEvilType() != 100) { return CommonResp.error(ErrorCodeConstant.CONTENT_ILLEGAL, getImageLabel(res.getData().getEvilType())); } } catch (TencentCloudSDKException e) { log.error("调用腾讯图片内容检测异常"); e.printStackTrace(); return CommonResp.failed("图片检查异常"); } return CommonResp.success(); } public static void main(String[] args) { final Base64.Encoder encoder = Base64.getEncoder(); Credential cred = new Credential("AKIDTjjV2IhK4ZKBm8z5g14vPedNSJuFnTIq", "PaVBZfeQwDVXKr7TZOzM6c9VZNwGJGyA"); TmsClient client = new TmsClient(cred, "ap-shanghai"); CmsClient cmsClient = new CmsClient(cred, "ap-shanghai"); TextModerationRequest req = new TextModerationRequest(); req.setContent(encoder.encodeToString("卖毒品".getBytes(StandardCharsets.UTF_8))); ImageModerationRequest imgReq = new ImageModerationRequest(); imgReq.setFileUrl("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.08lr.cn%2Fuploads%2Fallimg%2F170513%2F1-1F513164126.jpg&refer=http%3A%2F%2Fwww.08lr.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1617540249&t=2506e9bd61a31ea3bf89d07f4fe49744"); try { TextModerationResponse res = client.TextModeration(req); ImageModerationResponse imgRes = cmsClient.ImageModeration(imgReq); System.out.println(Arrays.toString(res.getKeywords())); System.out.println(imgRes.getData()); } catch (Exception e) { e.printStackTrace(); } } public static String getTextLabel(String label, String[] keywords) { StringBuilder message = new StringBuilder("该内容包含"); if (label.equals("Porn")) { message.append("色情"); } else if (label.equals("Abuse")) { message.append("谩骂"); } else if (label.equals("Ad")) { message.append("广告"); } else if (label.equals("Polity")) { message.append("政治"); } else if (label.equals("Illegal")) { message.append("违法"); } message.append("等敏感词"); if (keywords != null && keywords.length > 0) { message.append(" "); message.append(Arrays.toString(keywords)); } return message.toString(); } public static String getImageLabel(Long label) { StringBuilder message = new StringBuilder("该图片可能包含"); if (label.equals(20002)) { message.append("色情"); } else if (label.equals(20007)) { message.append("谩骂"); } else if (label.equals(20001) || label.equals(24001)) { message.append("政治"); } else if (label.equals(20006)) { message.append("违法"); } else if (label.equals(20103)) { message.append("性感"); } message.append("敏感内容"); return message.toString(); } }