fix(system/file): 修复上传文件不指定 parentPath 默认上级目录不自动创建的问题

This commit is contained in:
2025-07-30 20:30:51 +08:00
parent e4f4554eef
commit 7ad12effae
3 changed files with 8 additions and 11 deletions

View File

@@ -60,7 +60,7 @@ public class FileController extends BaseController<FileService, FileResp, FileRe
/** /**
* 上传文件 * 上传文件
* <p> * <p>
* 公共上传文件请使用 {@link top.continew.admin.controller.common.CommonController#upload} * 公共上传文件请使用 {@link CommonController#upload}
* </p> * </p>
* *
* @param file 文件 * @param file 文件
@@ -69,7 +69,7 @@ public class FileController extends BaseController<FileService, FileResp, FileRe
* @throws IOException / * @throws IOException /
*/ */
@Operation(summary = "上传文件", description = "上传文件") @Operation(summary = "上传文件", description = "上传文件")
@Parameter(name = "parentPath", description = "上级目录", example = "/", in = ParameterIn.QUERY) @Parameter(name = "parentPath", description = "上级目录(默认:/yyyy/MM/dd", example = "/", in = ParameterIn.QUERY)
@SaCheckPermission("system:file:upload") @SaCheckPermission("system:file:upload")
@PostMapping("/upload") @PostMapping("/upload")
public FileUploadResp upload(@NotNull(message = "文件不能为空") @RequestPart MultipartFile file, public FileUploadResp upload(@NotNull(message = "文件不能为空") @RequestPart MultipartFile file,

View File

@@ -16,6 +16,7 @@
package top.continew.admin.system.service; package top.continew.admin.system.service;
import cn.hutool.core.util.StrUtil;
import org.dromara.x.file.storage.core.FileInfo; import org.dromara.x.file.storage.core.FileInfo;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.common.base.service.BaseService; import top.continew.admin.common.base.service.BaseService;
@@ -60,7 +61,7 @@ public interface FileService extends BaseService<FileResp, FileResp, FileQuery,
* @throws IOException / * @throws IOException /
*/ */
default FileInfo upload(MultipartFile file, String parentPath) throws IOException { default FileInfo upload(MultipartFile file, String parentPath) throws IOException {
return upload(file, parentPath, null); return upload(file, StrUtil.blankToDefault(parentPath, getDefaultParentPath()), null);
} }
/** /**
@@ -94,7 +95,7 @@ public interface FileService extends BaseService<FileResp, FileResp, FileQuery,
* @throws IOException / * @throws IOException /
*/ */
default FileInfo upload(File file, String parentPath) throws IOException { default FileInfo upload(File file, String parentPath) throws IOException {
return upload(file, parentPath, null); return upload(file, StrUtil.blankToDefault(parentPath, getDefaultParentPath()), null);
} }
/** /**

View File

@@ -252,10 +252,9 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
* 处理路径 * 处理路径
* *
* <p> * <p>
* 1.如果 path 为空,则使用 {@link FileService#getDefaultParentPath()} 作为默认值 <br /> * 1.如果 path 为 {@code /},则设置为空 <br />
* 2.如果 path {@code /},则设置为空 <br /> * 2.如果 path 不以 {@code /} 结尾,则添加后缀 {@code /} <br />
* 3.如果 path 以 {@code /} 结尾,则添加后缀 {@code /} <br /> * 3.如果 path 以 {@code /} 开头,则移除前缀 {@code /} <br />
* 4.如果 path 以 {@code /} 开头,则移除前缀 {@code /} <br />
* 示例yyyy/MM/dd/ * 示例yyyy/MM/dd/
* </p> * </p>
* *
@@ -263,9 +262,6 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
* @return 处理路径 * @return 处理路径
*/ */
private String pretreatmentPath(String path) { private String pretreatmentPath(String path) {
if (StrUtil.isBlank(path)) {
return this.getDefaultParentPath();
}
if (StringConstants.SLASH.equals(path)) { if (StringConstants.SLASH.equals(path)) {
return StringConstants.EMPTY; return StringConstants.EMPTY;
} }