|
@ -76,31 +76,31 @@ class File { |
|
|
return reject(response.statusMessage); |
|
|
return reject(response.statusMessage); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Get the content length and parse it to an integer
|
|
|
|
|
|
const contentLength = parseInt(response.headers['content-length'], 10); |
|
|
emitter.emit('download.started', { url: this.url, filepath: this.filepath }); |
|
|
emitter.emit('download.started', { url: this.url, filepath: this.filepath, length: contentLength }); |
|
|
|
|
|
|
|
|
|
|
|
const step = Math.floor(contentLength / 100); |
|
|
|
|
|
let stepLimit = step; |
|
|
|
|
|
// Create a variable to hold the downloaded size
|
|
|
// Create a variable to hold the downloaded size
|
|
|
let downloaded = 0; |
|
|
let downloaded = 0; |
|
|
|
|
|
|
|
|
// Handle response data
|
|
|
// Handle response data
|
|
|
|
|
|
let i = 0; |
|
|
response.on('data', (chunk) => { |
|
|
response.on('data', (chunk) => { |
|
|
// Add the size of the chunk to the downloaded variable
|
|
|
|
|
|
downloaded += chunk.length; |
|
|
downloaded += chunk.length; |
|
|
|
|
|
i++; |
|
|
|
|
|
|
|
|
// Check if the downloaded size is bigger than the step limit
|
|
|
// Check if the downloaded size is bigger than the step limit
|
|
|
if (downloaded > stepLimit) { |
|
|
if (i === 500) { |
|
|
// Emit a download.progress event with the url, filepath and downloaded size
|
|
|
// Emit a download.progress event with the url, filepath and downloaded size
|
|
|
emitter.emit('download.progress', { url: this.url, filepath: this.filepath, percentage: Math.floor((downloaded / contentLength) * 100) }); |
|
|
emitter.emit('download.progress', { url: this.url, filepath: this.filepath, downloaded }); |
|
|
stepLimit += step; |
|
|
i = 0; |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
response.pipe(file); |
|
|
response.pipe(file); |
|
|
|
|
|
|
|
|
file.on('finish', () => { |
|
|
file.on('finish', () => { |
|
|
|
|
|
emitter.emit('download.progress', { url: this.url, filepath: this.filepath, downloaded }); |
|
|
|
|
|
|
|
|
// Emit a download.end event with the url and filepath
|
|
|
// Emit a download.end event with the url and filepath
|
|
|
emitter.emit('download.end', { url: this.url, filepath: this.filepath }); |
|
|
emitter.emit('download.end', { url: this.url, filepath: this.filepath }); |
|
|
file.close(() => { |
|
|
file.close(() => { |
|
|