加上通过url上传文件-返回下载url优化

This commit is contained in:
xudawei 2024-04-28 15:52:42 +08:00
parent d1212e402b
commit c618aec98a
2 changed files with 38 additions and 20 deletions

View File

@ -125,17 +125,25 @@ public class FileByUrlServiceImpl implements FileByUrlService {
@Autowired
private FileService fileService;
@Value("${sign.url.download.expire.second:2000}")
private Long SIGN_URL_DOWNLOAD_EXPIRE_SECOND;
/**
* 通过url上传
*/
@Override
public ServerFileUploadResponse uploadByUrl(String appCode, String bizScene, String fileName, String url, Integer channelType, ContextInfo.LiteSaasContext liteSaasContext) {
log.info("uploadByUrl params,appCode:{},bizScene:{}, fileName:{},url:{},channelType:{}", appCode, bizScene, fileName, url, channelType);
Long start = System.currentTimeMillis();
//重新构建链接
url = this.rebuildUrl(url);
File ossFile = uploadFileAndGetFile(appCode, bizScene, fileName, url, channelType);
//操作日志记录
operateLog(this.buildUploadParams(appCode, bizScene, fileName, url, channelType), "", FILE_UPLOAD_CODE, FILE_UPLOAD_NAME, liteSaasContext);
return buildResponse(ossFile);
ServerFileUploadResponse response = this.buildResponse(ossFile);
log.info("uploadByUrl result,appCode:{},bizScene:{}, fileName:{},url:{},channelType:{}, response:{},times:{}"
, appCode, bizScene, fileName, url, channelType, JsonUtil.obj2Str(response), System.currentTimeMillis() - start);
return response;
}
/**
@ -188,16 +196,6 @@ public class FileByUrlServiceImpl implements FileByUrlService {
log.info("update channelType:{}, fileName:{}, appCode:{}, bizScene:{}",
channelType, fileName, appCode, bizScene);
FileUploadConfig fileUploadConfig = getFileUploadConfig(appCode, bizScene, channelType);
// 上传文件并生成file对象
return generateFile(fileUploadConfig, fileName,url, fileUploadConfig.getChannelCode());
}
/**
* 获取配置信息
*/
private FileUploadConfig getFileUploadConfig(String appCode, String bizScene, Integer channelType) {
// 检查appCode
checkAppCode(appCode);
@ -211,7 +209,9 @@ public class FileByUrlServiceImpl implements FileByUrlService {
// 通过渠道码和桶名称获取获取指定上传配置
FileUploadConfig fileUploadConfig = fileUploadConfigManager
.getByUploadConfig(scene.getAppChannelBucketNo(), scene.getDirectory());
return fileUploadConfig;
// 上传文件并生成file对象
return generateFile(fileUploadConfig, fileName,url, fileUploadConfig.getChannelCode(), appChannelBucket.getBucketType(), scene.getDownloadExpiration());
}
/**
@ -228,8 +228,7 @@ public class FileByUrlServiceImpl implements FileByUrlService {
/**
* 生产File对象
*/
private File generateFile(FileUploadConfig fileUploadConfig, String fileName, String url, String channelCode) {
private File generateFile(FileUploadConfig fileUploadConfig, String fileName, String url, String channelCode, String bucketType, Long expire) {
// 判断容量
String fileConform = isFileConform(fileUploadConfig, fileName);
String uuid = Utility.getUUID();
@ -237,18 +236,37 @@ public class FileByUrlServiceImpl implements FileByUrlService {
String tgtFileKey = Utility.generateFileKey(fileUploadConfig.getDirectory(), uuid, fileConform);
// 上传文件
String fileUrl = fileManager.uploadByUrl(fileUploadConfig.getBucketName(), tgtFileKey, fileName, url, channelCode);
//重新构建fileUrl
fileUrl = rebuildFileUrl(fileUploadConfig, fileName, channelCode, bucketType, expire, tgtFileKey, fileUrl);
File file = rebuildOssFile(fileUploadConfig, fileName, fileConform, uuid, fileUrl, Utility.getMd5(url));
fileDao.save(file);
return file;
}
/**
* 重新构建fileUrl
*/
private String rebuildFileUrl(FileUploadConfig fileUploadConfig, String fileName, String channelCode, String bucketType, Long expire, String tgtFileKey, String fileUrl) {
switch (BucketTypeEnum.getByCode(bucketType)) {
case PUBLIC_BUCKET:
fileUrl = this.fileManager.fetchDownloadUrl(fileUploadConfig.getBucketName(), tgtFileKey, channelCode);
break;
case PRIVATE_BUCKET:
fileUrl = this.fileManager.signUrlDownload(fileUploadConfig.getBucketName(), tgtFileKey, Objects.nonNull(expire) ? expire : SIGN_URL_DOWNLOAD_EXPIRE_SECOND , channelCode, fileName);
default:
BizException.error(CodeEnum.CHANNEL_TYPE_NOT_EXIST);
}
// 保存失败
if (Utility.isBlank(fileUrl)) {
log.error("fileUrl is empty");
throw new BizException(CodeEnum.FILE_UPLOAD_FAILED);
}
File file = rebuildOssFile(fileUploadConfig, fileName, fileConform, uuid, fileUrl, Utility.getMd5(url));
fileDao.save(file);
return file;
return fileUrl;
}
/**
*
* @param fileUploadConfig

View File

@ -867,7 +867,7 @@ 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);
log.info("signUrl download end dto = {},responseList:{}, times:{}", JsonUtil.obj2Str(dto), JsonUtil.obj2Str(responseList), System.currentTimeMillis() - start);
return responseList;
}
@ -911,7 +911,7 @@ public class FileServiceImpl implements FileService {
operateLog(dto.toString(), dto.getServiceName(), FILE_UPLOAD_CODE, FILE_UPLOAD_NAME, liteSaasContext);
//3 保存File对象
SignUrlUploadResponse response = this.signUrlSaveFile(dto, fileUploadConfig, scene, appChannelBucket);
log.info("signUrl upload dto = {}, times:{}", JsonUtil.obj2Str(dto), System.currentTimeMillis() - start);
log.info("signUrl upload dto = {},response:{}, times:{}", JsonUtil.obj2Str(dto), JsonUtil.obj2Str(response),System.currentTimeMillis() - start);
return response;
}