Переход на axios

This commit is contained in:
Book Pauk
2022-07-01 21:38:32 +07:00
parent 12d7843377
commit 61b0712d36

View File

@@ -1,4 +1,4 @@
const got = require('got');
const axios = require('axios');
class FileDownloader {
constructor(limitDownloadSize = 0) {
@@ -7,54 +7,82 @@ class FileDownloader {
async load(url, callback, abort) {
let errMes = '';
const options = {
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'
},
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 {
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) {
errMes = (errMes ? errMes : error.message);
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;