perf: 优化本地文件上传,配置绝对路径

This commit is contained in:
ruying408
2025-02-05 23:28:38 +08:00
parent 3d03f7140a
commit 49f37d2558
5 changed files with 33 additions and 18 deletions

View File

@@ -13,6 +13,7 @@ import cn.hutool.json.JSONUtil;
import com.cool.core.enums.QueryModeEnum; import com.cool.core.enums.QueryModeEnum;
import com.cool.core.exception.CoolPreconditions; import com.cool.core.exception.CoolPreconditions;
import com.cool.core.request.CrudOption; import com.cool.core.request.CrudOption;
import com.cool.core.request.PageResult;
import com.cool.core.request.R; import com.cool.core.request.R;
import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
@@ -217,14 +218,14 @@ public abstract class BaseController<S extends BaseService<T>, T extends BaseEnt
* *
* @param page 分页返回数据 * @param page 分页返回数据
*/ */
protected Map<String, Object> pageResult(Page page) { protected PageResult pageResult(Page page) {
Map<String, Object> result = new HashMap<>(); PageResult result = new PageResult();
Map<String, Object> pagination = new HashMap<>(); Map<String, Object> pagination = new HashMap<>();
pagination.put("size", page.getPageSize()); pagination.put("size", page.getPageSize());
pagination.put("page", page.getPageNumber()); pagination.put("page", page.getPageNumber());
pagination.put("total", page.getTotalRow()); pagination.put("total", page.getTotalRow());
result.put("list", page.getRecords()); result.setList(page.getRecords());
result.put("pagination", pagination); result.setPagination(pagination);
return result; return result;
} }

View File

@@ -1,5 +1,7 @@
package com.cool.core.config; package com.cool.core.config;
import com.cool.core.util.PathUtils;
import java.io.File;
import lombok.Data; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@@ -18,6 +20,11 @@ public class LocalFileProperties {
private String uploadPath = "assets/public/upload"; private String uploadPath = "assets/public/upload";
public String getAbsoluteUploadFolder() { public String getAbsoluteUploadFolder() {
return System.getProperty("user.dir") + "/" + uploadPath; if (!PathUtils.isAbsolutePath(uploadPath)) {
// 相对路径
return System.getProperty("user.dir") + File.separator + uploadPath;
}
// 绝对路径
return uploadPath;
} }
} }

View File

@@ -34,26 +34,23 @@ public class LocalFileUploadStrategy implements FileUploadStrategy {
*/ */
@Override @Override
public Object upload(MultipartFile[] files, HttpServletRequest request, public Object upload(MultipartFile[] files, HttpServletRequest request,
PluginInfoEntity pluginInfoEntity) { PluginInfoEntity pluginInfoEntity) {
CoolPreconditions.check(StrUtil.isEmpty(localFileProperties.getBaseUrl()), CoolPreconditions.check(StrUtil.isEmpty(localFileProperties.getBaseUrl()),
"filePath 或 baseUrl 未配置"); "filePath 或 baseUrl 未配置");
try { try {
List<String> fileUrls = new ArrayList<>(); List<String> fileUrls = new ArrayList<>();
String baseUrl = localFileProperties.getBaseUrl(); String baseUrl = localFileProperties.getBaseUrl();
// 绝对路径
String filePath = System.getProperty("user.dir");
String date = DateUtil.format(new Date(), String date = DateUtil.format(new Date(),
DatePattern.PURE_DATE_PATTERN); DatePattern.PURE_DATE_PATTERN);
String relativePath = String absoluteUploadFolder = localFileProperties.getAbsoluteUploadFolder();
File.separator + localFileProperties.getUploadPath() + File.separator + date; String fullPath = absoluteUploadFolder + File.separator + date;
FileUtil.mkdir(fullPath);
FileUtil.mkdir(filePath + relativePath);
for (MultipartFile file : files) { for (MultipartFile file : files) {
// 保存文件 // 保存文件
String fileName = StrUtil.uuid().replaceAll("-", "") + getExtensionName( String fileName = StrUtil.uuid().replaceAll("-", "") + getExtensionName(
Objects.requireNonNull(file.getOriginalFilename())); Objects.requireNonNull(file.getOriginalFilename()));
file.transferTo(new File(filePath + File.separator + relativePath file.transferTo(new File(fullPath
+ File.separator + fileName)); + File.separator + fileName));
fileUrls.add(baseUrl + File.separator + date + File.separator + fileName); fileUrls.add(baseUrl + File.separator + date + File.separator + fileName);
} }
if (fileUrls.size() == 1) { if (fileUrls.size() == 1) {

View File

@@ -0,0 +1,11 @@
package com.cool.core.request;
import java.util.List;
import java.util.Map;
import lombok.Data;
@Data
public class PageResult {
private List<?> list;
private Map<String, Object> pagination;
}

View File

@@ -1,4 +1,3 @@
tip: 凡购买了【商城项目】和【Ai流程编排+知识库项目】这两个Java版本插件的用户可向cool-admin-java版本开发者提供支付记录免费获取[cool-admin-java-plus](https://gitee.com/hlc4417/cool-admin-java-plus)版本。
______ ___ ___ _____ _ ______ ____ ____ _____ ____ _____ ______ ___ ___ _____ _ ______ ____ ____ _____ ____ _____
.' ___ | .' `. .' `.|_ _| V7.x / \ |_ _ `.|_ \ / _||_ _||_ \|_ _| .' ___ | .' `. .' `.|_ _| V7.x / \ |_ _ `.|_ \ / _||_ _||_ \|_ _|
/ .' \_|/ .-. \/ .-. \ | | ______ / _ \ | | `. \ | \/ | | | | \ | | / .' \_|/ .-. \/ .-. \ | | ______ / _ \ | | `. \ | \/ | | | | \ | |