♻️ Refactoring code. 重构文件上传支持文件夹参数

This commit is contained in:
lbw committed 2023-12-03 23:44:10 +08:00
1 parent 8aa59c16fe
commit 83dc27186b
7 files changed
+125 -22

No files matched your search

@@ -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</a>
*/
S3Object getObject(String bucketName, String dir, String objectName);
/**
* 获取文件
* @param bucketName bucket名称
@@ -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</a>
*/
@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
@@ -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</a>
*/
@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名称
@@ -63,6 +63,12 @@ public class SysFile extends Model<SysFile> {
@Schema(description = "存储桶名称")
private String bucketName;
/***
* 文件夹
*/
@Schema(description = "文件夹")
private String dir;
/**
* 文件类型
*/
@@ -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);
}
/**
@@ -38,20 +38,20 @@ public interface SysFileService extends IService<SysFile> {
/**
* 上传文件
* @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);
/**
* 删除文件
@@ -69,23 +69,25 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> 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<String, String> 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<SysFileMapper, SysFile> 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.<SysFile>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<SysFileMapper, SysFile> 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<SysFileMapper, SysFile> 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());