Переход на axios
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
const got = require('got');
|
const axios = require('axios');
|
||||||
|
|
||||||
class FileDownloader {
|
class FileDownloader {
|
||||||
constructor(limitDownloadSize = 0) {
|
constructor(limitDownloadSize = 0) {
|
||||||
@@ -7,54 +7,82 @@ class FileDownloader {
|
|||||||
|
|
||||||
async load(url, callback, abort) {
|
async load(url, callback, abort) {
|
||||||
let errMes = '';
|
let errMes = '';
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
headers: {
|
headers: {
|
||||||
'user-agent': 'Mozilla/5.0 (X11; HasCodingOs 1.0; Linux x64) AppleWebKit/637.36 (KHTML, like Gecko) Chrome/70.0.3112.101 Safari/637.36 HasBrowser/5.0'
|
'user-agent': 'Mozilla/5.0 (X11; HasCodingOs 1.0; Linux x64) AppleWebKit/637.36 (KHTML, like Gecko) Chrome/70.0.3112.101 Safari/637.36 HasBrowser/5.0'
|
||||||
},
|
},
|
||||||
responseType: 'buffer',
|
responseType: 'stream',
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await got(url, Object.assign({}, options, {method: 'HEAD'}));
|
|
||||||
|
|
||||||
let estSize = 0;
|
|
||||||
if (response.headers['content-length']) {
|
|
||||||
estSize = response.headers['content-length'];
|
|
||||||
}
|
|
||||||
|
|
||||||
let prevProg = 0;
|
|
||||||
const request = got(url, options);
|
|
||||||
|
|
||||||
request.on('downloadProgress', progress => {
|
|
||||||
if (this.limitDownloadSize) {
|
|
||||||
if (progress.transferred > this.limitDownloadSize) {
|
|
||||||
errMes = 'Файл слишком большой';
|
|
||||||
request.cancel();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let prog = 0;
|
|
||||||
if (estSize)
|
|
||||||
prog = Math.round(progress.transferred/estSize*100);
|
|
||||||
else if (progress.transferred)
|
|
||||||
prog = Math.round(progress.transferred/(progress.transferred + 200000)*100);
|
|
||||||
|
|
||||||
if (prog != prevProg && callback)
|
|
||||||
callback(prog);
|
|
||||||
prevProg = prog;
|
|
||||||
|
|
||||||
if (abort && abort()) {
|
|
||||||
errMes = 'abort';
|
|
||||||
request.cancel();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return (await request).body;
|
const res = await axios.get(url, options);
|
||||||
|
|
||||||
|
let estSize = 0;
|
||||||
|
if (res.headers['content-length']) {
|
||||||
|
estSize = res.headers['content-length'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (estSize > this.limitDownloadSize) {
|
||||||
|
throw new Error('Файл слишком большой');
|
||||||
|
}
|
||||||
|
|
||||||
|
let prevProg = 0;
|
||||||
|
let transferred = 0;
|
||||||
|
|
||||||
|
const download = this.streamToBuffer(res.data, (chunk) => {
|
||||||
|
transferred += chunk.length;
|
||||||
|
if (this.limitDownloadSize) {
|
||||||
|
if (transferred > this.limitDownloadSize) {
|
||||||
|
errMes = 'Файл слишком большой';
|
||||||
|
res.request.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let prog = 0;
|
||||||
|
if (estSize)
|
||||||
|
prog = Math.round(transferred/estSize*100);
|
||||||
|
else
|
||||||
|
prog = Math.round(transferred/(transferred + 200000)*100);
|
||||||
|
|
||||||
|
if (prog != prevProg && callback)
|
||||||
|
callback(prog);
|
||||||
|
prevProg = prog;
|
||||||
|
|
||||||
|
if (abort && abort()) {
|
||||||
|
errMes = 'abort';
|
||||||
|
res.request.abort();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return await download;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
errMes = (errMes ? errMes : error.message);
|
errMes = (errMes ? errMes : error.message);
|
||||||
throw new Error(errMes);
|
throw new Error(errMes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
streamToBuffer(stream, progress) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
|
if (!progress)
|
||||||
|
progress = () => {};
|
||||||
|
|
||||||
|
const _buf = [];
|
||||||
|
|
||||||
|
stream.on('data', (chunk) => {
|
||||||
|
_buf.push(chunk);
|
||||||
|
progress(chunk);
|
||||||
|
});
|
||||||
|
stream.on('end', () => resolve(Buffer.concat(_buf)));
|
||||||
|
stream.on('error', (err) => {
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
stream.on('aborted', () => {
|
||||||
|
reject(new Error('aborted'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = FileDownloader;
|
module.exports = FileDownloader;
|
||||||
Reference in New Issue
Block a user