node-file-handler.ts
2.45 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { Util } from './../util';
import { EventEmitter } from 'events';
import { FileHandler } from '../api/file-handler';
import { File } from '../api/file';
import * as http from 'http';
import * as https from 'https';
import * as fs from 'fs';
export class NodeFileHandler extends EventEmitter implements FileHandler {
selectProtocol(url:string) : any {
if (url.search(/^http:\/\//) === 0) {
return http;
} else if (url.search(/^https:\/\//) === 0) {
return https;
} else {
return null;
}
}
download(source:string, target:string) {
let handler = this.selectProtocol(source);
if (!handler) {
this.emit("error","No handler for source: " + source);
return this;
}
// file already exists and is not empty
if (fs.existsSync(target) && (fs.statSync(target)['size'] > 0)) {
this.emit("complete");
return this;
} else {
let file = fs.createWriteStream(target, {'flags': 'a'});
handler.get(source, (response) => {
let size = response.headers['content-length']; // in bytes
let prog = 0; // already downloaded
let progCounts = 100; // how many progress events should be triggerd (1-100 %)
let nextProg = (1/progCounts);
response.on('data', (chunk) => {
prog += chunk.length;
file.write(chunk, 'binary');
if ((prog / size) > nextProg) {
this.emit('progress',prog / size);
nextProg += (1 / progCounts);
}
});
response.once('end', () => {
file.end();
this.emit('complete');
});
}).on('error', (error) => {
fs.unlink(target);
this.emit("error", "Error while downloading: " + error);
});
return this;
}
}
cleanup(files:Array<File>, basePath?:string) {
try {
let localFiles = fs.readdirSync(basePath);
Util.getFilesForCleanup(files, localFiles)
.forEach((file) => {
fs.unlinkSync(basePath + "/" + file);
});
} catch (e) {
}
return this;
}
}