组件文件获取接口支持域名可配

This commit is contained in:
gaoqr
2026-08-04 09:07:01 +08:00
parent 87e5ae247f
commit bc899eb693
4 changed files with 48 additions and 3 deletions
@@ -0,0 +1,19 @@
package com.cf.imes.module.infra.framework.file.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* 外部文件访问地址配置。
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "chenfeng.file")
public class ExternalFileProperties {
/**
* 拼接数据库外部文件路径的 CDN 域名。
*/
private String externalDomain = "https://cdn.fzcfkj.com/";
}
@@ -8,6 +8,7 @@ import com.cf.imes.framework.common.util.Assert.AssertUtils;
import com.cf.imes.framework.common.util.io.FileUtils; import com.cf.imes.framework.common.util.io.FileUtils;
import com.cf.imes.module.infra.framework.file.core.client.FileClient; import com.cf.imes.module.infra.framework.file.core.client.FileClient;
import com.cf.imes.module.infra.framework.file.core.utils.FileTypeUtils; import com.cf.imes.module.infra.framework.file.core.utils.FileTypeUtils;
import com.cf.imes.module.infra.framework.file.config.ExternalFileProperties;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX; import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.infra.controller.admin.file.vo.file.FilePageReqVO; import com.cf.imes.module.infra.controller.admin.file.vo.file.FilePageReqVO;
import com.cf.imes.module.infra.dal.dataobject.file.FileDO; import com.cf.imes.module.infra.dal.dataobject.file.FileDO;
@@ -40,6 +41,9 @@ public class FileServiceImpl implements FileService {
@Resource @Resource
private FileMapper fileMapper; private FileMapper fileMapper;
@Resource
private ExternalFileProperties externalFileProperties;
private static final String CLIENT_ERR_NOTIFICATION = "客户端({}) 不能为空"; private static final String CLIENT_ERR_NOTIFICATION = "客户端({}) 不能为空";
@Override @Override
@@ -200,7 +204,25 @@ public class FileServiceImpl implements FileService {
*/ */
@Override @Override
public String getExternalFileUrl(Long id) { public String getExternalFileUrl(Long id) {
return validateFileExists(id).getUrl(); String storedUrl = validateFileExists(id).getUrl();
return joinUrl(externalFileProperties.getExternalDomain(), storedUrl);
}
/**
* 拼接配置域名和数据库文件路径,并消除连接处的重复斜杠。
*/
private String joinUrl(String domain, String path) {
AssertUtils.notEmpty(domain, FILE_IS_EMPTY);
AssertUtils.notEmpty(path, FILE_IS_EMPTY);
int domainEnd = domain.length();
while (domainEnd > 0 && domain.charAt(domainEnd - 1) == '/') {
domainEnd--;
}
int pathStart = 0;
while (pathStart < path.length() && path.charAt(pathStart) == '/') {
pathStart++;
}
return domain.substring(0, domainEnd) + "/" + path.substring(pathStart);
} }
/** /**
@@ -126,6 +126,8 @@ spring:
# 晨丰配置项,设置当前项目所有自定义的配置 # 晨丰配置项,设置当前项目所有自定义的配置
chenfeng: chenfeng:
file:
external-domain: https://cdn.fzcfkj.com/
info: info:
version: 1.0.0 version: 1.0.0
base-package: com.cf.imes.module.infra base-package: com.cf.imes.module.infra
@@ -186,4 +188,4 @@ chenfeng:
mybatis-plus: mybatis-plus:
configuration: configuration:
log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl
@@ -144,6 +144,8 @@ logging:
# 晨丰配置项,设置当前项目所有自定义的配置 # 晨丰配置项,设置当前项目所有自定义的配置
chenfeng: chenfeng:
file:
external-domain: https://cdn.fzcfkj.com/
info: info:
version: 1.0.0 version: 1.0.0
base-package: com.cf.imes.module.infra base-package: com.cf.imes.module.infra
@@ -207,4 +209,4 @@ chenfeng:
enable: false enable: false
encrypt: encrypt:
enable: false enable: false
publicKey: cfimes publicKey: cfimes