feat(file): 添加批量上传文件接口
- 新增接口支持多个文件的批量上传功能 - 根据云存储配置处理上传路径与访问URL - 保存上传文件信息至数据库,包括缩略图和下载链接 - 添加权限控制,需具有sys:file:upload权限 - 捕获上传异常并返回失败信息
This commit is contained in:
@@ -50,10 +50,6 @@ public class CmsCaseController extends BaseController {
|
||||
@Operation(summary = "添加客户案例")
|
||||
@PostMapping()
|
||||
public ApiResult<?> save(@RequestBody CmsCase cmsCase) {
|
||||
User loginUser = getLoginUser();
|
||||
if (loginUser != null) {
|
||||
cmsCase.setUserId(loginUser.getUserId());
|
||||
}
|
||||
if (cmsCaseService.save(cmsCase)) {
|
||||
return success("添加成功");
|
||||
}
|
||||
|
||||
@@ -86,6 +86,50 @@ public class FileController extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:file:upload')")
|
||||
@Operation(summary = "批量上传文件")
|
||||
@PostMapping("/uploads")
|
||||
public ApiResult<List<FileRecord>> uploads(@RequestParam("files") MultipartFile[] files,
|
||||
HttpServletRequest request) {
|
||||
List<FileRecord> list = new ArrayList<>();
|
||||
try {
|
||||
String dir = getUploadDir();
|
||||
String requestURL = config.getFileServer() + "/api/file";
|
||||
// 云存储配置(与单文件上传保持一致)
|
||||
final String s = redisUtil.get("setting:upload:" + getTenantId());
|
||||
final JSONObject jsonObject = JSONObject.parseObject(s);
|
||||
final String uploadMethod = jsonObject == null ? "file" : jsonObject.getString("uploadMethod");
|
||||
final String bucketDomain = jsonObject == null ? "" : jsonObject.getString("bucketDomain");
|
||||
for (MultipartFile file : files) {
|
||||
File upload = FileServerUtil.upload(file, dir, config.getUploadUuidName());
|
||||
String path = upload.getAbsolutePath().replace("\\", "/").substring(dir.length() - 1);
|
||||
String originalName = file.getOriginalFilename();
|
||||
FileRecord result = new FileRecord();
|
||||
result.setCreateUserId(getLoginUserId());
|
||||
result.setName(StrUtil.isBlank(originalName) ? upload.getName() : originalName);
|
||||
result.setLength(upload.length());
|
||||
result.setPath(path);
|
||||
String contentType = FileServerUtil.getContentType(upload);
|
||||
result.setContentType(contentType);
|
||||
if (FileServerUtil.isImage(contentType)) {
|
||||
result.setThumbnail(requestURL + "/thumbnail" + path);
|
||||
}
|
||||
result.setDownloadUrl(config.getFileServer() + "/download" + path);
|
||||
String finalPath = path;
|
||||
if (uploadMethod != null && !"file".equals(uploadMethod)) {
|
||||
finalPath = bucketDomain + path;
|
||||
}
|
||||
result.setUrl(finalPath);
|
||||
fileRecordService.save(result);
|
||||
list.add(result);
|
||||
}
|
||||
return success(list);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return fail("上传失败", list).setError(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:file:upload')")
|
||||
@Operation(summary = "上传base64文件")
|
||||
@PostMapping("/upload/base64")
|
||||
|
||||
Reference in New Issue
Block a user