From c618aec98a7952baafa763ca8a0ca2b09fb0aeaf Mon Sep 17 00:00:00 2001 From: xudawei Date: Sun, 28 Apr 2024 15:52:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E9=80=9A=E8=BF=87url?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6-=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E4=B8=8B=E8=BD=BDurl=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/FileByUrlServiceImpl.java | 54 ++++++++++++------- .../oss/service/impl/FileServiceImpl.java | 4 +- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/oss-service/src/main/java/cn/axzo/oss/service/impl/FileByUrlServiceImpl.java b/oss-service/src/main/java/cn/axzo/oss/service/impl/FileByUrlServiceImpl.java index 90759a2..6678147 100644 --- a/oss-service/src/main/java/cn/axzo/oss/service/impl/FileByUrlServiceImpl.java +++ b/oss-service/src/main/java/cn/axzo/oss/service/impl/FileByUrlServiceImpl.java @@ -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 diff --git a/oss-service/src/main/java/cn/axzo/oss/service/impl/FileServiceImpl.java b/oss-service/src/main/java/cn/axzo/oss/service/impl/FileServiceImpl.java index 850b12a..e5b4879 100644 --- a/oss-service/src/main/java/cn/axzo/oss/service/impl/FileServiceImpl.java +++ b/oss-service/src/main/java/cn/axzo/oss/service/impl/FileServiceImpl.java @@ -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; }