Browse Source

Рефакторинг

Book Pauk 6 years ago
parent
commit
15177d3fed
2 changed files with 26 additions and 36 deletions
  1. 26 4
      server/core/ReaderWorker.js
  2. 0 32
      server/core/readerLoader.js

+ 26 - 4
server/core/ReaderWorker.js

@@ -1,5 +1,11 @@
 const workerState = require('./workerState');
 const workerState = require('./workerState');
+const utils = require('./utils');
+
 const fs = require('fs-extra');
 const fs = require('fs-extra');
+const util = require('util');
+const stream = require('stream');
+const pipeline = util.promisify(stream.pipeline);
+const download = require('download');
 
 
 class ReaderWorker {
 class ReaderWorker {
     constructor(config) {
     constructor(config) {
@@ -9,10 +15,26 @@ class ReaderWorker {
     }
     }
 
 
     async loadBook(url, wState) {
     async loadBook(url, wState) {
-        const loader = require('./readerLoader');
-        loader(url, this.config, (state) => {
-            wState.set(state)
-        });
+        const maxDownloadSize = 10*1024*1024;
+        let errMes = '';
+        try {
+            wState.set({state: 'download', step: 1, totalSteps: 3, url});
+
+            const tempFilename = utils.randomHexString(30);
+            const d = download(url);
+            d.on('downloadProgress', progress => {
+                wState.set({progress:  Math.round(progress.percent*100)});
+                if (progress.transferred > maxDownloadSize) {
+                    errMes = 'file too big';
+                    d.destroy();
+                }
+            });
+            await pipeline(d, fs.createWriteStream(`${this.config.tempDownloadDir}/${tempFilename}`));
+            
+            wState.finish({step: 3, file: tempFilename});
+        } catch (e) {
+            wState.set({state: 'error', error: (errMes ? errMes : e.message)});
+        }
     }
     }
 
 
     loadBookUrl(url) {
     loadBookUrl(url) {

+ 0 - 32
server/core/readerLoader.js

@@ -1,32 +0,0 @@
-const utils = require('./utils');
-
-const fs = require('fs-extra');
-const util = require('util');
-const stream = require('stream');
-const pipeline = util.promisify(stream.pipeline);
-const download = require('download');
-
-async function main(url, config, setState) {
-    const maxDownloadSize = 10*1024*1024;
-    let errMes = '';
-    try {
-        setState({state: 'download', step: 1, totalSteps: 3, url});
-
-        const tempFilename = utils.randomHexString(30);
-        const d = download(url);
-        d.on('downloadProgress', progress => {
-            setState({progress:  Math.round(progress.percent*100)});
-            if (progress.transferred > maxDownloadSize) {
-                errMes = 'file too big';
-                d.destroy();
-            }
-        });
-        await pipeline(d, fs.createWriteStream(`${config.tempDownloadDir}/${tempFilename}`));
-        
-        setState({state: 'finish', step: 3, file: tempFilename});
-    } catch (e) {
-        setState({state: 'error', error: (errMes ? errMes : e.message)});
-    }
-}
-
-module.exports = main;