10 Star 46 Fork 10

Cherokee / fast-down

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sub-download.js 1.88 KB
一键复制 编辑 原始数据 按行查看 历史
Rorical 提交于 2020-09-07 22:04 . !1增加下载请求时的header头
const request = require('request');
const progress = require('request-progress');
const fs = require('fs');
process.send = process.send || function(msg) {
console.log(msg)
};
const download = (key, url, filepath, range_start, range_end, throttle, headers) => {
let d_err;
headers = Object.assign({
"Range":'bytes=' + range_start + '-' + range_end
}, headers);
let r = request(url, {
'headers': headers
});
progress(r, {
'throttle': throttle
})
.on('response', response => {
if (response.statusCode < 200 || response.statusCode > 210) {
r.abort();
d_err = 'http status code: ' + response.statusCode;
process.send({
'key': key,
'err': d_err
});
}
})
.on('progress', function(state) {
process.send({
'key': key,
'progress': state
});
})
.on('error', function(err) {
d_err = err;
process.send({
'key': key,
'err': err
});
})
.on('end', function() {
process.send({
'key': key,
'finish': true,
'err': d_err
});
})
.pipe(fs.createWriteStream(filepath));
}
if (module.parent) {
exports.download = download;
} else {
if (process.argv.length < 8) {
console.error('invalidate process arguments');
} else {
let [_1, _2, key, url, filepath, range_start, range_end, throttle, headers] = process.argv;
range_start = parseInt(range_start);
range_end = parseInt(range_end);
throttle = parseInt(throttle);
headers = JSON.parse(headers)
download(key, url, filepath, range_start, range_end, throttle, headers);
}
}
NodeJS
1
https://gitee.com/dreamidea/fast-down.git
git@gitee.com:dreamidea/fast-down.git
dreamidea
fast-down
fast-down
master

搜索帮助