Pre Merge pull request !489 from 丶Stone/dev

This commit is contained in:
丶Stone 2024-02-26 02:42:15 +00:00 committed by Gitee
commit e6eb4ae0ed
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 30 additions and 6 deletions

View File

@ -162,10 +162,11 @@ public class OssClient {
* @param filePath 本地文件路径 * @param filePath 本地文件路径
* @param key Amazon S3 中的对象键 * @param key Amazon S3 中的对象键
* @param md5Digest 本地文件的 MD5 哈希值可选 * @param md5Digest 本地文件的 MD5 哈希值可选
* @param deleteAfterUpload 是否自动删除临时文件
* @return UploadResult 包含上传后的文件信息 * @return UploadResult 包含上传后的文件信息
* @throws OssException 如果上传失败抛出自定义异常 * @throws OssException 如果上传失败抛出自定义异常
*/ */
public UploadResult upload(Path filePath, String key, String md5Digest) { public UploadResult upload(Path filePath, String key, String md5Digest, Boolean deleteAfterUpload) {
try { try {
// 构建上传请求对象 // 构建上传请求对象
FileUpload fileUpload = transferManager.uploadFile( FileUpload fileUpload = transferManager.uploadFile(
@ -187,8 +188,9 @@ public class OssClient {
// 捕获异常并抛出自定义异常 // 捕获异常并抛出自定义异常
throw new OssException("上传文件失败,请检查配置信息:[" + e.getMessage() + "]"); throw new OssException("上传文件失败,请检查配置信息:[" + e.getMessage() + "]");
} finally { } finally {
// 无论上传是否成功最终都会删除临时文件 if (deleteAfterUpload){
FileUtils.del(filePath); FileUtils.del(filePath);
}
} }
} }
@ -322,11 +324,12 @@ public class OssClient {
* *
* @param file 要上传的文件 * @param file 要上传的文件
* @param suffix 对象键的后缀 * @param suffix 对象键的后缀
* @param deleteAfterUpload 是否自动删除临时文件
* @return UploadResult 包含上传后的文件信息 * @return UploadResult 包含上传后的文件信息
* @throws OssException 如果上传失败抛出自定义异常 * @throws OssException 如果上传失败抛出自定义异常
*/ */
public UploadResult uploadSuffix(File file, String suffix) { public UploadResult uploadSuffix(File file, String suffix, Boolean deleteAfterUpload) {
return upload(file.toPath(), getPath(properties.getPrefix(), suffix), null); return upload(file.toPath(), getPath(properties.getPrefix(), suffix), null, deleteAfterUpload);
} }
/** /**

View File

@ -60,6 +60,15 @@ public interface ISysOssService {
*/ */
SysOssVo upload(File file); SysOssVo upload(File file);
/**
* 上传文件到对象存储服务并保存文件信息到数据库
*
* @param file 要上传的文件对象
* @param deleteAfterUpload 是否自动删除临时文件
* @return 上传成功后的 SysOssVo 对象包含文件信息
*/
SysOssVo upload(File file, Boolean deleteAfterUpload);
/** /**
* 文件下载方法支持一次性下载完整文件 * 文件下载方法支持一次性下载完整文件
* *

View File

@ -196,10 +196,22 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
*/ */
@Override @Override
public SysOssVo upload(File file) { public SysOssVo upload(File file) {
return this.upload(file, true);
}
/**
* 上传文件到对象存储服务并保存文件信息到数据库
*
* @param file 要上传的文件对象
* @param deleteAfterUpload 是否自动删除临时文件
* @return 上传成功后的 SysOssVo 对象包含文件信息
*/
@Override
public SysOssVo upload(File file, Boolean deleteAfterUpload) {
String originalfileName = file.getName(); String originalfileName = file.getName();
String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length()); String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
OssClient storage = OssFactory.instance(); OssClient storage = OssFactory.instance();
UploadResult uploadResult = storage.uploadSuffix(file, suffix); UploadResult uploadResult = storage.uploadSuffix(file, suffix, deleteAfterUpload);
// 保存文件信息 // 保存文件信息
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult); return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult);
} }