From f00aeacb8b5c81f0bafa4c1b76723dec2b6b7784 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Fri, 13 Jun 2025 12:18:26 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=94=AF=E6=8C=81s3=20access=E5=81=9A?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/pipeline/src/access/api.ts | 1 + .../core/pipeline/src/access/decorator.ts | 5 ++-- .../src/user/access/service/access-service.ts | 20 +++++++------ .../plugins/plugin-lib/src/oss/impls/s3.ts | 3 ++ packages/plugins/plugin-lib/src/s3/access.ts | 28 +++++++++++++++++++ .../user/pipeline/handle-controller.ts | 7 +++-- 6 files changed, 50 insertions(+), 14 deletions(-) diff --git a/packages/core/pipeline/src/access/api.ts b/packages/core/pipeline/src/access/api.ts index 48bafb50..3d8c2f98 100644 --- a/packages/core/pipeline/src/access/api.ts +++ b/packages/core/pipeline/src/access/api.ts @@ -37,6 +37,7 @@ export type AccessContext = { http: HttpClient; logger: ILogger; utils: typeof utils; + accessService: IAccessService; }; export abstract class BaseAccess implements IAccess { diff --git a/packages/core/pipeline/src/access/decorator.ts b/packages/core/pipeline/src/access/decorator.ts index d961131f..bb2f0d6b 100644 --- a/packages/core/pipeline/src/access/decorator.ts +++ b/packages/core/pipeline/src/access/decorator.ts @@ -1,5 +1,5 @@ // src/decorator/memoryCache.decorator.ts -import { AccessContext, AccessDefine, AccessInputDefine } from "./api.js"; +import { AccessContext, AccessDefine, AccessInputDefine, IAccessService } from "./api.js"; import { Decorator } from "../decorator/index.js"; import * as _ from "lodash-es"; import { accessRegistry } from "./registry.js"; @@ -41,7 +41,7 @@ export function AccessInput(input?: AccessInputDefine): PropertyDecorator { }; } -export async function newAccess(type: string, input: any, ctx?: AccessContext) { +export async function newAccess(type: string, input: any, accessService: IAccessService, ctx?: AccessContext) { const register = accessRegistry.get(type); if (register == null) { throw new Error(`access ${type} not found`); @@ -58,6 +58,7 @@ export async function newAccess(type: string, input: any, ctx?: AccessContext) { http, logger, utils, + accessService, }; } access.setCtx(ctx); diff --git a/packages/libs/lib-server/src/user/access/service/access-service.ts b/packages/libs/lib-server/src/user/access/service/access-service.ts index b928f04b..bc19f964 100644 --- a/packages/libs/lib-server/src/user/access/service/access-service.ts +++ b/packages/libs/lib-server/src/user/access/service/access-service.ts @@ -1,16 +1,16 @@ -import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core'; -import { InjectEntityModel } from '@midwayjs/typeorm'; -import { Repository } from 'typeorm'; -import { BaseService, PageReq, PermissionException, ValidateException } from '../../../index.js'; -import { AccessEntity } from '../entity/access.js'; -import { AccessDefine, accessRegistry, newAccess } from '@certd/pipeline'; -import { EncryptService } from './encrypt-service.js'; +import {Inject, Provide, Scope, ScopeEnum} from '@midwayjs/core'; +import {InjectEntityModel} from '@midwayjs/typeorm'; +import {Repository} from 'typeorm'; +import {AccessGetter, BaseService, PageReq, PermissionException, ValidateException} from '../../../index.js'; +import {AccessEntity} from '../entity/access.js'; +import {AccessDefine, accessRegistry, newAccess} from '@certd/pipeline'; +import {EncryptService} from './encrypt-service.js'; /** * 授权 */ @Provide() -@Scope(ScopeEnum.Request, { allowDowngrade: true }) +@Scope(ScopeEnum.Request, {allowDowngrade: true}) export class AccessService extends BaseService { @InjectEntityModel(AccessEntity) repository: Repository; @@ -95,6 +95,7 @@ export class AccessService extends BaseService { param.encryptSetting = JSON.stringify(encryptSetting); param.setting = JSON.stringify(json); } + /** * 修改 * @param param 数据 @@ -140,7 +141,8 @@ export class AccessService extends BaseService { id: entity.id, ...setting, }; - return await newAccess(entity.type, input); + const accessGetter = new AccessGetter(userId, this.getById.bind(this)); + return await newAccess(entity.type, input,accessGetter); } async getById(id: any, userId: number): Promise { diff --git a/packages/plugins/plugin-lib/src/oss/impls/s3.ts b/packages/plugins/plugin-lib/src/oss/impls/s3.ts index 644a01ff..a0954094 100644 --- a/packages/plugins/plugin-lib/src/oss/impls/s3.ts +++ b/packages/plugins/plugin-lib/src/oss/impls/s3.ts @@ -54,6 +54,9 @@ export default class S3OssClientImpl extends BaseOssClient { Prefix: dirKey, // The name of the object. For example, 'sample_upload.txt'. }; const res = await this.client.send(new ListObjectsCommand({ ...params })); + if (!res.Contents) { + return []; + } return res.Contents.map(item => { return { path: item.Key, diff --git a/packages/plugins/plugin-lib/src/s3/access.ts b/packages/plugins/plugin-lib/src/s3/access.ts index 3db87d38..a44f3988 100644 --- a/packages/plugins/plugin-lib/src/s3/access.ts +++ b/packages/plugins/plugin-lib/src/s3/access.ts @@ -1,4 +1,6 @@ import { AccessInput, BaseAccess, IsAccess } from "@certd/pipeline"; +import { ossClientFactory } from "../oss/index.js"; +import S3OssClientImpl from "../oss/impls/s3.js"; /** * 这个注解将注册一个授权配置 @@ -82,6 +84,32 @@ export class S3Access extends BaseAccess { required: true, }) bucket!: string; + + @AccessInput({ + title: "测试", + component: { + name: "api-test", + action: "TestRequest", + }, + helper: "点击测试接口是否正常", + }) + testRequest = true; + + async onTestRequest() { + const client: S3OssClientImpl = await ossClientFactory.createOssClientByType("s3", { + access: this, + rootDir: "", + ctx: { + accessService: this.ctx.accessService, + logger: this.ctx.logger, + utils: this.ctx.utils, + }, + }); + + await client.listDir("/"); + + return "ok"; + } } new S3Access(); diff --git a/packages/ui/certd-server/src/controller/user/pipeline/handle-controller.ts b/packages/ui/certd-server/src/controller/user/pipeline/handle-controller.ts index dbdbea9c..06d3f45a 100644 --- a/packages/ui/certd-server/src/controller/user/pipeline/handle-controller.ts +++ b/packages/ui/certd-server/src/controller/user/pipeline/handle-controller.ts @@ -1,5 +1,5 @@ import {ALL, Body, Controller, Inject, Post, Provide} from '@midwayjs/core'; -import {AccessService, BaseController, Constants} from '@certd/lib-server'; +import {AccessGetter, AccessService, BaseController, Constants} from '@certd/lib-server'; import { AccessRequestHandleReq, IAccessService, @@ -33,6 +33,7 @@ export class HandleController extends BaseController { @Post('/access', { summary: Constants.per.authOnly }) async accessRequest(@Body(ALL) body: AccessRequestHandleReq) { + const userId = this.getUserId(); let inputAccess = body.input.access; if (body.input.id > 0) { const oldEntity = await this.accessService.info(body.input.id); @@ -48,8 +49,8 @@ export class HandleController extends BaseController { inputAccess = this.accessService.decryptAccessEntity(param); } } - - const access = await newAccess(body.typeName, inputAccess); + const accessGetter = new AccessGetter(userId, this.accessService.getById.bind(this.accessService)); + const access = await newAccess(body.typeName, inputAccess,accessGetter); const res = await access.onRequest(body);