Files
certd/packages/plugins/plugin-lib/src/oss/factory.ts
T

42 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-04-25 01:26:04 +08:00
import { OssClientContext } from "./api";
2025-01-03 00:12:15 +08:00
2025-04-25 01:26:04 +08:00
export class OssClientFactory {
2025-01-02 00:28:13 +08:00
async getClassByType(type: string) {
if (type === "alioss") {
2025-01-03 00:12:15 +08:00
const module = await import("./impls/alioss.js");
2025-04-25 01:26:04 +08:00
return module.default;
2025-01-02 00:28:13 +08:00
} else if (type === "ssh") {
2025-01-03 00:12:15 +08:00
const module = await import("./impls/ssh.js");
2025-04-25 01:26:04 +08:00
return module.default;
} else if (type === "sftp") {
const module = await import("./impls/sftp.js");
2025-04-25 01:26:04 +08:00
return module.default;
2025-01-02 00:28:13 +08:00
} else if (type === "ftp") {
2025-01-03 00:12:15 +08:00
const module = await import("./impls/ftp.js");
2025-04-25 01:26:04 +08:00
return module.default;
2025-01-02 00:28:13 +08:00
} else if (type === "tencentcos") {
2025-01-03 00:12:15 +08:00
const module = await import("./impls/tencentcos.js");
2025-04-25 01:26:04 +08:00
return module.default;
2025-01-02 00:28:13 +08:00
} else if (type === "qiniuoss") {
2025-01-03 00:12:15 +08:00
const module = await import("./impls/qiniuoss.js");
2025-04-25 01:26:04 +08:00
return module.default;
} else if (type === "s3") {
const module = await import("./impls/s3.js");
return module.default;
2025-01-02 00:28:13 +08:00
} else {
throw new Error(`暂不支持此文件上传方式: ${type}`);
}
}
2025-04-25 01:26:04 +08:00
async createOssClientByType(type: string, opts: { rootDir: string; access: any; ctx: OssClientContext }) {
2025-01-03 00:12:15 +08:00
const cls = await this.getClassByType(type);
2025-01-02 00:28:13 +08:00
if (cls) {
// @ts-ignore
2025-01-03 00:12:15 +08:00
const instance = new cls(opts);
await instance.setCtx(opts.ctx);
return instance;
2025-01-02 00:28:13 +08:00
}
}
}
2025-04-25 01:26:04 +08:00
export const ossClientFactory = new OssClientFactory();