Files
certd/packages/plugins/plugin-cert/src/plugin/cert-plugin/uploads/impls/ssh.ts
T

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-01-03 00:12:15 +08:00
import { BaseHttpChallengeUploader } from "../api.js";
2025-01-02 00:28:13 +08:00
import { SshAccess, SshClient } from "@certd/plugin-lib";
import path from "path";
import os from "os";
import fs from "fs";
export class SshHttpChallengeUploader extends BaseHttpChallengeUploader<SshAccess> {
async upload(filePath: string, fileContent: Buffer) {
const tmpFilePath = path.join(os.tmpdir(), "cert", "http", filePath);
2025-01-02 00:28:13 +08:00
// Write file to temp path
const dir = path.dirname(tmpFilePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
2025-01-02 00:28:13 +08:00
fs.writeFileSync(tmpFilePath, fileContent);
const key = this.rootDir + filePath;
2025-01-02 00:28:13 +08:00
try {
const client = new SshClient(this.logger);
await client.uploadFiles({
connectConf: this.access,
mkdirs: true,
transports: [
{
localPath: tmpFilePath,
remotePath: key,
2025-01-02 00:28:13 +08:00
},
],
});
} finally {
// Remove temp file
fs.unlinkSync(tmpFilePath);
}
}
async remove(filePath: string) {
const client = new SshClient(this.logger);
const key = this.rootDir + filePath;
2025-01-02 00:28:13 +08:00
await client.removeFiles({
connectConf: this.access,
files: [key],
2025-01-02 00:28:13 +08:00
});
}
}