perf: 上传自定义证书

This commit is contained in:
xiaojunnuo
2025-01-24 18:04:17 +08:00
parent c89686a2fd
commit 75a38d95f3
7 changed files with 177 additions and 63 deletions
@@ -1,11 +1,17 @@
import { Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { BaseService, CodeException, Constants, PageReq } from '@certd/lib-server';
import { BaseService, CodeException, CommonException, Constants, PageReq } from '@certd/lib-server';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { CertInfoEntity } from '../entity/cert-info.js';
import { utils } from '@certd/basic';
import { CertInfo, CertReader } from '@certd/plugin-cert';
export type UploadCertReq = {
id?: number;
certReader: CertReader;
fromType?: string;
};
@Provide()
@Scope(ScopeEnum.Request, { allowDowngrade: true })
export class CertInfoService extends BaseService<CertInfoEntity> {
@@ -125,15 +131,27 @@ export class CertInfoService extends BaseService<CertInfoEntity> {
return certReader.toCertInfo();
}
async updateCert(pipelineId: number, certReader: CertReader) {
async updateCertByPipelineId(pipelineId: number, certReader: CertReader, fromType = 'pipeline') {
const found = await this.repository.findOne({
where: {
pipelineId,
},
});
const bean = await this.updateCert({
id: found?.id,
certReader,
fromType,
});
return bean;
}
private async updateCert(req: UploadCertReq) {
const bean = new CertInfoEntity();
if (found) {
bean.id = found.id;
const { id, fromType, certReader } = req;
if (id) {
bean.id = id;
} else {
bean.fromType = fromType;
}
const certInfo = certReader.toCertInfo();
bean.certInfo = JSON.stringify(certInfo);
@@ -146,5 +164,19 @@ export class CertInfoService extends BaseService<CertInfoEntity> {
bean.certProvider = certReader.detail.issuer.commonName;
await this.addOrUpdate(bean);
return bean;
}
async upload(body: { id?: number; cert: CertInfo }) {
const { id, cert } = body;
if (!cert) {
throw new CommonException("cert can't be empty");
}
const res = await this.updateCert({
id,
certReader: new CertReader(cert),
fromType: 'upload',
});
return res.id;
}
}