1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.tanpu.community.manager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.green.model.v20180509.ImageAsyncScanResultsRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.tanpu.common.constant.ErrorCodeConstant;
import com.tanpu.common.exception.BizException;
import com.tanpu.community.api.beans.resp.FileUploadResp;
import com.tanpu.community.api.enums.FileChechStatusEnum;
import com.tanpu.community.api.enums.OssDirEnum;
import com.tanpu.community.dao.entity.community.FileRecordEntity;
import com.tanpu.community.service.OSSFileService;
import com.tanpu.community.util.ConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@Service
public class FileManager {
@Autowired
private OSSFileService ossFileService;
public FileUploadResp uploadFile(MultipartFile file, OssDirEnum dirEnum, String userId) {
if (file == null) {
throw new BizException(ErrorCodeConstant.FILE_UPLOAD_FAIL);
}
byte[] data = null;
try {
data = file.getBytes();
} catch (IOException e) {
throw new BizException(ErrorCodeConstant.FILE_UPLOAD_FAIL);
}
String originalName = file.getOriginalFilename();
if (data.length == 0 || StringUtils.isEmpty(originalName)) {
throw new BizException("上传文件为空");
}
String dirPrefix = "";
if (dirEnum != null) {
dirPrefix = dirPrefix + dirEnum.dir;
}
if (StringUtils.isNotBlank(userId)) {
dirPrefix = dirPrefix + userId + "/";
}
FileRecordEntity fileRecordEntity = ossFileService.uploadFile(data, originalName, dirPrefix);
return ConvertUtil.fileRecordEntity2Resp(fileRecordEntity);
}
public Map<String, String> getFileUrlByIds(List<String> fileIds) {
return ossFileService.queryByIds(fileIds).stream()
.collect(Collectors.toMap(FileRecordEntity::getFileId, FileRecordEntity::getUrl));
}
public void updateCheckResult(String fileKey, FileChechStatusEnum type, String resultLog) {
FileRecordEntity fileRecordEntity = ossFileService.queryByOssKey(fileKey);
if (fileRecordEntity == null) {
throw new BizException("图片未找到:" + fileKey);
}
fileRecordEntity.setCheckStatus(type.getCode());
fileRecordEntity.setCheckResultLog(resultLog);
ossFileService.update(fileRecordEntity);
}
// 同步查询,使用taskId,且1/24小时内才能查询
public JSONArray queryTask(String taskId) throws Exception {
// 帐号没有权限,会导致查询结果为空
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", ossFileService.accessId,
ossFileService.accessSK);
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
ImageAsyncScanResultsRequest imageAsyncScanResultsRequest = new ImageAsyncScanResultsRequest();
// 指定API返回格式。
imageAsyncScanResultsRequest.setAcceptFormat(FormatType.JSON);
// 指定请求方法。
imageAsyncScanResultsRequest.setMethod(MethodType.POST);
imageAsyncScanResultsRequest.setEncoding("utf-8");
// 支持HTTP和HTTPS。
imageAsyncScanResultsRequest.setProtocol(ProtocolType.HTTP);
List<String> taskIds = new ArrayList<String>();
taskIds.add(taskId);
imageAsyncScanResultsRequest.setHttpContent(JSON.toJSONString(taskIds).getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* 请务必设置超时时间。
*/
imageAsyncScanResultsRequest.setConnectTimeout(3000);
imageAsyncScanResultsRequest.setReadTimeout(6000);
HttpResponse httpResponse = client.doAction(imageAsyncScanResultsRequest);
if (httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println(JSON.toJSONString(scrResponse, true));
if (200 == scrResponse.getInteger("code")) {
JSONArray taskResults = scrResponse.getJSONArray("data");
for (Object taskResult : taskResults) {
if (200 == ((JSONObject) taskResult).getInteger("code")) {
JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
for (Object sceneResult : sceneResults) {
String scene = ((JSONObject) sceneResult).getString("scene");
String suggestion = ((JSONObject) sceneResult).getString("suggestion");
// 根据scene和suggestion做相关的处理。
// 根据不同的suggestion结果做业务上的不同处理。例如,将违规数据删除等。
}
return sceneResults;
} else {
throw new BizException("task process fail:" + ((JSONObject) taskResult).getInteger("code"));
}
}
} else {
throw new BizException("detect not success. code:" + scrResponse.getInteger("code"));
}
} else {
throw new BizException("response not success. status:" + httpResponse.getStatus());
}
return null;
}
}