feat(api): 添加文件上传组件专用接口支持本地和OSS上传
- 在api store中新增fileUploadApplicationApi和ossUploadApplicationApi两个专用接口地址 - 从getters中移除已废弃的deployUploadApi和databaseUploadApi - 将上传组件改为使用新的专用接口,并通过mode参数区分本地和OSS上传 - 更新组件文档注释将上传方式从server改为local - 创建CuteFileUploadApi控制器提供uploadLocal和uploadOSS接口 - 在SystemLocalStorageService中实现uploadLocal方法处理本地文件上传逻辑 - 更新演示页面默认使用本地上传模式
This commit is contained in:
+41
@@ -0,0 +1,41 @@
|
||||
package cn.odboy.system.application;
|
||||
|
||||
import cn.odboy.system.framework.operalog.OperationLog;
|
||||
import cn.odboy.system.service.SystemLocalStorageService;
|
||||
import cn.odboy.system.service.SystemOssStorageService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@RestController
|
||||
@Api(tags = "系统组件:CuteFileUpload")
|
||||
@RequestMapping("/api/component/CuteFileUpload")
|
||||
public class CuteFileUploadApi {
|
||||
|
||||
@Autowired
|
||||
private SystemLocalStorageService systemLocalStorageService;
|
||||
@Autowired
|
||||
private SystemOssStorageService systemOssStorageService;
|
||||
|
||||
@OperationLog
|
||||
@ApiOperation("上传文件到服务器本地")
|
||||
@PostMapping(value = "/uploadLocal")
|
||||
public ResponseEntity<String> uploadLocal(@RequestParam("file") MultipartFile file) {
|
||||
String fileUrl = systemLocalStorageService.uploadLocal(file);
|
||||
return ResponseEntity.ok(fileUrl);
|
||||
}
|
||||
|
||||
@OperationLog
|
||||
@ApiOperation("上传文件到OSS")
|
||||
@PostMapping(value = "/uploadOSS")
|
||||
public ResponseEntity<String> uploadOSS(@RequestParam("file") MultipartFile file) {
|
||||
String fileUrl = systemOssStorageService.uploadFile(file);
|
||||
return ResponseEntity.ok(fileUrl);
|
||||
}
|
||||
}
|
||||
+38
@@ -22,6 +22,7 @@ import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.odboy.base.KitPageResult;
|
||||
import cn.odboy.constant.FileTypeEnum;
|
||||
import cn.odboy.framework.context.KitRequestHolder;
|
||||
import cn.odboy.framework.exception.BadRequestException;
|
||||
import cn.odboy.framework.properties.AppProperties;
|
||||
import cn.odboy.framework.server.core.KitFileLocalUploadHelper;
|
||||
@@ -41,6 +42,7 @@ import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
@@ -194,4 +196,40 @@ public class SystemLocalStorageService {
|
||||
}
|
||||
wrapper.orderByDesc(SystemLocalStorageTb::getId);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public String uploadLocal(MultipartFile multipartFile) {
|
||||
long size = multipartFile.getSize();
|
||||
KitFileUtil.checkSize(properties.getOss().getMaxSize(), size);
|
||||
String suffix = KitFileUtil.getSuffix(multipartFile.getOriginalFilename());
|
||||
String type = KitFileUtil.getFileType(suffix);
|
||||
String uploadDateStr = DateUtil.format(new Date(), DatePattern.PURE_DATE_FORMAT);
|
||||
File file = KitFileUtil.upload(multipartFile, fileUploadPathHelper.getPath() + uploadDateStr + File.separator);
|
||||
if (file == null) {
|
||||
throw new BadRequestException("上传失败");
|
||||
}
|
||||
try {
|
||||
String formatSize = KitFileUtil.getSize(size);
|
||||
String prefixName = KitFileUtil.getPrefix(multipartFile.getOriginalFilename(), null);
|
||||
SystemLocalStorageTb localStorage = new SystemLocalStorageTb();
|
||||
localStorage.setRealName(file.getName());
|
||||
localStorage.setName(prefixName);
|
||||
localStorage.setSuffix(suffix);
|
||||
localStorage.setPath(file.getPath());
|
||||
localStorage.setType(type);
|
||||
localStorage.setSize(formatSize);
|
||||
localStorage.setDateGroup(uploadDateStr);
|
||||
systemLocalStorageMapper.insert(localStorage);
|
||||
// 构建上传路径 /file/20260117/odboycn-20260117055246428.png
|
||||
HttpServletRequest httpServletRequest = KitRequestHolder.getHttpServletRequest();
|
||||
StringBuffer requestURL = httpServletRequest.getRequestURL();
|
||||
String[] splits = requestURL.toString().split("/api/component/CuteFileUpload");
|
||||
String requestAddress = splits[0];
|
||||
return String.format("%s/file/%s/%s", requestAddress, uploadDateStr, localStorage.getRealName());
|
||||
} catch (Exception e) {
|
||||
log.error("上传文件失败", e);
|
||||
KitFileUtil.del(file);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user