diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileConfigDO.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileConfigDO.java index 92d617e330..b8aa2fd11f 100755 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileConfigDO.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileConfigDO.java @@ -62,7 +62,7 @@ public class FileConfigDO extends BaseDO { private Boolean master; /** - * 支付渠道配置 + * 文件存储客户端配置 */ @TableField(typeHandler = FileClientConfigTypeHandler.class) private FileClientConfig config; diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/db/DBFileClient.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/db/DBFileClient.java index 0a050d3251..bd4312aea5 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/db/DBFileClient.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/db/DBFileClient.java @@ -33,6 +33,10 @@ public class DBFileClient extends AbstractFileClient { .setPath(path).setContent(content); fileContentMapper.insert(contentDO); // 拼接返回路径 + if (config.getUseRelativePath()){ + // 如果是 相对路径,则拼接相对路径前缀 + return super.formatFileUrl(config.getRelativePathPrefix(), path); + } return super.formatFileUrl(config.getDomain(), path); } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/db/DBFileClientConfig.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/db/DBFileClientConfig.java index 1f619df9af..8502d3f9a9 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/db/DBFileClientConfig.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/db/DBFileClientConfig.java @@ -1,10 +1,8 @@ package cn.iocoder.yudao.module.infra.framework.file.core.client.db; import cn.iocoder.yudao.module.infra.framework.file.core.client.FileClientConfig; +import jakarta.validation.constraints.NotNull; import lombok.Data; -import org.hibernate.validator.constraints.URL; - -import jakarta.validation.constraints.NotEmpty; /** * 基于 DB 存储的文件客户端的配置类 @@ -14,11 +12,18 @@ import jakarta.validation.constraints.NotEmpty; @Data public class DBFileClientConfig implements FileClientConfig { + @NotNull(message = "是否使用相对路径不能为空") + private Boolean useRelativePath = false; + + + private String relativePathPrefix = "/file"; + /** * 自定义域名 */ - @NotEmpty(message = "domain 不能为空") - @URL(message = "domain 必须是 URL 格式") + // TODO 王祁 20250515 这里看如何做校验?当useRelativePath为true时,只校验relativePath,否则只校验domain,还是说在前端做 + // @NotEmpty(message = "domain 不能为空") + // @URL(message = "domain 必须是 URL 格式") private String domain; } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/ftp/FtpFileClient.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/ftp/FtpFileClient.java index 4207eb7e15..2635e5e7ca 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/ftp/FtpFileClient.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/ftp/FtpFileClient.java @@ -43,6 +43,10 @@ public class FtpFileClient extends AbstractFileClient { throw new FtpException(StrUtil.format("上传文件到目标目录 ({}) 失败", filePath)); } // 拼接返回路径 + if (config.getUseRelativePath()){ + // 如果是 相对路径,则拼接相对路径前缀 + return super.formatFileUrl(config.getRelativePathPrefix(), path); + } return super.formatFileUrl(config.getDomain(), path); } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/ftp/FtpFileClientConfig.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/ftp/FtpFileClientConfig.java index a1b92ffc1c..839d017345 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/ftp/FtpFileClientConfig.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/ftp/FtpFileClientConfig.java @@ -21,11 +21,18 @@ public class FtpFileClientConfig implements FileClientConfig { @NotEmpty(message = "基础路径不能为空") private String basePath; + @NotNull(message = "是否使用相对路径不能为空") + private Boolean useRelativePath = false; + + + private String relativePathPrefix = "/file"; + /** * 自定义域名 */ - @NotEmpty(message = "domain 不能为空") - @URL(message = "domain 必须是 URL 格式") + // TODO 王祁 20250515 这里看如何做校验?当useRelativePath为true时,只校验relativePath,否则只校验domain,还是说在前端做 + // @NotEmpty(message = "domain 不能为空") + // @URL(message = "domain 必须是 URL 格式") private String domain; /** diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/local/LocalFileClient.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/local/LocalFileClient.java index 7fa2a7ea9a..64701f0e48 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/local/LocalFileClient.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/local/LocalFileClient.java @@ -26,6 +26,10 @@ public class LocalFileClient extends AbstractFileClient { String filePath = getFilePath(path); FileUtil.writeBytes(content, filePath); // 拼接返回路径 + if (config.getUseRelativePath()){ + // 如果是 相对路径,则拼接相对路径前缀 + return super.formatFileUrl(config.getRelativePathPrefix(), path); + } return super.formatFileUrl(config.getDomain(), path); } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/local/LocalFileClientConfig.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/local/LocalFileClientConfig.java index d56d4ef365..982c35a579 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/local/LocalFileClientConfig.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/local/LocalFileClientConfig.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.infra.framework.file.core.client.local; import cn.iocoder.yudao.module.infra.framework.file.core.client.FileClientConfig; +import jakarta.validation.constraints.NotNull; import lombok.Data; import org.hibernate.validator.constraints.URL; @@ -20,11 +21,18 @@ public class LocalFileClientConfig implements FileClientConfig { @NotEmpty(message = "基础路径不能为空") private String basePath; + @NotNull(message = "是否使用相对路径不能为空") + private Boolean useRelativePath = false; + + + private String relativePathPrefix = "/file"; + /** * 自定义域名 */ - @NotEmpty(message = "domain 不能为空") - @URL(message = "domain 必须是 URL 格式") + // TODO 王祁 20250515 这里看如何做校验?当useRelativePath为true时,只校验relativePath,否则只校验domain,还是说在前端做 + // @NotEmpty(message = "domain 不能为空") + // @URL(message = "domain 必须是 URL 格式") private String domain; } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/s3/S3FileClient.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/s3/S3FileClient.java index a33f0d738c..17ee19c2ea 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/s3/S3FileClient.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/s3/S3FileClient.java @@ -75,6 +75,10 @@ public class S3FileClient extends AbstractFileClient { // 上传文件 client.putObject(putRequest, RequestBody.fromBytes(content)); // 拼接返回路径 + if (config.getUseRelativePath()){ + // 如果是 相对路径,则拼接相对路径前缀 + return config.getRelativePathPrefix() + "/" + path; + } return config.getDomain() + "/" + path; } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/s3/S3FileClientConfig.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/s3/S3FileClientConfig.java index fb19317e02..13478fff41 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/s3/S3FileClientConfig.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/s3/S3FileClientConfig.java @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.infra.framework.file.core.client.s3; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.module.infra.framework.file.core.client.FileClientConfig; import com.fasterxml.jackson.annotation.JsonIgnore; +import jakarta.validation.constraints.NotEmpty; import lombok.Data; import org.hibernate.validator.constraints.URL; @@ -33,6 +34,13 @@ public class S3FileClientConfig implements FileClientConfig { */ @NotNull(message = "endpoint 不能为空") private String endpoint; + + @NotNull(message = "是否使用相对路径不能为空") + private Boolean useRelativePath = false; + + + private String relativePathPrefix = "/file"; + /** * 自定义域名 * 1. MinIO:通过 Nginx 配置 @@ -42,7 +50,9 @@ public class S3FileClientConfig implements FileClientConfig { * 5. 华为云:https://support.huaweicloud.com/usermanual-obs/obs_03_0032.html * 6. 火山云:https://www.volcengine.com/docs/6349/128983 */ - @URL(message = "domain 必须是 URL 格式") + // TODO 王祁 20250515 这里看如何做校验?当useRelativePath为true时,只校验relativePath,否则只校验domain,还是说在前端做 + // @NotEmpty(message = "domain 不能为空") + // @URL(message = "domain 必须是 URL 格式") private String domain; /** * 存储 Bucket diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/sftp/SftpFileClient.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/sftp/SftpFileClient.java index 000cbd10b3..ff7cd0c667 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/sftp/SftpFileClient.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/sftp/SftpFileClient.java @@ -35,6 +35,10 @@ public class SftpFileClient extends AbstractFileClient { sftp.mkDirs(FileUtil.getParent(filePath, 1)); // 需要创建父目录,不然会报错 sftp.upload(filePath, file); // 拼接返回路径 + if (config.getUseRelativePath()){ + // 如果是 相对路径,则拼接相对路径前缀 + return super.formatFileUrl(config.getRelativePathPrefix(), path); + } return super.formatFileUrl(config.getDomain(), path); } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/sftp/SftpFileClientConfig.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/sftp/SftpFileClientConfig.java index c56d045fef..f4e8186abd 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/sftp/SftpFileClientConfig.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/sftp/SftpFileClientConfig.java @@ -21,11 +21,17 @@ public class SftpFileClientConfig implements FileClientConfig { @NotEmpty(message = "基础路径不能为空") private String basePath; + @NotNull(message = "是否使用相对路径不能为空") + private Boolean useRelativePath = false; + + private String relativePathPrefix = "/file"; + /** * 自定义域名 */ - @NotEmpty(message = "domain 不能为空") - @URL(message = "domain 必须是 URL 格式") + // TODO 王祁 20250515 这里看如何做校验?当useRelativePath为true时,只校验relativePath,否则只校验domain,还是说在前端做 + // @NotEmpty(message = "domain 不能为空") + // @URL(message = "domain 必须是 URL 格式") private String domain; /**