ReaderWorker.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const fs = require('fs-extra');
  2. const path = require('path');
  3. const crypto = require('crypto');
  4. const workerState = require('./workerState');
  5. const FileDownloader = require('./FileDownloader');
  6. const FileDecompressor = require('./FileDecompressor');
  7. const BookConverter = require('./BookConverter');
  8. const utils = require('./utils');
  9. class ReaderWorker {
  10. constructor(config) {
  11. this.config = Object.assign({}, config);
  12. this.config.tempDownloadDir = `${config.tempDir}/download`;
  13. fs.ensureDirSync(this.config.tempDownloadDir);
  14. this.config.tempPublicDir = `${config.publicDir}/tmp`;
  15. fs.ensureDirSync(this.config.tempPublicDir);
  16. this.down = new FileDownloader();
  17. this.decomp = new FileDecompressor();
  18. this.bookConverter = new BookConverter();
  19. }
  20. async loadBook(url, wState) {
  21. let errMes = '';
  22. let decompDir = '';
  23. let downloadedFilename = '';
  24. let convertFilename = '';
  25. try {
  26. wState.set({state: 'download', step: 1, totalSteps: 3, url});
  27. const tempFilename = utils.randomHexString(30);
  28. const tempFilename2 = utils.randomHexString(30);
  29. const decompDirname = utils.randomHexString(30);
  30. //download
  31. const downdata = await this.down.load(url, (progress) => {
  32. wState.set({progress});
  33. });
  34. downloadedFilename = `${this.config.tempDownloadDir}/${tempFilename}`;
  35. await fs.writeFile(downloadedFilename, downdata);
  36. wState.set({progress: 100});
  37. //decompress
  38. wState.set({state: 'decompress', step: 2, progress: 0});
  39. decompDir = `${this.config.tempDownloadDir}/${decompDirname}`;
  40. const decompFilename = await this.decomp.decompressFile(downloadedFilename, decompDir);
  41. wState.set({progress: 100});
  42. //parse book
  43. wState.set({state: 'convert', step: 3, progress: 0});
  44. convertFilename = `${this.config.tempDownloadDir}/${tempFilename2}`;
  45. await this.bookConverter.convertToFb2(decompFilename, convertFilename, url, progress => {
  46. wState.set({progress});
  47. });
  48. //compress file to tmp dir, if not exists with the same hashname
  49. const compFilename = await this.decomp.gzipFileIfNotExists(convertFilename, `${this.config.tempPublicDir}`);
  50. wState.set({progress: 100});
  51. //finish
  52. const finishFilename = path.basename(compFilename);
  53. wState.finish({path: `/tmp/${finishFilename}`});
  54. } catch (e) {
  55. wState.set({state: 'error', error: (errMes ? errMes : e.message)});
  56. } finally {
  57. //clean
  58. if (decompDir)
  59. await fs.remove(decompDir);
  60. if (downloadedFilename)
  61. await fs.remove(downloadedFilename);
  62. if (convertFilename)
  63. await fs.remove(convertFilename);
  64. }
  65. }
  66. loadBookUrl(url) {
  67. const workerId = workerState.generateWorkerId();
  68. const wState = workerState.getControl(workerId);
  69. wState.set({state: 'start'});
  70. this.loadBook(url, wState);
  71. return workerId;
  72. }
  73. async saveFile(file) {
  74. const buf = await fs.readFile(file.path);
  75. const hash = crypto.createHash('sha256').update(buf).digest('hex');
  76. const outFilename = `${this.config.uploadDir}/${hash}`;
  77. if (!await fs.pathExists(outFilename)) {
  78. await fs.move(file.path, outFilename);
  79. } else {
  80. await fs.utimes(outFilename, Date.now()/1000, Date.now()/1000);
  81. await fs.remove(file.path);
  82. }
  83. return `file://${hash}`;
  84. }
  85. }
  86. module.exports = ReaderWorker;