From 83dc27186bb2c2fa184360ac68b2b88387585378 Mon Sep 17 00:00:00 2001 From: lbw Date: Sat, 21 Oct 2023 11:13:27 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20Refactoring=20code.=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pigx/common/file/core/FileTemplate.java | 21 +++++++++++ .../common/file/local/LocalFileTemplate.java | 34 +++++++++++++++++ .../common/file/oss/service/OssTemplate.java | 37 +++++++++++++++++++ .../pigx/admin/api/entity/SysFile.java | 6 +++ .../admin/controller/SysFileController.java | 12 +++--- .../pigx/admin/service/SysFileService.java | 12 +++--- .../service/impl/SysFileServiceImpl.java | 25 ++++++++----- 7 files changed, 125 insertions(+), 22 deletions(-) diff --git a/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/file/core/FileTemplate.java b/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/file/core/FileTemplate.java index f789b7829..019846cdf 100644 --- a/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/file/core/FileTemplate.java +++ b/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/file/core/FileTemplate.java @@ -36,6 +36,18 @@ public interface FileTemplate extends InitializingBean { */ void removeBucket(String bucketName); + /** + * 上传文件 + * @param bucketName bucket名称 + * @param objectName 文件名称 + * @param dir 文件夹名称 + * @param stream 文件流 + * @param contextType 文件类型 + * @throws Exception + */ + void putObject(String bucketName, String dir, String objectName, InputStream stream, String contextType) + throws Exception; + /** * 上传文件 * @param bucketName bucket名称 @@ -56,6 +68,15 @@ public interface FileTemplate extends InitializingBean { */ void putObject(String bucketName, String objectName, InputStream stream) throws Exception; + /** + * 获取文件 + * @param bucketName bucket名称 + * @param dir 文件夹名称 + * @param objectName 文件名称 + * @return 二进制流 API Documentation + */ + S3Object getObject(String bucketName, String dir, String objectName); + /** * 获取文件 * @param bucketName bucket名称 diff --git a/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/file/local/LocalFileTemplate.java b/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/file/local/LocalFileTemplate.java index f330c264e..0e556bf66 100644 --- a/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/file/local/LocalFileTemplate.java +++ b/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/file/local/LocalFileTemplate.java @@ -1,6 +1,7 @@ package com.pig4cloud.pigx.common.file.local; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.StrUtil; import com.amazonaws.services.s3.model.Bucket; import com.amazonaws.services.s3.model.S3Object; import com.amazonaws.services.s3.model.S3ObjectSummary; @@ -76,6 +77,24 @@ public class LocalFileTemplate implements FileTemplate { FileUtil.writeFromStream(stream, file); } + /** + * 上传文件 + * @param bucketName bucket名称 + * @param dir 文件夹名称 + * @param objectName 文件名称 + * @param stream 文件流 + * @param contextType 文件类型 + * @throws Exception + */ + @Override + public void putObject(String bucketName, String dir, String objectName, InputStream stream, String contextType) + throws Exception { + if (StrUtil.isNotBlank(dir)) { + bucketName = bucketName + FileUtil.FILE_SEPARATOR + dir; + } + putObject(bucketName, objectName, stream, contextType); + } + /** * 获取文件 * @param bucketName bucket名称 @@ -91,6 +110,21 @@ public class LocalFileTemplate implements FileTemplate { return s3Object; } + /** + * 获取文件 + * @param bucketName bucket名称 + * @param dir 文件夹名称 + * @param objectName 文件名称 + * @return 二进制流 API Documentation + */ + @Override + public S3Object getObject(String bucketName, String dir, String objectName) { + if (StrUtil.isNotBlank(dir)) { + bucketName = bucketName + FileUtil.FILE_SEPARATOR + dir; + } + return getObject(bucketName, objectName); + } + /** * @param bucketName * @param objectName diff --git a/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/file/oss/service/OssTemplate.java b/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/file/oss/service/OssTemplate.java index 44417c8b6..724950b6a 100755 --- a/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/file/oss/service/OssTemplate.java +++ b/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/file/oss/service/OssTemplate.java @@ -17,6 +17,8 @@ package com.pig4cloud.pigx.common.file.oss.service; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.StrUtil; import com.amazonaws.ClientConfiguration; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSCredentialsProvider; @@ -144,6 +146,21 @@ public class OssTemplate implements InitializingBean, FileTemplate { return amazonS3.getObject(bucketName, objectName); } + /** + * 获取文件 + * @param bucketName bucket名称 + * @param dir 文件夹名称 + * @param objectName 文件名称 + * @return 二进制流 API Documentation + */ + @Override + public S3Object getObject(String bucketName, String dir, String objectName) { + if (StrUtil.isNotBlank(dir)) { + objectName = dir + FileUtil.FILE_SEPARATOR + objectName; + } + return getObject(bucketName, objectName); + } + /** * 上传文件 * @param bucketName bucket名称 @@ -168,6 +185,26 @@ public class OssTemplate implements InitializingBean, FileTemplate { putObject(bucketName, objectName, stream, stream.available(), contextType); } + /** + * 上传文件 + * @param bucketName bucket名称 + * @param dir 文件夹名称 + * @param objectName 文件名称 + * @param stream 文件流 + * @param contextType 文件类型 + * @throws Exception + */ + @Override + public void putObject(String bucketName, String dir, String objectName, InputStream stream, String contextType) + throws Exception { + if (StrUtil.isNotBlank(dir)) { + // dir 路径为 a/b + objectName = dir + FileUtil.FILE_SEPARATOR + objectName; + } + + putObject(bucketName, objectName, stream, stream.available(), contextType); + } + /** * 上传文件 * @param bucketName bucket名称 diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysFile.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysFile.java index 9521627bb..66a1a3c4e 100644 --- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysFile.java +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysFile.java @@ -63,6 +63,12 @@ public class SysFile extends Model { @Schema(description = "存储桶名称") private String bucketName; + /*** + * 文件夹 + */ + @Schema(description = "文件夹") + private String dir; + /** * 文件类型 */ diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/SysFileController.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/SysFileController.java index dfb678526..993793fc9 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/SysFileController.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/SysFileController.java @@ -99,26 +99,26 @@ public class SysFileController { /** * 上传文件 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常 * @param file 资源 + * @param dir 文件夹 * @return R(/ admin / bucketName / filename) */ @PostMapping(value = "/upload") - public R upload(@RequestPart("file") MultipartFile file, + public R upload(@RequestPart("file") MultipartFile file, @RequestParam(value = "dir", required = false) String dir, @RequestParam(value = "groupId", required = false) Long groupId, @RequestParam(value = "type", required = false) String type) { - return sysFileService.uploadFile(file, groupId, type); + return sysFileService.uploadFile(file, dir,groupId, type); } /** * 获取文件 - * @param bucket 桶名称 * @param fileName 文件空间/名称 * @param response * @return */ @Inner(false) - @GetMapping("/{bucket}/{fileName}") - public void file(@PathVariable String bucket, @PathVariable String fileName, HttpServletResponse response) { - sysFileService.getFile(bucket, fileName, response); + @GetMapping("/oss/file") + public void file(String fileName, HttpServletResponse response) { + sysFileService.getFile(fileName, response); } /** diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysFileService.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysFileService.java index f91edb4aa..103524186 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysFileService.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/SysFileService.java @@ -38,20 +38,20 @@ public interface SysFileService extends IService { /** * 上传文件 - * @param file - * @param groupId - * @param type + * @param file 文件流 + * @param dir 文件夹 + * @param groupId 分组ID + * @param type 类型 * @return */ - R uploadFile(MultipartFile file, Long groupId, String type); + R uploadFile(MultipartFile file, String dir, Long groupId, String type); /** * 读取文件 - * @param bucket 桶名称 * @param fileName 文件名称 * @param response 输出流 */ - void getFile(String bucket, String fileName, HttpServletResponse response); + void getFile(String fileName, HttpServletResponse response); /** * 删除文件 diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysFileServiceImpl.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysFileServiceImpl.java index 8718fc1c3..0cb1be179 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysFileServiceImpl.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysFileServiceImpl.java @@ -69,23 +69,25 @@ public class SysFileServiceImpl extends ServiceImpl impl /** * 上传文件 - * @param file - * @param groupId - * @param type + * @param file 文件流 + * @param dir 文件夹 + * @param groupId 分组ID + * @param type 类型 * @return */ @Override - public R uploadFile(MultipartFile file, Long groupId, String type) { + public R uploadFile(MultipartFile file, String dir, Long groupId, String type) { String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename()); Map resultMap = new HashMap<>(4); resultMap.put("bucketName", properties.getBucketName()); resultMap.put("fileName", fileName); - resultMap.put("url", String.format("/admin/sys-file/%s/%s", properties.getBucketName(), fileName)); + + resultMap.put("url", String.format("/admin/sys-file/oss/file?fileName=%s", fileName)); try (InputStream inputStream = file.getInputStream()) { - fileTemplate.putObject(properties.getBucketName(), fileName, inputStream, file.getContentType()); + fileTemplate.putObject(properties.getBucketName(), dir, fileName, inputStream, file.getContentType()); // 文件管理数据记录,收集管理追踪文件 - fileLog(file, fileName, groupId, type); + fileLog(file, dir, fileName, groupId, type); } catch (Exception e) { log.error("上传失败", e); @@ -101,8 +103,9 @@ public class SysFileServiceImpl extends ServiceImpl impl * @param response */ @Override - public void getFile(String bucket, String fileName, HttpServletResponse response) { - try (S3Object s3Object = fileTemplate.getObject(bucket, fileName)) { + public void getFile(String fileName, HttpServletResponse response) { + SysFile sysFile = baseMapper.selectOne(Wrappers.lambdaQuery().eq(SysFile::getFileName, fileName)); + try (S3Object s3Object = fileTemplate.getObject(sysFile.getBucketName(), sysFile.getDir(), fileName)) { response.setContentType("application/octet-stream; charset=UTF-8"); IoUtil.copy(s3Object.getObjectContent(), response.getOutputStream()); } @@ -199,11 +202,12 @@ public class SysFileServiceImpl extends ServiceImpl impl /** * 文件管理数据记录,收集管理追踪文件 * @param file 上传的文件格式 + * @param dir 文件夹 * @param fileName 文件名 * @param groupId 文件组ID * @param type 文件类型 */ - private void fileLog(MultipartFile file, String fileName, Long groupId, String type) { + private void fileLog(MultipartFile file, String dir, String fileName, Long groupId, String type) { // 创建SysFile对象并设置相关属性 SysFile sysFile = new SysFile(); sysFile.setFileName(fileName); @@ -212,6 +216,7 @@ public class SysFileServiceImpl extends ServiceImpl impl String originalFilename = new String( Objects.requireNonNull(file.getOriginalFilename()).getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); + sysFile.setDir(dir); sysFile.setOriginal(originalFilename); sysFile.setFileSize(file.getSize()); sysFile.setBucketName(properties.getBucketName());