-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcleanup.ts
45 lines (38 loc) · 1.42 KB
/
cleanup.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
/*
* You can import this module by using:
* import { clean } from "easydl/utils";
* or
* const { clean } = require('easydl/utils');
*/
import { clean } from "../dist/utils";
// import EasyDl from "easydl";
import EasyDl from "../dist";
// All downloaded chunks are kept until the download has finished.
// If you decide to abort the download and do not plan to resume, you
// can use the cleaning utility provided by easydl to delete all temporary chunk files
function createDownload(output: string) {
const dl = new EasyDl(
"http://www.ovh.net/files/10Mio.dat",
`/tmp/${output}.dat`
).on("progress", ({ total }) => {
// stop the download at 50% so that we have chunk files left
if (total.percentage >= 50) dl.destroy();
});
return dl;
}
(async () => {
// let's create some chunk files
const dl1 = createDownload("1");
const dl2 = createDownload("2");
const dl3 = createDownload("3");
const dl4 = createDownload("4");
console.log("downloading ...");
await Promise.all([dl1.wait(), dl2.wait(), dl3.wait(), dl4.wait()]);
// we should have chunk files from 4 downloads at "/tmp" now
// this will delete chunks belonging to the first download only
const deletedChunks1 = await clean("/tmp/1.dat");
console.log(deletedChunks1);
// this will delete all remaining chunks in /tmp directory
const deletedChunks2 = await clean("/tmp");
console.log(deletedChunks2);
})();