feat: save files

pull/21/merge
xiaojunnuo 2023-06-25 23:45:13 +08:00
parent 671d273e2f
commit 99522fb49a
3 changed files with 20 additions and 5 deletions

View File

@ -28,11 +28,16 @@ export class FileStore {
writeFile(filename: string, file: Buffer) {
const localPath = this.buildFilePath(filename);
fs.writeFileSync(localPath, file);
return localPath;
}
private buildFilePath(filename: string) {
return path.join(this.rootDir, this.scope, dayjs().format("YYYY-MM-DD"), this.parent, filename);
const parentDir = path.join(this.rootDir, this.scope + "", dayjs().format("YYYY-MM-DD"), this.parent + "");
if (!fs.existsSync(parentDir)) {
fs.mkdirSync(parentDir, { recursive: true });
}
return path.join(parentDir, filename);
}
}

View File

@ -19,6 +19,7 @@
"dependencies": {
"@certd/acme-client": "^1.0.6",
"@certd/pipeline": "^1.0.6",
"jszip": "^3.10.1",
"node-forge": "^0.10.0"
},
"devDependencies": {

View File

@ -5,7 +5,7 @@ import _ from "lodash";
import { Logger } from "log4js";
import { DnsProviderDefine, dnsProviderRegistry } from "../../dns-provider";
import { CertReader } from "./cert-reader";
import JSZip from "jszip";
export { CertReader };
export type { CertInfo };
@ -133,11 +133,11 @@ export class CertApplyPlugin extends AbstractTaskPlugin {
async execute(): Promise<void> {
const oldCert = await this.condition();
if (oldCert != null) {
return this.output(oldCert.toCertInfo());
return await this.output(oldCert.toCertInfo());
}
const cert = await this.doCertApply();
if (cert != null) {
this.output(cert.toCertInfo());
await this.output(cert.toCertInfo());
//清空后续任务的状态,让后续任务能够重新执行
this.clearLastStatus();
} else {
@ -145,8 +145,17 @@ export class CertApplyPlugin extends AbstractTaskPlugin {
}
}
output(cert: CertInfo) {
async output(cert: CertInfo) {
this.cert = cert;
await this.zipCert(cert);
}
async zipCert(cert: CertInfo) {
const zip = new JSZip();
zip.file("cert.crt", cert.crt);
zip.file("cert.key", cert.key);
const content = await zip.generateAsync({ type: "nodebuffer" });
this.saveFile("cert.zip", content);
}
/**