临时授权下载-入参是http时不做处理添加

This commit is contained in:
xudawei 2024-04-12 17:16:17 +08:00
parent d5e228c9e6
commit 334c089984

View File

@ -832,7 +832,31 @@ public class FileServiceImpl implements FileService {
log.info("signUrl download dto = {}", JsonUtil.obj2Str(dto));
Long start = System.currentTimeMillis();
List<File> fileList = fileDao.getByFileUuids(dto.getFileKeys());
if (CollectionUtil.isEmpty(dto.getFileKeys())) {
return Lists.newArrayList();
}
//构建http入参的对象集合
List<SignUrlDownloadResponse> httpUrlResList = this.buildHttpUrlResponse(dto);
//构建fileKey入参对象集合
List<SignUrlDownloadResponse> fileKeyResList = this.buildFileKeyResponse(dto);
httpUrlResList.addAll(fileKeyResList);
log.info("signUrl download end result = {}, times:{}", JsonUtil.obj2Str(httpUrlResList), System.currentTimeMillis() - start);
return httpUrlResList;
}
/**
* 构建fileKey(非http的入参)的返回对象
*/
private List<SignUrlDownloadResponse> buildFileKeyResponse(SignUrlDownloadDto dto) {
if (CollectionUtil.isEmpty(dto.getFileKeys())) {
return Lists.newArrayList();
}
List<String> fileKeyList = dto.getFileKeys().stream().filter(item -> StringUtils.isNoneBlank(item) && !item.startsWith("http")).collect(Collectors.toList());
if (CollectionUtil.isEmpty(dto.getFileKeys())) {
return Lists.newArrayList();
}
List<File> fileList = fileDao.getByFileUuids(fileKeyList);
if (CollectionUtils.isEmpty(fileList)) {
return Lists.newArrayList();
}
@ -840,23 +864,30 @@ public class FileServiceImpl implements FileService {
//通过appChannelBucketNo集合获取文件渠道桶信息
List<AppChannelBucket> appChannelBucketList = appChannelBucketManager.getByAppChannelBucketNos(fileList.stream().map(File::getAppChannelBucketNo).collect(Collectors.toSet()));
Map<String, String> bucketTypeMap = appChannelBucketList.stream().collect(Collectors.toMap(AppChannelBucket::getAppChannelBucketNo, AppChannelBucket::getBucketType, (x, y) -> y));
//构建场景集合
List<FileBusinessScene> fileBusinessSceneList = fileBusinessSceneManager.queryByBucketNoAndScene(fileList.stream().map(File::getAppChannelBucketNo).collect(Collectors.toSet()), dto.getBizScene());
Map<String, Long> bizSceneExpireMap = fileBusinessSceneList.stream().collect(Collectors.toMap(FileBusinessScene::getAppChannelBucketNo, FileBusinessScene::getDownloadExpiration, (x, y) -> y));
//构建返回集合
return this.buildFileKeyRespByFile(fileList, bucketTypeMap, bizSceneExpireMap);
}
/**
* 通过File对象构建返回集合
*/
private List<SignUrlDownloadResponse> buildFileKeyRespByFile(List<File> fileList,Map<String, String> bucketTypeMap, Map<String, Long> bizSceneExpireMap) {
List<SignUrlDownloadResponse> responseList = fileList.stream().map(item -> {
Long expire = bizSceneExpireMap.get(item.getAppChannelBucketNo());
// bucket下的key
String tgtFileKey = Utility.generateFileKey(item.getDirectory(), item.getFileUuid(), item.getFileFormat());
String bucketType = StringUtils.isNotBlank(bucketTypeMap.get(item.getAppChannelBucketNo())) ? bucketTypeMap.get(item.getAppChannelBucketNo()) : BucketTypeEnum.PRIVATE_BUCKET.getCode();
switch (BucketTypeEnum.getByCode(bucketType)) {
case PUBLIC_BUCKET:
case PUBLIC_BUCKET://公有桶 - 永久链接例如 http://xxx.png
String url = this.fileManager.fetchDownloadUrl(item.getBucketName(), tgtFileKey, item.getChannelCode());
return SignUrlDownloadResponse.builder()
.signUrl(url)
.fileKey(item.getFileUuid())
.build();
case PRIVATE_BUCKET:
case PRIVATE_BUCKET://私有桶 - 临时授权链接 例如 http://xxx.png?Expire=a&AccessKeyId=b&Signature=c&repsonse-content-disposition=d
String signUrl = this.fileManager.signUrlDownload(item.getBucketName(), tgtFileKey, Objects.nonNull(expire) ? expire : SIGN_URL_DOWNLOAD_EXPIRE_SECOND , item.getChannelCode(), item.getFileName());
return SignUrlDownloadResponse.builder()
.signUrl(UrlUtil.httpToHttps(signUrl))
@ -867,10 +898,26 @@ public class FileServiceImpl implements FileService {
}
return SignUrlDownloadResponse.builder().build();
}).collect(Collectors.toList());
log.info("signUrl download end dto = {}, times:{}", JsonUtil.obj2Str(dto), System.currentTimeMillis() - start);
return responseList;
}
/**
* 构建http链接(非fileKey入参)返回对象
*/
private List<SignUrlDownloadResponse> buildHttpUrlResponse(SignUrlDownloadDto dto) {
if (CollectionUtil.isEmpty(dto.getFileKeys())) {
return Lists.newArrayList();
}
List<String> httpUrlList = dto.getFileKeys().stream().filter(item -> StringUtils.isNoneBlank(item) && item.startsWith("http")).collect(Collectors.toList());
if (CollectionUtil.isEmpty(httpUrlList)) {
return Lists.newArrayList();
}
return httpUrlList.stream().map(item -> SignUrlDownloadResponse.builder()
.signUrl(item)
.fileKey(item)
.build()).collect(Collectors.toList());
}
/**
* 授权给第三方下载-上传
*/