Рефакторинг

This commit is contained in:
Book Pauk
2019-01-13 15:20:03 +07:00
parent 47b4bd9838
commit d040dd0718
2 changed files with 42 additions and 15 deletions

View File

@@ -0,0 +1,34 @@
const got = require('got');
class FileDownloader {
constructor() {
}
async load(url, callback) {
const maxDownloadSize = 10*1024*1024;
let estSize = 100000;
const request = got(url).on('downloadProgress', progress => {
if (progress.transferred > maxDownloadSize) {
request.cancel();
}
const prog = Math.round(progress.transferred/estSize*100);
if (callback)
callback(prog);
if (prog > 100)
estSize *= 1.5;
});
try {
return (await request).body;
} catch (error) {
if (request.isCanceled) {
throw new Error('file too big')
}
throw error;
}
}
}
module.exports = FileDownloader;