add 新增上传附件保存扩展信息

This commit is contained in:
lau
2026-06-10 17:08:49 +08:00
parent 0b37392a8f
commit 4498b9c9d5
3 changed files with 16 additions and 11 deletions
@@ -7,10 +7,12 @@ import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.PageResult;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.QueryGroup;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.SysOssExt;
import org.dromara.system.domain.bo.SysOssBo;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
@@ -73,8 +75,8 @@ public class SysOssController extends BaseController {
@SaCheckPermission("system:oss:upload")
@Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R<SysOssUploadVo> upload(@RequestPart("file") MultipartFile file) {
SysOssVo oss = ossService.upload(file);
public R<SysOssUploadVo> upload(@RequestPart("file") MultipartFile file, @RequestParam(value = "ossExt", required = false) String ossExtJson) {
SysOssVo oss = ossService.upload(file, JsonUtils.parseObject(ossExtJson, SysOssExt.class));
SysOssUploadVo uploadVo = new SysOssUploadVo(oss.getUrl(), oss.getOriginalName(), oss.getOssId().toString());
return R.ok(uploadVo);
}
@@ -2,6 +2,7 @@ package org.dromara.system.service;
import org.dromara.common.core.domain.PageResult;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.system.domain.SysOssExt;
import org.dromara.system.domain.bo.SysOssBo;
import org.dromara.system.domain.vo.SysOssVo;
import org.springframework.http.ResponseEntity;
@@ -47,17 +48,19 @@ public interface ISysOssService {
* 上传 MultipartFile 到对象存储服务,并保存文件信息到数据库
*
* @param file 要上传的 MultipartFile 对象
* @param ossExt 扩展信息
* @return 上传成功后的 SysOssVo 对象,包含文件信息
*/
SysOssVo upload(MultipartFile file);
SysOssVo upload(MultipartFile file, SysOssExt ossExt);
/**
* 上传文件到对象存储服务,并保存文件信息到数据库
*
* @param file 要上传的文件对象
* @param ossExt 扩展信息
* @return 上传成功后的 SysOssVo 对象,包含文件信息
*/
SysOssVo upload(File file);
SysOssVo upload(File file, SysOssExt ossExt);
/**
* 文件下载方法,支持一次性下载完整文件
@@ -230,7 +230,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
* @throws ServiceException 如果上传过程中发生异常,则抛出 ServiceException 异常
*/
@Override
public SysOssVo upload(MultipartFile file) {
public SysOssVo upload(MultipartFile file, SysOssExt ossExt) {
if (ObjectUtil.isNull(file) || file.isEmpty()) {
throw new ServiceException("上传文件不能为空");
}
@@ -240,11 +240,11 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
String pathKey = instance.buildPathKey(originalfileName);
try (InputStream inputStream = file.getInputStream()) {
PutObjectResult result = instance.upload(pathKey, inputStream, file.getSize(), Options.builder().setContentType(file.getContentType()));
SysOssExt ext1 = new SysOssExt();
ext1.setFileSize(file.getSize());
ext1.setContentType(file.getContentType());
ossExt = ossExt == null ? new SysOssExt() : ossExt;
ossExt.setFileSize(file.getSize());
ossExt.setContentType(file.getContentType());
// 保存文件信息
return buildResultEntity(originalfileName, suffix, instance.clientId(), result, ext1);
return buildResultEntity(originalfileName, suffix, instance.clientId(), result, ossExt);
} catch (IOException e) {
throw new ServiceException(e.getMessage());
}
@@ -257,7 +257,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
* @return 上传成功后的 SysOssVo 对象,包含文件信息
*/
@Override
public SysOssVo upload(File file) {
public SysOssVo upload(File file, SysOssExt ossExt) {
if (ObjectUtil.isNull(file) || !file.isFile() || file.length() <= 0) {
throw new ServiceException("上传文件不能为空");
}
@@ -266,7 +266,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
OssClient instance = OssFactory.instance();
String pathKey = instance.buildPathKey(originalfileName);
PutObjectResult result = instance.upload(pathKey, file, Options.builder().setContentType(FileUtils.getMimeType(file.toPath())));
SysOssExt ext1 = new SysOssExt();
SysOssExt ext1 = ossExt == null ? new SysOssExt() : ossExt;
ext1.setFileSize(result.size());
// 保存文件信息
return buildResultEntity(originalfileName, suffix, instance.clientId(), result, ext1);