2025-01-03 00:12:15 +08:00
|
|
|
import { BaseHttpChallengeUploader } from "../api.js";
|
|
|
|
|
import { FtpAccess, FtpClient } from "@certd/plugin-lib";
|
2025-01-04 01:45:24 +08:00
|
|
|
import path from "path";
|
|
|
|
|
import os from "os";
|
|
|
|
|
import fs from "fs";
|
2025-01-02 00:28:13 +08:00
|
|
|
|
|
|
|
|
export class FtpHttpChallengeUploader extends BaseHttpChallengeUploader<FtpAccess> {
|
2025-01-04 01:45:24 +08:00
|
|
|
async upload(filePath: string, fileContent: Buffer) {
|
2025-01-02 00:28:13 +08:00
|
|
|
const client = new FtpClient({
|
|
|
|
|
access: this.access,
|
|
|
|
|
logger: this.logger,
|
|
|
|
|
});
|
|
|
|
|
await client.connect(async (client) => {
|
2025-01-04 01:45:24 +08:00
|
|
|
const tmpFilePath = path.join(os.tmpdir(), "cert", "http", filePath);
|
|
|
|
|
const dir = path.dirname(tmpFilePath);
|
|
|
|
|
if (!fs.existsSync(dir)) {
|
|
|
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
fs.writeFileSync(tmpFilePath, fileContent);
|
|
|
|
|
try {
|
|
|
|
|
// Write file to temp path
|
|
|
|
|
const path = this.rootDir + filePath;
|
|
|
|
|
await client.upload(path, tmpFilePath);
|
|
|
|
|
} finally {
|
|
|
|
|
// Remove temp file
|
|
|
|
|
fs.unlinkSync(tmpFilePath);
|
|
|
|
|
}
|
2025-01-02 00:28:13 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-04 01:45:24 +08:00
|
|
|
async remove(filePath: string) {
|
2025-01-02 00:28:13 +08:00
|
|
|
const client = new FtpClient({
|
|
|
|
|
access: this.access,
|
|
|
|
|
logger: this.logger,
|
|
|
|
|
});
|
|
|
|
|
await client.connect(async (client) => {
|
2025-01-04 01:45:24 +08:00
|
|
|
const path = this.rootDir + filePath;
|
|
|
|
|
await client.client.remove(path);
|
2025-01-02 00:28:13 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|