summaryrefslogtreecommitdiff
path: root/packages/backend/src/misc/download-text-file.ts
blob: c62c70ee33b16d78c18233b4b0e3e69df8d12c38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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';

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);

		const text = await util.promisify(fs.readFile)(path, 'utf8');

		return text;
	} finally {
		cleanup();
	}
}