Рефакторинг
This commit is contained in:
@@ -1,40 +1,18 @@
|
|||||||
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) {
|
||||||
this.config = config;
|
this.config = Object.assign({}, config);
|
||||||
this.tempDownloadDir = `${config.tempDir}/download`;
|
this.config.tempDownloadDir = `${config.tempDir}/download`;
|
||||||
fs.ensureDirSync(this.tempDownloadDir);
|
fs.ensureDirSync(this.config.tempDownloadDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadBook(wState, url) {
|
async loadBook(url, wState) {
|
||||||
const maxDownloadSize = 10*1024*1024;
|
const loader = require('./readerLoader');
|
||||||
let errMes = '';
|
loader(url, this.config, (state) => {
|
||||||
try {
|
wState.set(state)
|
||||||
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.tempDownloadDir}/${tempFilename}`));
|
|
||||||
|
|
||||||
wState.finish({step: 3, file: tempFilename});
|
|
||||||
} catch (e) {
|
|
||||||
wState.set({state: 'error', error: (errMes ? errMes : e.message)});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loadBookUrl(url) {
|
loadBookUrl(url) {
|
||||||
@@ -42,7 +20,7 @@ class ReaderWorker {
|
|||||||
const wState = workerState.getControl(workerId);
|
const wState = workerState.getControl(workerId);
|
||||||
wState.set({state: 'start'});
|
wState.set({state: 'start'});
|
||||||
|
|
||||||
this.loadBook(wState, url);
|
this.loadBook(url, wState);
|
||||||
|
|
||||||
return workerId;
|
return workerId;
|
||||||
}
|
}
|
||||||
|
|||||||
32
server/core/readerLoader.js
Normal file
32
server/core/readerLoader.js
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
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;
|
||||||
Reference in New Issue
Block a user