Эх сурвалжийг харах

Переход на axios

Book Pauk 3 жил өмнө
parent
commit
61b0712d36

+ 59 - 31
server/core/FileDownloader.js

@@ -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'}));
+        try {
+            const res = await axios.get(url, options);
 
-        let estSize = 0;
-        if (response.headers['content-length']) {
-            estSize = response.headers['content-length'];
-        }
+            let estSize = 0;
+            if (res.headers['content-length']) {
+                estSize = res.headers['content-length'];
+            }
 
-        let prevProg = 0;
-        const request = got(url, options);
+            if (estSize > this.limitDownloadSize) {
+                throw new Error('Файл слишком большой');
+            }
+
+            let prevProg = 0;
+            let transferred = 0;
 
-        request.on('downloadProgress', progress => {
-            if (this.limitDownloadSize) {
-                if (progress.transferred > this.limitDownloadSize) {
-                    errMes = 'Файл слишком большой';
-                    request.cancel();
+            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(progress.transferred/estSize*100);
-            else if (progress.transferred)
-                prog = Math.round(progress.transferred/(progress.transferred + 200000)*100);
+                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 (prog != prevProg && callback)
+                    callback(prog);
+                prevProg = prog;
 
-            if (abort && abort()) {
-                errMes = 'abort';
-                request.cancel();
-            }
-        });
+                if (abort && abort()) {
+                    errMes = 'abort';
+                    res.request.abort();
+                }
+            });
 
-        try {
-            return (await request).body;
+            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;