mirror of
https://github.com/ZSCGR/CloudFlare-ImgBed.git
synced 2026-08-22 08:33:42 +08:00
146 lines
4.0 KiB
JavaScript
146 lines
4.0 KiB
JavaScript
/**
|
|
* 渠道凭据解析工具
|
|
* 负责从当前渠道配置解析读取、删除、移动等操作需要的真实凭据
|
|
*/
|
|
import { findConfiguredChannel, loadChannelConfig } from './channelConfig.js';
|
|
import { normalizeWebDAVHeaders } from '../storage/webdavAPI.js';
|
|
|
|
/* ========== 主要函数 ========== */
|
|
|
|
// 解析 S3 渠道凭据,并保留文件对象 key
|
|
export async function resolveS3Credentials(db, env, metadata = {}) {
|
|
const channel = await loadConfiguredChannel(db, env, 's3', metadata);
|
|
if (channel) {
|
|
return {
|
|
source: 'config',
|
|
endpoint: channel.endpoint,
|
|
region: channel.region || 'auto',
|
|
bucketName: channel.bucketName,
|
|
pathStyle: channel.pathStyle || false,
|
|
accessKeyId: channel.accessKeyId,
|
|
secretAccessKey: channel.secretAccessKey,
|
|
cdnDomain: channel.cdnDomain || '',
|
|
key: metadata.S3FileKey,
|
|
};
|
|
}
|
|
|
|
return missingCredentials({
|
|
endpoint: '',
|
|
region: 'auto',
|
|
bucketName: '',
|
|
pathStyle: false,
|
|
accessKeyId: '',
|
|
secretAccessKey: '',
|
|
cdnDomain: '',
|
|
key: metadata.S3FileKey,
|
|
});
|
|
}
|
|
|
|
// 解析 Telegram 渠道凭据,并保留文件 ID
|
|
export async function resolveTelegramCredentials(db, env, metadata = {}) {
|
|
const channel = await loadConfiguredChannel(db, env, 'telegram', metadata);
|
|
if (channel) {
|
|
return {
|
|
source: 'config',
|
|
botToken: channel.botToken,
|
|
chatId: channel.chatId,
|
|
proxyUrl: channel.proxyUrl || '',
|
|
fileId: metadata.TgFileId,
|
|
};
|
|
}
|
|
|
|
return missingCredentials({
|
|
botToken: '',
|
|
chatId: '',
|
|
proxyUrl: '',
|
|
fileId: metadata.TgFileId,
|
|
});
|
|
}
|
|
|
|
// 解析 Discord 渠道凭据,并保留消息 ID
|
|
export async function resolveDiscordCredentials(db, env, metadata = {}) {
|
|
const channel = await loadConfiguredChannel(db, env, 'discord', metadata);
|
|
if (channel) {
|
|
return {
|
|
source: 'config',
|
|
botToken: channel.botToken,
|
|
channelId: channel.channelId,
|
|
proxyUrl: channel.proxyUrl || '',
|
|
messageId: metadata.DiscordMessageId,
|
|
};
|
|
}
|
|
|
|
return missingCredentials({
|
|
botToken: '',
|
|
channelId: '',
|
|
proxyUrl: '',
|
|
messageId: metadata.DiscordMessageId,
|
|
});
|
|
}
|
|
|
|
// 解析 HuggingFace 渠道凭据,并保留仓库内文件路径
|
|
export async function resolveHuggingFaceCredentials(db, env, metadata = {}) {
|
|
const channel = await loadConfiguredChannel(db, env, 'huggingface', metadata);
|
|
if (channel) {
|
|
return {
|
|
source: 'config',
|
|
token: channel.token,
|
|
repo: channel.repo,
|
|
isPrivate: channel.isPrivate || false,
|
|
filePath: metadata.HfFilePath,
|
|
};
|
|
}
|
|
|
|
return missingCredentials({
|
|
token: '',
|
|
repo: '',
|
|
isPrivate: false,
|
|
filePath: metadata.HfFilePath,
|
|
});
|
|
}
|
|
|
|
// 解析 WebDAV 渠道凭据,并规范化自定义请求头
|
|
export async function resolveWebDAVCredentials(db, env, metadata = {}) {
|
|
const channel = await loadConfiguredChannel(db, env, 'webdav', metadata);
|
|
if (channel) {
|
|
return {
|
|
source: 'config',
|
|
baseUrl: channel.baseUrl || '',
|
|
username: channel.username || '',
|
|
password: channel.password || '',
|
|
headers: normalizeWebDAVHeaders(channel.headers || {}),
|
|
createDirectory: channel.createDirectory !== false,
|
|
publicUrl: channel.publicUrl || '',
|
|
filePath: metadata.WebDAVFilePath,
|
|
};
|
|
}
|
|
|
|
return missingCredentials({
|
|
baseUrl: '',
|
|
username: '',
|
|
password: '',
|
|
headers: {},
|
|
createDirectory: true,
|
|
publicUrl: '',
|
|
filePath: metadata.WebDAVFilePath,
|
|
});
|
|
}
|
|
|
|
/* ========== 关键函数 ========== */
|
|
|
|
// 统一加载上传配置并定位 metadata 对应渠道
|
|
async function loadConfiguredChannel(db, env, groupName, metadata = {}) {
|
|
const uploadConfig = await loadChannelConfig(db, env, `${groupName} credentials`);
|
|
return findConfiguredChannel(uploadConfig, groupName, metadata);
|
|
}
|
|
|
|
/* ========== 工具函数 ========== */
|
|
|
|
// 返回稳定结构,调用方可根据 source 判断是否找到了可用配置
|
|
function missingCredentials(fields) {
|
|
return {
|
|
source: 'missing',
|
|
...fields,
|
|
};
|
|
}
|