Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
download(src, path) {
const req = RequestProgress(Request.get(src), {
delay: 1000 // start to emit after 1000ms delay
});
req.on('progress', (state) => {
this.emit('download-progress', Math.floor(state.percentage * 100));
})
.on('error', (error) => {
console.log('error when requesting file');
console.log(error);
this.emit('download-error');
})
.pipe(Fs.createWriteStream(path))
.on('error', (error) => {
console.log('error when saving file to path' + path);
console.log(error);
this.emit('download-error');
return new Promise((resolve, reject) => {
var r = request({
url,
headers: {
'Origin': 'http://music.163.com',
'Referer': 'http://music.163.com/',
},
timeout: 10000,
});
rp(r)
.on('error',
err => {
delete cancels[canceler];
// https://github.com/request/request/blob/a92e138d897d78ce19dd70bd1ad271c7f9c6a23d/request.js#L1167
if (r._aborted) {
reject(new Error('_aborted'));
} else {
reject(err);
}
}
)
.on('progress',
state => {
callback.size = state.size || {};
callback(state);
started: (req) => {
this.attempts += 1;
this.request = req;
return progress(this.request, {throtte: 250})
.on('progress', (prog) => {
this.percent = prog.percent;
this.progressCallback();
})
// This is a /socket/ error event, not an HTTP error event. It fires
// when the conn is dropped, user if offline, but not on HTTP status codes.
// It is sometimes called in place of "end", not before or after.
.on('error', onFailed)
.on('end', () => {
if (this.state === State.Failed) { return; }
const {response} = this.request
const statusCode = response ? response.statusCode : null;
if ([200, 202, 204].includes(statusCode)) {
downloadFile (filename, url, done) {
this.setState({
...this.state,
progress: 0
})
console.log('downloading to', path.resolve(config.drive.mountPoint, filename))
requestProgress(request(url))
.on('progress', (state) => {
this.setState({
...this.state,
progress: (state.percent * 100).toFixed(0)
})
})
.on('end', (err) => {
if (err) {
return done(err)
}
this.setState({
...this.state,
progress: 100
})
return done()
})
loadMaterialData() {
if (this.dataLoading) {
this.progressVisible = true;
}
if (!this.materialData) {
this.dataLoading = true;
this.progressTitle = '下载物料数据';
requestProgress(
request(
'http://ice.alicdn.com/iceland-assets/material-engine-production.json',
(error, response, body) => {
if (!error) {
this.materialData = body;
this.getEngine('react');
this.loadIconData();
} else if (this.requestCount < 3) {
this.requestCount++;
this.loadMaterialData();
} else {
this.requestCount = 0;
this.errorVisible = true;
this.dataLoading = false;
this.progressVisible = false;
}
return new Promise((resolve, reject) => {
const headers = { Accept: 'application/octet-stream' };
const ws = fs.createWriteStream(file);
let result;
const req = progress(this.request.get(url, {
headers
}, (error, response) => {
if (error) {
log.disableProgress();
return reject(wasReported(error.message));
}
if (response.statusCode !== 200) {
log.disableProgress();
const message = `${response.statusCode} ${response.body}`;
return reject(wasReported(message, url));
}
result = response;
}));
req.on('progress', (state) => {
let p;
if (state.size && state.size.transferred && state.size.total) {
loadIconData() {
if (!this.iconData) {
this.progressTitle = '下载 Iconfont 数据';
requestProgress(
request('http://ice.alicdn.com/iceland-assets/iconData.json',
(error, response, body) => {
if (!error) {
this.materialProgress = 100;
this.iconData = body;
this.dataLoading = false;
this.progressVisible = false;
this.dataTest();
} else if (this.requestCount < 3) {
this.requestCount++;
this.loadIconData();
} else {
this.requestCount = 0;
this.errorVisible = true;
this.dataLoading = false;
this.progressVisible = false;