ReaderWorker.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const workerState = require('./workerState');
  2. const FileDetector = require('./FileDetector');
  3. const FileDecompressor = require('./FileDecompressor');
  4. const BookConverter = require('./BookConverter');
  5. const utils = require('./utils');
  6. const fs = require('fs-extra');
  7. const util = require('util');
  8. const stream = require('stream');
  9. const pipeline = util.promisify(stream.pipeline);
  10. const download = require('download');
  11. class ReaderWorker {
  12. constructor(config) {
  13. this.config = Object.assign({}, config);
  14. this.config.tempDownloadDir = `${config.tempDir}/download`;
  15. fs.ensureDirSync(this.config.tempDownloadDir);
  16. this.config.tempPublicDir = `${config.publicDir}/tmp`;
  17. fs.ensureDirSync(this.config.tempPublicDir);
  18. this.detector = new FileDetector();
  19. this.decomp = new FileDecompressor();
  20. this.bookConverter = new BookConverter();
  21. }
  22. async loadBook(url, wState) {
  23. const maxDownloadSize = 10*1024*1024;
  24. let errMes = '';
  25. let decompDir = '';
  26. let downloadedFilename = '';
  27. try {
  28. wState.set({state: 'download', step: 1, totalSteps: 3, url});
  29. const tempFilename = utils.randomHexString(30);
  30. const tempFilename2 = utils.randomHexString(30);
  31. const decompDirname = utils.randomHexString(30);
  32. //download
  33. const d = download(url);
  34. d.on('downloadProgress', progress => {
  35. wState.set({progress: Math.round(progress.percent*100)});
  36. if (progress.transferred > maxDownloadSize) {
  37. errMes = 'file too big';
  38. d.destroy();
  39. }
  40. });
  41. downloadedFilename = `${this.config.tempDownloadDir}/${tempFilename}`;
  42. await pipeline(d, fs.createWriteStream(downloadedFilename));
  43. //decompress
  44. wState.set({state: 'decompress', step: 2, progress: 0});
  45. decompDir = `${this.config.tempDownloadDir}/${decompDirname}`;
  46. const decompFilename = await this.decomp.decompressFile(downloadedFilename, decompDir);
  47. wState.set({progress: 100});
  48. //parse book
  49. wState.set({state: 'parse', step: 3, progress: 0});
  50. const fileType = await this.detector.detectFile(decompFilename);
  51. fileType.url = url;
  52. let resultFilename = `${this.config.tempPublicDir}/${tempFilename2}`;
  53. await this.bookConverter.convertToFb2(decompFilename, resultFilename, fileType, progress => {
  54. wState.set({progress});
  55. });
  56. wState.set({progress: 100});
  57. //finish
  58. wState.finish({path: `/tmp/${tempFilename2}`});
  59. } catch (e) {
  60. wState.set({state: 'error', error: (errMes ? errMes : e.message)});
  61. } finally {
  62. //clean
  63. if (decompDir)
  64. await fs.remove(decompDir);
  65. if (downloadedFilename)
  66. await fs.remove(downloadedFilename);
  67. }
  68. }
  69. loadBookUrl(url) {
  70. const workerId = workerState.generateWorkerId();
  71. const wState = workerState.getControl(workerId);
  72. wState.set({state: 'start'});
  73. this.loadBook(url, wState);
  74. return workerId;
  75. }
  76. }
  77. module.exports = ReaderWorker;