forked from ericoyf/node-filedownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (115 loc) · 3.2 KB
/
index.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const EventEmitter = require('events')
const utils = require('./lib/util')
const path = require('path')
const spawn = require('child_process').spawn
const defaults = {
url: null,
saveAs: null,
saveTo: process.cwd(),
deleteIfExists: false,
resume: true
}
class Downloader extends EventEmitter {
constructor (options) {
super()
this.options = Object.assign({}, defaults, options)
if (!this.options.url) {
setImmediate(() => {
this.emit('error', new Error('No url specified'))
})
return this
}
if (!utils.ValidUrl(this.options.url)) {
setImmediate(() => {
this.emit('error', new Error('Not a valid url!'))
})
return this
}
utils
.checkDir(this.options.saveTo)
.then(() => utils.getHead(this.options.url))
.then(res => utils.responseCheck(res))
.then(res => utils.parseHead(res))
.then(fileInfo => {
this.options.saveAs = this.options.saveAs || fileInfo.filename
this.fileInfo = fileInfo
return utils.checkBeforeDownload(this.options, fileInfo.size)
})
.then(fileSize => this.download(fileSize))
.catch(err => this.emit('error', err))
return this
}
download (fileSize) {
if (!isNaN(fileSize) && fileSize === this.fileInfo.size) {
return this.emit('end')
}
const filePath = path.join(this.options.saveTo, this.options.saveAs)
const Args = [].concat(utils.PorcessOptions)
// const speed = 0
// const now = {
// time: 0,
// data: 0
// }
// const previous = {
// time: new Date().getTime() / 1000,
// data: 0
// }
let started = false
let progress = 0
Args.push(`-o${filePath}`)
if (!isNaN(fileSize)) Args.push(`-C ${fileSize}`)
Args.push(this.options.url)
this.curl = spawn('curl', Args).stderr
.on('data', data => {
data = data.toString('ascii').trim()
if (data.indexOf('#') !== -1) return
if (!started) this.emit('start')
if (started) {
utils
.getFilesize(filePath)
.then(size => {
progress = parseFloat(size / this.fileInfo.size)
this.emit('progress', {
progress,
dataWritten: size,
filesize: this.fileInfo.size,
speed: null
})
})
.catch(err => {
this.curl.kill()
this.emit('error', err)
})
}
started = true
})
.on('close', code => {
if (code === null) return
if (code) this.emit('error', new Error(code))
utils
.getFilesize(filePath)
.then(size => {
this.emit('progress', {
progress: 1,
dataWritten: size,
filesize: this.fileInfo.size,
speed: 0
})
this.emit('end')
})
.catch(err => this.emit('error', err))
})
}
pause () {
if (this.curl) this.curl.kill()
return this
}
resume () {
if (this.curl) utils.download(utils.getFilesize(this.filePath))
return this
}
getCurl () {
if (this.curl) return this.curl
}
}
module.exports = Downloader