Files
certd/packages/plugins/plugin-lib/src/oss/impls/ssh.ts
T

58 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-04-25 01:26:04 +08:00
import { BaseOssClient, OssClientRemoveByOpts, OssFileItem } from "../api.js";
2025-01-02 00:28:13 +08:00
import path from "path";
import os from "os";
import fs from "fs";
2025-04-25 01:26:04 +08:00
import { SshAccess, SshClient } from "../../ssh/index.js";
2025-01-02 00:28:13 +08:00
2025-04-27 01:31:46 +08:00
//废弃
2025-04-25 01:26:04 +08:00
export default class SshOssClientImpl extends BaseOssClient<SshAccess> {
download(fileName: string, savePath: string): Promise<void> {
throw new Error("Method not implemented.");
}
removeBy(removeByOpts: OssClientRemoveByOpts): Promise<void> {
throw new Error("Method not implemented.");
}
listDir(dir: string): Promise<OssFileItem[]> {
throw new Error("Method not implemented.");
}
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);
}
}
2025-04-27 01:31:46 +08:00
async remove(filePath: string, opts?: { joinRootDir?: boolean }) {
if (opts?.joinRootDir !== false) {
filePath = this.join(this.rootDir, filePath);
}
2025-01-02 00:28:13 +08:00
const client = new SshClient(this.logger);
await client.removeFiles({
connectConf: this.access,
2025-04-27 01:31:46 +08:00
files: [filePath],
2025-01-02 00:28:13 +08:00
});
}
}