refactor: 优化代码重复和依赖导入
- ♻️ buildS3Config 复用 OssStorageClient.buildConfig (DRY 原则) - ♻️ 删除重复的 crypto 导入(3处) - ✅ 提高代码可维护性:配置逻辑只需在一处维护 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2716,45 +2716,12 @@ app.get('/api/files/download-url', authMiddleware, async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 辅助函数:构建 S3 配置(与 OssStorageClient.buildConfig 保持一致)
|
// 辅助函数:构建 S3 配置(复用 OssStorageClient.buildConfig)
|
||||||
function buildS3Config(user) {
|
function buildS3Config(user) {
|
||||||
const config = {
|
// 创建临时 OssStorageClient 实例并复用其 buildConfig 方法
|
||||||
region: user.oss_region || 'us-east-1',
|
const { OssStorageClient } = require('./storage');
|
||||||
credentials: {
|
const tempClient = new OssStorageClient(user);
|
||||||
accessKeyId: user.oss_access_key_id,
|
return tempClient.buildConfig();
|
||||||
secretAccessKey: user.oss_access_key_secret
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 阿里云 OSS
|
|
||||||
if (user.oss_provider === 'aliyun') {
|
|
||||||
config.region = user.oss_region || 'oss-cn-hangzhou';
|
|
||||||
if (user.oss_endpoint) {
|
|
||||||
config.endpoint = user.oss_endpoint;
|
|
||||||
} else {
|
|
||||||
// 默认 endpoint 格式:https://oss-{region}.aliyuncs.com
|
|
||||||
config.endpoint = `https://oss-${config.region.replace('oss-', '')}.aliyuncs.com`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 腾讯云 COS
|
|
||||||
else if (user.oss_provider === 'tencent') {
|
|
||||||
config.region = user.oss_region || 'ap-guangzhou';
|
|
||||||
if (user.oss_endpoint) {
|
|
||||||
config.endpoint = user.oss_endpoint;
|
|
||||||
} else {
|
|
||||||
// 默认 endpoint 格式:https://cos.{region}.myqcloud.com
|
|
||||||
config.endpoint = `https://cos.${config.region}.myqcloud.com`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// AWS S3 或其他兼容服务
|
|
||||||
else {
|
|
||||||
if (user.oss_endpoint) {
|
|
||||||
config.endpoint = user.oss_endpoint;
|
|
||||||
}
|
|
||||||
// AWS 使用默认 endpoint,无需额外配置
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 辅助函数:清理文件名
|
// 辅助函数:清理文件名
|
||||||
@@ -2966,7 +2933,6 @@ app.get('/api/files/download', authMiddleware, async (req, res) => {
|
|||||||
app.post('/api/upload/generate-tool', authMiddleware, async (req, res) => {
|
app.post('/api/upload/generate-tool', authMiddleware, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// 生成新的API密钥(32位随机字符串)
|
// 生成新的API密钥(32位随机字符串)
|
||||||
const crypto = require('crypto');
|
|
||||||
const newApiKey = crypto.randomBytes(16).toString('hex');
|
const newApiKey = crypto.randomBytes(16).toString('hex');
|
||||||
|
|
||||||
// 更新用户的upload_api_key
|
// 更新用户的upload_api_key
|
||||||
@@ -3001,7 +2967,6 @@ app.get('/api/upload/download-tool', authMiddleware, async (req, res) => {
|
|||||||
console.log(`[上传工具] 用户 ${req.user.username} 请求下载上传工具`);
|
console.log(`[上传工具] 用户 ${req.user.username} 请求下载上传工具`);
|
||||||
|
|
||||||
// 生成新的API密钥
|
// 生成新的API密钥
|
||||||
const crypto = require('crypto');
|
|
||||||
const newApiKey = crypto.randomBytes(16).toString('hex');
|
const newApiKey = crypto.randomBytes(16).toString('hex');
|
||||||
|
|
||||||
// 更新用户的upload_api_key
|
// 更新用户的upload_api_key
|
||||||
|
|||||||
Reference in New Issue
Block a user