-
Notifications
You must be signed in to change notification settings - Fork 3
/
url_downloader.ts
64 lines (54 loc) · 1.58 KB
/
url_downloader.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { ensureDir, exists } from "https://deno.land/std/fs/mod.ts";
import { join } from "https://deno.land/std/path/mod.ts";
import { blue, bold, green } from "https://deno.land/std/fmt/colors.ts";
import { fmtFileSize } from "https://deno.land/x/getfiles/mod.ts";
class UrlDownloader {
url: string;
name: string;
path: string;
constructor(url: string, name: string, path: string) {
this.url = url;
this.name = name;
this.path = path;
}
async download() {
const fileExists = await exists(this.filepath);
if (fileExists) {
console.log(this.name + " already exists, skipping...");
return;
}
console.log(
bold(green("Downloading " + this.name + " (" + this.url + ") ...")),
);
const response = await fetch(this.url);
if (!response.ok) {
console.log(
`Skipping ${this.name} (${response.status} ${response.statusText}: ${this.url})`,
);
return;
}
console.log("Writing file ...");
await ensureDir(this.path);
const newFile = await Deno.open(this.filepath, {
create: true,
write: true,
});
const reader = response.body?.getReader();
if (reader) {
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (value) {
await newFile.write(value);
}
}
}
const stats = await newFile.stat();
newFile.close();
console.log(blue(`Wrote ${fmtFileSize(stats.size)} to ${this.filepath}`));
}
get filepath(): string {
return join(this.path, this.name);
}
}
export default UrlDownloader;