feat(infra): 存储器自定义url可控

This commit is contained in:
Kroytz committed 2026-07-30 16:52:04 +08:00
1 parent c18fdcbad2
commit 0e117875c7
9 files changed
+78 -5

No files matched your search

@@ -66,4 +66,22 @@ public abstract class AbstractFileClient<Config extends FileClientConfig> implem
return StrUtil.format("{}/admin-api/infra/file/{}/get/{}", domain, getId(), path);
}
/**
* 格式化文件的 URL 访问地址,可选是否拼接“admin-api/infra/file/{id}/get” 前缀
*
* 部分场景(例如 SFTP/FTP 直接将文件上传到 Web 根目录),自定义域名直接指向文件,
* 无需再经过后端 FileController 转发,此时应关闭前缀,仅返回 {domain}/{path}。
*
* @param domain 自定义域名
* @param path 文件路径
* @param addPrefix 是否添加“admin-api/infra/file/{id}/get”前缀
* @return URL 访问地址
*/
protected String formatFileUrl(String domain, String path, Boolean addPrefix) {
if (Boolean.FALSE.equals(addPrefix)) {
return StrUtil.format("{}/{}", domain, path);
}
return formatFileUrl(domain, path);
}
}
@@ -49,7 +49,7 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> {
throw new FtpException(StrUtil.format("上传文件到目标目录 ({}) 失败", filePath));
}
// 拼接返回路径
return super.formatFileUrl(config.getDomain(), path);
return super.formatFileUrl(config.getDomain(), path, config.getAddPrefix());
}
@Override
@@ -28,6 +28,14 @@ public class FtpFileClientConfig implements FileClientConfig {
@URL(message = "domain 必须是 URL 格式")
private String domain;
/**
* 是否添加“admin-api/infra/file/{id}/get”前缀
*
* 当自定义域名直接指向文件时,应设为 false,此时返回 {domain}/{path};
* 否则默认 true,通过后端 FileController 转发获取文件。
*/
private Boolean addPrefix = true;
/**
* 主机地址
*/
@@ -30,7 +30,7 @@ public class LocalFileClient extends AbstractFileClient<LocalFileClientConfig> {
String filePath = getFilePath(path);
FileUtil.writeBytes(content, filePath);
// 拼接返回路径
return super.formatFileUrl(config.getDomain(), path);
return super.formatFileUrl(config.getDomain(), path, config.getAddPrefix());
}
@Override
@@ -27,4 +27,12 @@ public class LocalFileClientConfig implements FileClientConfig {
@URL(message = "domain 必须是 URL 格式")
private String domain;
/**
* 是否添加“admin-api/infra/file/{id}/get”前缀
*
* 当自定义域名直接指向文件时,应设为 false,此时返回 {domain}/{path};
* 否则默认 true,通过后端 FileController 转发获取文件。
*/
private Boolean addPrefix = true;
}
@@ -37,7 +37,7 @@ public class SftpFileClient extends AbstractFileClient<SftpFileClientConfig> {
File file = FileUtils.createTempFile(content);
sftp.upload(filePath, file);
// 拼接返回路径
return super.formatFileUrl(config.getDomain(), path);
return super.formatFileUrl(config.getDomain(), path, config.getAddPrefix());
}
@Override
@@ -28,6 +28,14 @@ public class SftpFileClientConfig implements FileClientConfig {
@URL(message = "domain 必须是 URL 格式")
private String domain;
/**
* 是否添加“admin-api/infra/file/{id}/get”前缀
*
* 当自定义域名直接指向文件(例如 SFTP 上传到 Web 根目录)时,应设为 false,
* 此时返回 {domain}/{path};否则默认 true,通过后端 FileController 转发获取文件。
*/
private Boolean addPrefix = true;
/**
* 主机地址
*/