完成阿里云文本图片审核服务

This commit is contained in:
KilLze
2025-12-19 01:04:18 +08:00
parent 0aa68e3ec2
commit 8cb0830f8d
3 changed files with 57 additions and 8 deletions

14
pom.xml
View File

@@ -50,6 +50,14 @@
<scope>test</scope>
</dependency>
<!-- JUnit Platform Launcher for resolving junit-platform-launcher:1.8.2 issue -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
@@ -95,12 +103,6 @@
<version>0.2.8</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>teautil</artifactId>
<version>0.2.16</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>

View File

@@ -1,5 +1,7 @@
package com.bao.dating.service.impl;
import com.bao.dating.common.aliyun.GreenImageScan;
import com.bao.dating.common.aliyun.GreenTextScan;
import com.bao.dating.mapper.PostMapper;
import com.bao.dating.pojo.dto.PostRequestDTO;
import com.bao.dating.pojo.entity.Post;
@@ -14,6 +16,7 @@ import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
@@ -27,6 +30,12 @@ public class PostServiceImpl implements PostService {
@Autowired
private AliOssUtil ossUtil;
@Autowired
private GreenTextScan greenTextScan;
@Autowired
private GreenImageScan greenImageScan;
@Autowired
private PostMapper postMapper;
@@ -73,6 +82,34 @@ public class PostServiceImpl implements PostService {
*/
@Override
public Post createPost(PostRequestDTO postRequestDTO) {
// 1. 文本内容审核
Map textResult;
try {
textResult = greenTextScan.greeTextScan(postRequestDTO.getContent());
} catch (Exception e) {
throw new RuntimeException("文本审核失败");
}
String textSuggestion = (String) textResult.get("suggestion");
if ("block".equals(textSuggestion)) {
throw new RuntimeException("动态内容违规,禁止发布");
}
// 2. 图片审核(如果有)
if (postRequestDTO.getMediaUrls() != null && !postRequestDTO.getMediaUrls().isEmpty()) {
Map imageResult;
try {
imageResult = greenImageScan.imageScan(postRequestDTO.getMediaUrls());
} catch (Exception e) {
throw new RuntimeException(e);
}
if ("block".equals(imageResult.get("suggestion"))) {
throw new RuntimeException("图片内容违规,禁止发布");
}
}
Post post = new Post();
post.setUserId(postRequestDTO.getUserId());
post.setContent(postRequestDTO.getContent());
@@ -84,7 +121,14 @@ public class PostServiceImpl implements PostService {
post.setMediaOssKeys(postRequestDTO.getMediaUrls());
}
post.setIsPublic(0);
// 4. 根据审核结果设置状态
if ("review".equals(textSuggestion)) {
// 待审核,需人工审核
post.setIsPublic(0);
} else {
// 审核通过
post.setIsPublic(1);
}
post.setUpdatedAt(LocalDateTime.now());
post.setCreatedAt(LocalDateTime.now());

View File

@@ -31,7 +31,10 @@ public class DatingApplicationTests {
public void testUploadAndScanImageFromUrl() {
try {
// 阿里oss图片url
String imageUrl = "https://heimato.oss-cn-beijing.aliyuncs.com/9d1325d1-6e5a-426c-8081-306e7d3f6386.jpg?x-oss-credential=LTAI5tKo9TpWH1aW6JxWm1Gp%2F20251218%2Fcn-beijing%2Foss%2Faliyun_v4_request&x-oss-date=20251218T132839Z&x-oss-expires=3600&x-oss-signature-version=OSS4-HMAC-SHA256&x-oss-signature=a1b6b5a8e8728012093e9c38c6fb6f4487e7adf983db63c412848d3dbb0697b8";
String imageUrl = "https://heimato.oss-cn-beijing.aliyuncs.com/75d067b3-8605-494a-a839-b0a9d32c993a.jpg" +
"?x-oss-credential=LTAI5tKo9TpWH1aW6JxWm1Gp%2F20251218%2Fcn-beijing%2Foss%2Faliyun_v4_request" +
"&x-oss-date=20251218T164128Z&x-oss-expires=32400&x-oss-signature-version=OSS4-HMAC-SHA256" +
"&x-oss-signature=e9c46381a5d33e5c541fe2e838ff3ebeeb5dbc51f792ed31ae992aa0a4109d8a";
// 将图片URL添加到列表中进行检测
List<String> imageUrls = Arrays.asList(imageUrl);