iceshrimp-161sh/packages/backend/src/misc/download-text-file.ts

26 lines
623 B
TypeScript
Raw Normal View History

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";
2023-01-13 05:40:33 +01:00
const logger = new Logger("download-text-file");
export async function downloadTextFile(url: string): Promise<string> {
// Create temp file
const [path, cleanup] = await createTemp();
logger.info(`Temp file is ${path}`);
try {
// write content at URL to temp file
await downloadUrl(url, path);
2023-01-13 05:40:33 +01:00
const text = await util.promisify(fs.readFile)(path, "utf8");
return text;
} finally {
cleanup();
}
}