/
index.ts
46 lines (41 loc) · 990 Bytes
/
index.ts
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
36
37
38
39
40
41
42
43
44
45
46
import request from "request";
import fs from "fs";
function writeFile(output: string, buf: any) {
return new Promise((resolve, reject) => {
fs.writeFile(output, buf, error => {
if (error) {
reject(error);
}
resolve();
});
});
}
function getImageBuf(imageUrl: string) {
const options = {
url: imageUrl,
method: "get",
encoding: null
};
return new Promise((resolve, reject) => {
request(options, (error, res) => {
if (error != null) {
reject(error);
}
resolve(res.body);
});
});
}
export default async function daolnwod(url: string, output: string) {
if (typeof url !== "string") {
throw new Error("url is must be string");
}
if (typeof output !== "string") {
throw new Error("output is must be string");
}
try {
const buf = await getImageBuf(url);
await writeFile(output, buf);
} catch (error) {
throw error;
}
}