1 | 'use strict'
|
2 |
|
3 | const https = require('https')
|
4 | const fs = require('fs')
|
5 |
|
6 | exports.get = function (url, options, cb) {
|
7 | const dest = options.destination
|
8 |
|
9 | https.get(url, response => {
|
10 | if (parseInt(response.statusCode.toString().substring(0, 1), 10) > 3) {
|
11 | return cb(new Error(`Could not download ${url}, statusCode: ${response.statusCode.toString()}`), null)
|
12 | }
|
13 |
|
14 | const file = fs.createWriteStream(dest)
|
15 | response.pipe(file)
|
16 | file.on('finish', () => {
|
17 | file.close()
|
18 | cb(null, dest)
|
19 | })
|
20 | }).on('error', e => {
|
21 | cb(e.message, null)
|
22 | })
|
23 | }
|