-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.mjs
35 lines (29 loc) · 923 Bytes
/
utils.mjs
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
26
27
28
29
30
31
32
33
34
35
import fs from 'node:fs';
import { Readable } from 'stream';
import { finished } from 'stream/promises';
export async function downloadImage(imageUrl, destination) {
if (fs.existsSync(destination)) {
console.error('File already exists:', destination);
return true;
}
try {
const response = await fetch(imageUrl);
if (response.ok) {
const fileStream = fs.createWriteStream(destination, { flags: 'wx' });
await finished(Readable.fromWeb(response.body).pipe(fileStream));
return true;
}
console.error('Failed to download image:', imageUrl);
return false;
}
catch (error) {
console.error('Failed to download image:', imageUrl, error);
return false;
}
}
export function convertEscapedAscii(string) {
return string.replace(/&#(\d+);/g, (_, dec) => String.fromCharCode(dec));
}
export function stripHtml(string) {
return string.replace(/<[^>]*>/g, '');
}