feat(infra): 存储器自定义url可控
This commit is contained in:
1 parent
c18fdcbad2
commit
0e117875c7
9 files changed
+78
-5
No files matched your search
+18
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -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
|
||||
|
||||
+8
@@ -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;
|
||||
|
||||
/**
|
||||
* 主机地址
|
||||
*/
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+8
@@ -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;
|
||||
|
||||
}
|
||||
+1
-1
@@ -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
|
||||
|
||||
+8
@@ -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;
|
||||
|
||||
/**
|
||||
* 主机地址
|
||||
*/
|
||||
|
||||
@@ -12,6 +12,7 @@ export interface FileClientConfig {
|
||||
accessKey?: string
|
||||
accessSecret?: string
|
||||
domain: string
|
||||
addPrefix?: boolean
|
||||
}
|
||||
|
||||
export interface FileConfigVO {
|
||||
|
||||
@@ -91,6 +91,22 @@
|
||||
<el-form-item v-else-if="formData.storage" label="自定义域名" prop="config.domain">
|
||||
<el-input v-model="formData.config.domain" placeholder="请输入自定义域名" />
|
||||
</el-form-item>
|
||||
<!-- Local / FTP / SFTP:是否添加 admin-api/infra/file/{id}/get 前缀 -->
|
||||
<el-form-item
|
||||
v-if="formData.storage >= 10 && formData.storage <= 12"
|
||||
label="添加访问前缀"
|
||||
prop="config.addPrefix"
|
||||
>
|
||||
<el-switch
|
||||
v-model="formData.config.addPrefix"
|
||||
active-text="通过后端转发"
|
||||
inactive-text="直连域名"
|
||||
/>
|
||||
<div class="form-tip">
|
||||
开启后返回 {域名}/admin-api/infra/file/{配置ID}/get/{文件路径},由后端转发获取文件;
|
||||
关闭后返回 {域名}/{文件路径},适用于自定义域名直接指向文件(如 SFTP 上传到 Web 根目录)。
|
||||
</div>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<template #footer>
|
||||
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
|
||||
@@ -117,7 +133,7 @@ const formData = ref({
|
||||
name: '',
|
||||
storage: 0,
|
||||
remark: '',
|
||||
config: {} as FileConfigApi.FileClientConfig
|
||||
config: { addPrefix: true } as FileConfigApi.FileClientConfig
|
||||
})
|
||||
const formRules = reactive<FormRules>({
|
||||
name: [{ required: true, message: '配置名不能为空', trigger: 'blur' }],
|
||||
@@ -149,6 +165,10 @@ const open = async (type: string, id?: number) => {
|
||||
formLoading.value = true
|
||||
try {
|
||||
formData.value = await FileConfigApi.getFileConfig(id)
|
||||
// 兼容旧配置:addPrefix 缺省默认为 true(通过后端转发)
|
||||
if (formData.value.config && formData.value.config.addPrefix === undefined) {
|
||||
formData.value.config.addPrefix = true
|
||||
}
|
||||
} finally {
|
||||
formLoading.value = false
|
||||
}
|
||||
@@ -189,8 +209,18 @@ const resetForm = () => {
|
||||
name: '',
|
||||
storage: undefined!,
|
||||
remark: '',
|
||||
config: {} as FileConfigApi.FileClientConfig
|
||||
config: { addPrefix: true } as FileConfigApi.FileClientConfig
|
||||
}
|
||||
formRef.value?.resetFields()
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.form-tip {
|
||||
width: 100%;
|
||||
margin-top: 4px;
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
color: var(--el-text-color-secondary);
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user