perf: 支持s3 access做测试

pull/436/head
xiaojunnuo 2025-06-13 12:18:26 +08:00
parent 5b49071d6b
commit f00aeacb8b
6 changed files with 50 additions and 14 deletions

View File

@ -37,6 +37,7 @@ export type AccessContext = {
http: HttpClient; http: HttpClient;
logger: ILogger; logger: ILogger;
utils: typeof utils; utils: typeof utils;
accessService: IAccessService;
}; };
export abstract class BaseAccess implements IAccess { export abstract class BaseAccess implements IAccess {

View File

@ -1,5 +1,5 @@
// src/decorator/memoryCache.decorator.ts // 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 { Decorator } from "../decorator/index.js";
import * as _ from "lodash-es"; import * as _ from "lodash-es";
import { accessRegistry } from "./registry.js"; 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); const register = accessRegistry.get(type);
if (register == null) { if (register == null) {
throw new Error(`access ${type} not found`); throw new Error(`access ${type} not found`);
@ -58,6 +58,7 @@ export async function newAccess(type: string, input: any, ctx?: AccessContext) {
http, http,
logger, logger,
utils, utils,
accessService,
}; };
} }
access.setCtx(ctx); access.setCtx(ctx);

View File

@ -1,16 +1,16 @@
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core'; import {Inject, Provide, Scope, ScopeEnum} from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm'; import {InjectEntityModel} from '@midwayjs/typeorm';
import { Repository } from 'typeorm'; import {Repository} from 'typeorm';
import { BaseService, PageReq, PermissionException, ValidateException } from '../../../index.js'; import {AccessGetter, BaseService, PageReq, PermissionException, ValidateException} from '../../../index.js';
import { AccessEntity } from '../entity/access.js'; import {AccessEntity} from '../entity/access.js';
import { AccessDefine, accessRegistry, newAccess } from '@certd/pipeline'; import {AccessDefine, accessRegistry, newAccess} from '@certd/pipeline';
import { EncryptService } from './encrypt-service.js'; import {EncryptService} from './encrypt-service.js';
/** /**
* *
*/ */
@Provide() @Provide()
@Scope(ScopeEnum.Request, { allowDowngrade: true }) @Scope(ScopeEnum.Request, {allowDowngrade: true})
export class AccessService extends BaseService<AccessEntity> { export class AccessService extends BaseService<AccessEntity> {
@InjectEntityModel(AccessEntity) @InjectEntityModel(AccessEntity)
repository: Repository<AccessEntity>; repository: Repository<AccessEntity>;
@ -95,6 +95,7 @@ export class AccessService extends BaseService<AccessEntity> {
param.encryptSetting = JSON.stringify(encryptSetting); param.encryptSetting = JSON.stringify(encryptSetting);
param.setting = JSON.stringify(json); param.setting = JSON.stringify(json);
} }
/** /**
* *
* @param param * @param param
@ -140,7 +141,8 @@ export class AccessService extends BaseService<AccessEntity> {
id: entity.id, id: entity.id,
...setting, ...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<any> { async getById(id: any, userId: number): Promise<any> {

View File

@ -54,6 +54,9 @@ export default class S3OssClientImpl extends BaseOssClient<S3Access> {
Prefix: dirKey, // The name of the object. For example, 'sample_upload.txt'. Prefix: dirKey, // The name of the object. For example, 'sample_upload.txt'.
}; };
const res = await this.client.send(new ListObjectsCommand({ ...params })); const res = await this.client.send(new ListObjectsCommand({ ...params }));
if (!res.Contents) {
return [];
}
return res.Contents.map(item => { return res.Contents.map(item => {
return { return {
path: item.Key, path: item.Key,

View File

@ -1,4 +1,6 @@
import { AccessInput, BaseAccess, IsAccess } from "@certd/pipeline"; 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, required: true,
}) })
bucket!: string; 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(); new S3Access();

View File

@ -1,5 +1,5 @@
import {ALL, Body, Controller, Inject, Post, Provide} from '@midwayjs/core'; 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 { import {
AccessRequestHandleReq, AccessRequestHandleReq,
IAccessService, IAccessService,
@ -33,6 +33,7 @@ export class HandleController extends BaseController {
@Post('/access', { summary: Constants.per.authOnly }) @Post('/access', { summary: Constants.per.authOnly })
async accessRequest(@Body(ALL) body: AccessRequestHandleReq) { async accessRequest(@Body(ALL) body: AccessRequestHandleReq) {
const userId = this.getUserId();
let inputAccess = body.input.access; let inputAccess = body.input.access;
if (body.input.id > 0) { if (body.input.id > 0) {
const oldEntity = await this.accessService.info(body.input.id); const oldEntity = await this.accessService.info(body.input.id);
@ -48,8 +49,8 @@ export class HandleController extends BaseController {
inputAccess = this.accessService.decryptAccessEntity(param); inputAccess = this.accessService.decryptAccessEntity(param);
} }
} }
const accessGetter = new AccessGetter(userId, this.accessService.getById.bind(this.accessService));
const access = await newAccess(body.typeName, inputAccess); const access = await newAccess(body.typeName, inputAccess,accessGetter);
const res = await access.onRequest(body); const res = await access.onRequest(body);