diff --git a/packages/plugins/plugin-lib/src/oss/api.ts b/packages/plugins/plugin-lib/src/oss/api.ts index b311873c..d4458424 100644 --- a/packages/plugins/plugin-lib/src/oss/api.ts +++ b/packages/plugins/plugin-lib/src/oss/api.ts @@ -9,7 +9,6 @@ export type OssClientRemoveByOpts = { }; export type OssFileItem = { - name: string; path: string; size: number; //毫秒时间戳 diff --git a/packages/plugins/plugin-lib/src/oss/factory.ts b/packages/plugins/plugin-lib/src/oss/factory.ts index 33d78b1d..9bd128ed 100644 --- a/packages/plugins/plugin-lib/src/oss/factory.ts +++ b/packages/plugins/plugin-lib/src/oss/factory.ts @@ -27,7 +27,7 @@ export class OssClientFactory { throw new Error(`暂不支持此文件上传方式: ${type}`); } } - async createOssClientByType(type: string, opts: { rootDir: string; access: any; ctx: OssClientContext }) { + async createOssClientByType(type: string, opts: { rootDir?: string; access: any; ctx: OssClientContext }) { const cls = await this.getClassByType(type); if (cls) { // @ts-ignore diff --git a/packages/plugins/plugin-lib/src/oss/impls/tencentcos.ts b/packages/plugins/plugin-lib/src/oss/impls/tencentcos.ts index 5dad8de7..20fe9be2 100644 --- a/packages/plugins/plugin-lib/src/oss/impls/tencentcos.ts +++ b/packages/plugins/plugin-lib/src/oss/impls/tencentcos.ts @@ -1,37 +1,49 @@ +import dayjs from "dayjs"; import { TencentAccess, TencentCosAccess, TencentCosClient } from "../../tencent/index.js"; import { BaseOssClient, OssClientRemoveByOpts, OssFileItem } from "../api.js"; export default class TencentOssClientImpl extends BaseOssClient { - download(fileName: string, savePath: string): Promise { - throw new Error("Method not implemented."); + client: TencentCosClient; + + join(...strs: string[]) { + const str = super.join(...strs); + if (str.startsWith("/")) { + return str.substring(1); + } + return str; } - removeBy(removeByOpts: OssClientRemoveByOpts): Promise { - throw new Error("Method not implemented."); - } - listDir(dir: string): Promise { - throw new Error("Method not implemented."); - } - async upload(filePath: string, fileContent: Buffer) { + async init() { const access = await this.ctx.accessService.getById(this.access.accessId); - const client = new TencentCosClient({ + this.client = new TencentCosClient({ access: access, logger: this.logger, region: this.access.region, bucket: this.access.bucket, }); - const key = this.rootDir + filePath; - await client.uploadFile(key, fileContent); + } + async download(filePath: string, savePath: string): Promise { + const key = this.join(this.rootDir, filePath); + await this.client.downloadFile(key, savePath); + } + + async listDir(dir: string): Promise { + const dirKey = this.join(this.rootDir, dir) + "/"; + const res: any[] = await this.client.listDir(dirKey); + return res.map(item => { + return { + path: item.Key, + size: item.Size, + lastModified: dayjs(item.LastModified).valueOf(), + }; + }); + } + async upload(filePath: string, fileContent: Buffer) { + const key = this.join(this.rootDir, filePath); + await this.client.uploadFile(key, fileContent); } async remove(filePath: string) { - const access = await this.ctx.accessService.getById(this.access.accessId); - const client = new TencentCosClient({ - access: access, - logger: this.logger, - region: this.access.region, - bucket: this.access.bucket, - }); - const key = this.rootDir + filePath; - await client.removeFile(key); + const key = this.join(this.rootDir, filePath); + await this.client.removeFile(key); } } diff --git a/packages/plugins/plugin-lib/src/tencent/lib/cos-client.ts b/packages/plugins/plugin-lib/src/tencent/lib/cos-client.ts index 91bf0b51..25e576b6 100644 --- a/packages/plugins/plugin-lib/src/tencent/lib/cos-client.ts +++ b/packages/plugins/plugin-lib/src/tencent/lib/cos-client.ts @@ -1,5 +1,6 @@ import { TencentAccess } from "../access.js"; import { ILogger } from "@certd/basic"; +import fs from "fs"; export class TencentCosClient { access: TencentAccess; @@ -66,4 +67,47 @@ export class TencentCosClient { ); }); } + + async downloadFile(key: string, savePath: string) { + const cos = await this.getCosClient(); + const writeStream = fs.createWriteStream(savePath); + return new Promise((resolve, reject) => { + cos.getObject( + { + Bucket: this.bucket, + Region: this.region, + Key: key, + Output: writeStream, + }, + function (err, data) { + if (err) { + reject(err); + return; + } + resolve(data); + } + ); + }); + } + + async listDir(dirKey: string) { + const cos = await this.getCosClient(); + return new Promise((resolve, reject) => { + cos.getBucket( + { + Bucket: this.bucket, + Region: this.region, + Prefix: dirKey, + MaxKeys: 1000, + }, + function (err, data) { + if (err) { + reject(err); + return; + } + resolve(data.Contents); + } + ); + }); + } }