获取filekey接口兼容处理请求参数不是url情况

This commit is contained in:
tianliyong 2022-12-13 11:04:10 +08:00
parent 1cd40155ae
commit 2e571f6bab
2 changed files with 68 additions and 45 deletions

View File

@ -6,6 +6,7 @@ import cn.azxo.framework.common.utils.LogUtil;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -89,6 +90,10 @@ public class AliOssServiceImpl implements AliOssService {
@Override
public InputStream downloadFile(String bucket, String tgtFileKey) {
OSS client = aliOssClient.getClient();
// TODO: 2022/12/9 支持图片样式
/*GetObjectRequest request = new GetObjectRequest(bucket, tgtFileKey);
request.setProcess("");
client.getObject(request);*/
OSSObject ossObject = client.getObject(bucket, tgtFileKey);
return ossObject.getObjectContent();
}

View File

@ -54,6 +54,8 @@ public class FileServiceImpl implements FileService {
private static String APPCODE_BANKCARD = "bankcard";
private static String SCENE_BANKCARD = "bankcard";
private static String IS_URL = "http://";
@Autowired
private FileManager fileManager;
@ -225,7 +227,7 @@ public class FileServiceImpl implements FileService {
List<String> fileKeyList = new ArrayList<>();
Map<String, String> fileKeyStyleMap = new HashMap<>();
for (String fileKey : dto.getFileKey()) {
if (fileKey.contains("http")) {
if (fileKey.contains(IS_URL)) {
urlList.add(fileKey);
} else {
//对fileKey带图片样式进行处理
@ -238,13 +240,9 @@ public class FileServiceImpl implements FileService {
}
}
}
if (urlList.size() == dto.getFileKey().size()) {
return setFileUrlRes(urlList);
}
List<File> fileList = fileDao.getByFileUuids(fileKeyList);
if (CollectionUtil.isEmpty(fileList)) {
log.warn("find file url is null,key = {}", Arrays.toString(dto.getFileKey().toArray()));
return new ArrayList<>();
List<File> fileList = new ArrayList<>();
if (CollectionUtil.isNotEmpty(fileKeyList)) {
fileList = fileDao.getByFileUuids(fileKeyList);
}
return setFileUrlRes(urlList, fileKeyStyleMap, fileList);
}
@ -265,17 +263,19 @@ public class FileServiceImpl implements FileService {
private List<FindFileUrlResponse> setFileUrlRes(List<String> urlList, Map<String, String> fileKeyStyleMap, List<File> fileList) {
List<FindFileUrlResponse> resList = setFileUrlRes(urlList);
fileList.stream().forEach(file -> {
FindFileUrlResponse response = new FindFileUrlResponse();
response.setUrl(file.getFileUrl());
String fileKey = file.getFileUuid();
if (fileKeyStyleMap.containsKey(fileKey)) {
response.setFileKey(fileKeyStyleMap.get(fileKey));
} else {
response.setFileKey(fileKey);
}
resList.add(response);
});
if (CollectionUtil.isNotEmpty(fileList)) {
fileList.stream().forEach(file -> {
FindFileUrlResponse response = new FindFileUrlResponse();
response.setUrl(file.getFileUrl());
String fileKey = file.getFileUuid();
if (fileKeyStyleMap.containsKey(fileKey)) {
response.setFileKey(fileKeyStyleMap.get(fileKey));
} else {
response.setFileKey(fileKey);
}
resList.add(response);
});
}
return resList;
}
@ -284,23 +284,52 @@ public class FileServiceImpl implements FileService {
log.info("find file key dto = {}", JsonUtil.obj2Str(dto));
List<String> urlList = dto.getUrl();
List<String> urlMd5List = urlList.stream()
.map(url -> Utility.getMd5(url))
.collect(Collectors.toList());
List<File> fileList = fileDao.getByUrlMd5s(urlMd5List);
//处理app端历史数据不在file表中的情况
if (urlMd5List.size() > fileList.size()) {
List<String> existUrlList = fileList.stream().map(File::getFileUrl).collect(Collectors.toList());
urlList.removeAll(existUrlList);
List<FindFileKeyResponse> resList = setFileKeyResByUrl(urlList);
// 异步新增file数据
asyncSaveFile(resList);
return resList;
//用于对请求参数中为fileKey的兼容处理
List<String> fileKeyList = new ArrayList<>();
List<String> urlMd5List = new ArrayList<>();
urlList.stream().forEach(url -> {
if (url.contains(IS_URL)) {
urlMd5List.add(Utility.getMd5(url));
} else {
fileKeyList.add(url);
}
});
List<File> fileList = new ArrayList<>();
List<FindFileKeyResponse> resList = new ArrayList<>();
if (CollectionUtil.isNotEmpty(urlMd5List)) {
fileList = fileDao.getByUrlMd5s(urlMd5List);
//处理app端历史数据不在file表中的情况
if (urlMd5List.size() > fileList.size()) {
List<String> existUrlList = fileList.stream().map(File::getFileUrl).collect(Collectors.toList());
urlList.removeAll(existUrlList);
resList = setFileKeyResByUrl(urlList);
// 异步新增file数据
asyncSaveFile(resList);
}
}
resList.addAll(setFileKeyRes(fileKeyList, fileList));
return resList;
}
return setFileKeyResByFile(fileList);
private List<FindFileKeyResponse> setFileKeyRes(List<String> fileKeyList, List<File> fileList) {
List<FindFileKeyResponse> resList = new ArrayList<>();
if (CollectionUtil.isNotEmpty(fileKeyList)) {
fileKeyList.forEach(fileKey -> {
FindFileKeyResponse response = new FindFileKeyResponse();
response.setUrl(fileKey);
response.setFileKey(fileKey);
resList.add(response);
});
}
if (CollectionUtil.isNotEmpty(fileList)) {
fileList.forEach(file -> {
FindFileKeyResponse response = new FindFileKeyResponse();
response.setUrl(file.getFileUrl());
response.setFileKey(file.getFileUuid());
resList.add(response);
});
}
return resList;
}
private void asyncSaveFile(List<FindFileKeyResponse> resList) {
@ -372,17 +401,6 @@ public class FileServiceImpl implements FileService {
return resList;
}
private List<FindFileKeyResponse> setFileKeyResByFile(List<File> fileList) {
List<FindFileKeyResponse> resList = new ArrayList<>();
fileList.forEach(file -> {
FindFileKeyResponse response = new FindFileKeyResponse();
response.setUrl(file.getFileUrl());
response.setFileKey(file.getFileUuid());
resList.add(response);
});
return resList;
}
/**
* 判断文件是否符合要求
*/