2023-01-13 05:40:33 +01:00
|
|
|
import * as fs from "node:fs";
|
|
|
|
import * as util from "node:util";
|
|
|
|
import Logger from "@/services/logger.js";
|
|
|
|
import { createTemp } from "./create-temp.js";
|
|
|
|
import { downloadUrl } from "./download-url.js";
|
2019-03-11 12:23:29 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const logger = new Logger("download-text-file");
|
2019-03-11 12:23:29 +01:00
|
|
|
|
|
|
|
export async function downloadTextFile(url: string): Promise<string> {
|
|
|
|
// Create temp file
|
2019-03-20 20:50:44 +01:00
|
|
|
const [path, cleanup] = await createTemp();
|
2019-03-11 12:23:29 +01:00
|
|
|
|
|
|
|
logger.info(`Temp file is ${path}`);
|
|
|
|
|
2019-03-20 20:50:44 +01:00
|
|
|
try {
|
|
|
|
// write content at URL to temp file
|
|
|
|
await downloadUrl(url, path);
|
2019-03-11 12:23:29 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const text = await util.promisify(fs.readFile)(path, "utf8");
|
2019-03-11 12:23:29 +01:00
|
|
|
|
2019-03-20 20:50:44 +01:00
|
|
|
return text;
|
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
2019-03-11 12:23:29 +01:00
|
|
|
}
|