refactor(file): 重构文件路径处理逻辑

- 移除生产环境中的 /file 前缀
- 统一文件上传和访问路径
- 修复历史路径格式的兼容性问题
-优化路径拼接逻辑,提高代码可读性和维护性
This commit is contained in:
2025-09-09 17:29:14 +08:00
parent c96bc5efea
commit bda7887980
9 changed files with 84 additions and 60 deletions

View File

@@ -66,7 +66,7 @@ public class CertificateProperties {
/**
* 生产环境基础路径
*/
private String prodBasePath = "/file";
private String prodBasePath = "/";
/**
* 微信支付证书目录名
@@ -155,19 +155,23 @@ public class CertificateProperties {
* @return 完整路径
*/
public String getWechatPayCertPath(String fileName) {
// 生产环境特殊处理:数据库中存储的路径需要拼接到 /file/ 目录下
// 生产环境特殊处理:数据库中存储的路径直接拼接到根路径
if (loadMode == LoadMode.VOLUME) {
// 修复路径拼接逻辑:数据库中存储的路径如果已经包含 /file则直接拼接
// 生产环境已经没有/file目录所有路径都直接拼接到根路径
// 处理数据库中可能存在的历史路径格式
String cleanFileName = fileName;
if (fileName.startsWith("/file/")) {
// 路径已经包含 /file/ 前缀,直接拼接到根路径
return certRootPath + fileName;
// 去掉历史的 /file/ 前缀
cleanFileName = fileName.substring(6);
} else if (fileName.startsWith("file/")) {
// 路径包含 file/ 前缀,添加根路径和斜杠
return certRootPath + "/" + fileName;
} else {
// 路径不包含 file 前缀,添加完整的 /file/ 前缀
return certRootPath + "/file/" + fileName;
// 去掉历史的 file/ 前缀
cleanFileName = fileName.substring(5);
}
// 确保路径以 / 开头
if (!cleanFileName.startsWith("/")) {
cleanFileName = "/" + cleanFileName;
}
return certRootPath + cleanFileName;
} else {
// 开发环境和文件系统模式使用原有逻辑
return getCertificatePath(wechatPay.getCertDir(), fileName);

View File

@@ -64,7 +64,7 @@ public class MyQrCodeUtil {
* @return 二维码图片地址
*/
public static String createQrCode(String type,Integer id, String content) throws IOException {
String filePath = uploadPath + "/file/qrcode/".concat(type).concat("/");
String filePath = uploadPath + "/qrcode/".concat(type).concat("/");
String qrcodeUrl = "https://file.websoft.top/qrcode/".concat(type).concat("/");
// 将URL转为BufferedImage
BufferedImage bufferedImage = ImageIO.read(new URL(logoUrl));

View File

@@ -265,14 +265,14 @@ public class FileController extends BaseController {
* 文件上传基目录
*/
private String getUploadBaseDir() {
return config.getUploadPath() + "file/";
return config.getUploadPath();
}
/**
* 文件上传位置(服务器)
*/
private String getUploadDir() {
return config.getUploadPath() + "file/";
return config.getUploadPath();
}
/**

View File

@@ -574,7 +574,7 @@ public class WxLoginController extends BaseController {
* 文件上传位置(服务器)
*/
private String getUploadDir() {
return config.getUploadPath() + "file/";
return config.getUploadPath();
}
@Operation(summary = "调试:检查微信小程序配置")

View File

@@ -191,7 +191,7 @@ public class HouseInfoServiceImpl extends ServiceImpl<HouseInfoMapper, HouseInfo
combiner.combine();
// 创建保存目录
String posterDir = uploadPath + "/file/poster/" + houseInfo.getTenantId() + "/house";
String posterDir = uploadPath + "/poster/" + houseInfo.getTenantId() + "/house";
if (!FileUtil.exist(posterDir)) {
FileUtil.mkdir(posterDir);
}
@@ -318,7 +318,7 @@ public class HouseInfoServiceImpl extends ServiceImpl<HouseInfoMapper, HouseInfo
* 文件上传位置(服务器)
*/
private String getUploadDir() {
return config.getUploadPath() + "file/";
return config.getUploadPath();
}
}

View File

@@ -252,7 +252,7 @@ public class OaAppController extends BaseController {
// 读取项目附件链式构建GET请求
HashMap<String, Object> map = new HashMap<>();
map.put("appId", d.getAppId());
final String build = UrlBuilder.of(configProperties.getServerUrl() + "/file/page").setQuery(new UrlQuery(map)).build();
final String build = UrlBuilder.of(configProperties.getServerUrl() + "/page").setQuery(new UrlQuery(map)).build();
String response = HttpRequest.get(build)
.header("Authorization", param.getToken())
.header("Tenantid", d.getTenantId().toString())

View File

@@ -207,9 +207,9 @@ public class ShopGiftController extends BaseController {
@Operation(summary = "导出礼品卡")
@PostMapping("/export")
public ApiResult<?> export(@RequestBody(required = false) List<Integer> ids) throws IOException {
String filename = "file/excel/礼品卡.xlsx";
if (!FileUtil.exist(uploadPath + "file/excel")) {
FileUtil.mkdir(uploadPath + "file/excel");
String filename = "excel/礼品卡.xlsx";
if (!FileUtil.exist(uploadPath + "excel")) {
FileUtil.mkdir(uploadPath + "excel");
}
List<ShopGift> list;
if (ids != null && !ids.isEmpty()) {

View File

@@ -373,18 +373,22 @@ public class ShopOrderController extends BaseController {
String privateKeyRelativePath = payment.getApiclientKey();
logger.info("数据库中的私钥相对路径: {}", privateKeyRelativePath);
// 修复路径拼接逻辑:数据库中存储的路径如果已经包含 /file则直接拼接
// 生产环境已经没有/file目录所有路径都直接拼接到根路径
String privateKeyFullPath;
// 处理数据库中可能存在的历史路径格式
String cleanPath = privateKeyRelativePath;
if (privateKeyRelativePath.startsWith("/file/")) {
// 路径已经包含 /file/ 前缀,直接拼接到根路径
privateKeyFullPath = certRootPath + privateKeyRelativePath;
// 去掉历史的 /file/ 前缀
cleanPath = privateKeyRelativePath.substring(6);
} else if (privateKeyRelativePath.startsWith("file/")) {
// 路径包含 file/ 前缀,添加根路径和斜杠
privateKeyFullPath = certRootPath + "/" + privateKeyRelativePath;
} else {
// 路径不包含 file 前缀,添加完整的 /file/ 前缀
privateKeyFullPath = certRootPath + "/file/" + privateKeyRelativePath;
// 去掉历史的 file/ 前缀
cleanPath = privateKeyRelativePath.substring(5);
}
// 确保路径以 / 开头
if (!cleanPath.startsWith("/")) {
cleanPath = "/" + cleanPath;
}
privateKeyFullPath = certRootPath + cleanPath;
logger.info("生产环境私钥完整路径: {}", privateKeyFullPath);
String privateKey = certificateLoader.loadCertificatePath(privateKeyFullPath);

View File

@@ -774,18 +774,22 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
String privateKeyRelativePath = payment.getApiclientKey();
System.out.println("数据库中的私钥相对路径: " + privateKeyRelativePath);
// 修复路径拼接逻辑:数据库中存储的路径如果已经包含 /file则直接拼接
// 生产环境已经没有/file目录所有路径都直接拼接到根路径
String privateKeyFullPath;
// 处理数据库中可能存在的历史路径格式
String cleanPath = privateKeyRelativePath;
if (privateKeyRelativePath.startsWith("/file/")) {
// 路径已经包含 /file/ 前缀,直接拼接到根路径
privateKeyFullPath = certRootPath + privateKeyRelativePath;
// 去掉历史的 /file/ 前缀
cleanPath = privateKeyRelativePath.substring(6);
} else if (privateKeyRelativePath.startsWith("file/")) {
// 路径包含 file/ 前缀,添加根路径和斜杠
privateKeyFullPath = certRootPath + "/" + privateKeyRelativePath;
} else {
// 路径不包含 file 前缀,添加完整的 /file/ 前缀
privateKeyFullPath = certRootPath + "/file/" + privateKeyRelativePath;
// 去掉历史的 file/ 前缀
cleanPath = privateKeyRelativePath.substring(5);
}
// 确保路径以 / 开头
if (!cleanPath.startsWith("/")) {
cleanPath = "/" + cleanPath;
}
privateKeyFullPath = certRootPath + cleanPath;
System.out.println("生产环境私钥完整路径: " + privateKeyFullPath);
privateKey = certificateLoader.loadCertificatePath(privateKeyFullPath);
@@ -804,18 +808,22 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
String pubKeyRelativePath = payment.getPubKey();
System.out.println("数据库中的公钥相对路径: " + pubKeyRelativePath);
// 修复公钥路径拼接逻辑,与私钥路径处理保持一致
// 生产环境已经没有/file目录所有路径都直接拼接到根路径
String pubKeyFullPath;
// 处理数据库中可能存在的历史路径格式
String cleanPubPath = pubKeyRelativePath;
if (pubKeyRelativePath.startsWith("/file/")) {
// 路径已经包含 /file/ 前缀,直接拼接到根路径
pubKeyFullPath = certRootPath + pubKeyRelativePath;
// 去掉历史的 /file/ 前缀
cleanPubPath = pubKeyRelativePath.substring(6);
} else if (pubKeyRelativePath.startsWith("file/")) {
// 路径包含 file/ 前缀,添加根路径和斜杠
pubKeyFullPath = certRootPath + "/" + pubKeyRelativePath;
} else {
// 路径不包含 file 前缀,添加完整的 /file/ 前缀
pubKeyFullPath = certRootPath + "/file/" + pubKeyRelativePath;
// 去掉历史的 file/ 前缀
cleanPubPath = pubKeyRelativePath.substring(5);
}
// 确保路径以 / 开头
if (!cleanPubPath.startsWith("/")) {
cleanPubPath = "/" + cleanPubPath;
}
pubKeyFullPath = certRootPath + cleanPubPath;
System.out.println("生产环境公钥完整路径: " + pubKeyFullPath);
pubKeyFile = certificateLoader.loadCertificatePath(pubKeyFullPath);
@@ -977,18 +985,22 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
String privateKeyRelativePath = payment.getApiclientKey();
// 修复路径拼接逻辑:数据库中存储的路径如果已经包含 /file则直接拼接
// 生产环境已经没有/file目录所有路径都直接拼接到根路径
String privateKeyFullPath;
// 处理数据库中可能存在的历史路径格式
String cleanPath2 = privateKeyRelativePath;
if (privateKeyRelativePath.startsWith("/file/")) {
// 路径已经包含 /file/ 前缀,直接拼接到根路径
privateKeyFullPath = certRootPath + privateKeyRelativePath;
// 去掉历史的 /file/ 前缀
cleanPath2 = privateKeyRelativePath.substring(6);
} else if (privateKeyRelativePath.startsWith("file/")) {
// 路径包含 file/ 前缀,添加根路径和斜杠
privateKeyFullPath = certRootPath + "/" + privateKeyRelativePath;
} else {
// 路径不包含 file 前缀,添加完整的 /file/ 前缀
privateKeyFullPath = certRootPath + "/file/" + privateKeyRelativePath;
// 去掉历史的 file/ 前缀
cleanPath2 = privateKeyRelativePath.substring(5);
}
// 确保路径以 / 开头
if (!cleanPath2.startsWith("/")) {
cleanPath2 = "/" + cleanPath2;
}
privateKeyFullPath = certRootPath + cleanPath2;
System.out.println("私钥完整路径: " + privateKeyFullPath);
privateKey = certificateLoader.loadCertificatePath(privateKeyFullPath);
@@ -1000,18 +1012,22 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
String pubKeyRelativePath = payment.getPubKey();
System.out.println("数据库中的公钥相对路径: " + pubKeyRelativePath);
// 修复公钥路径拼接逻辑,与私钥路径处理保持一致
// 生产环境已经没有/file目录所有路径都直接拼接到根路径
String pubKeyFullPath;
// 处理数据库中可能存在的历史路径格式
String cleanPubPath2 = pubKeyRelativePath;
if (pubKeyRelativePath.startsWith("/file/")) {
// 路径已经包含 /file/ 前缀,直接拼接到根路径
pubKeyFullPath = certRootPath + pubKeyRelativePath;
// 去掉历史的 /file/ 前缀
cleanPubPath2 = pubKeyRelativePath.substring(6);
} else if (pubKeyRelativePath.startsWith("file/")) {
// 路径包含 file/ 前缀,添加根路径和斜杠
pubKeyFullPath = certRootPath + "/" + pubKeyRelativePath;
} else {
// 路径不包含 file 前缀,添加完整的 /file/ 前缀
pubKeyFullPath = certRootPath + "/file/" + pubKeyRelativePath;
// 去掉历史的 file/ 前缀
cleanPubPath2 = pubKeyRelativePath.substring(5);
}
// 确保路径以 / 开头
if (!cleanPubPath2.startsWith("/")) {
cleanPubPath2 = "/" + cleanPubPath2;
}
pubKeyFullPath = certRootPath + cleanPubPath2;
System.out.println("生产环境公钥完整路径: " + pubKeyFullPath);
pubKey = certificateLoader.loadCertificatePath(pubKeyFullPath);