ConvertMobi.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. const fs = require('fs-extra');
  2. const path = require('path');
  3. const ConvertBase = require('./ConvertBase');
  4. class ConvertMobi extends ConvertBase {
  5. async check(data, opts) {
  6. const {inputFiles} = opts;
  7. return (this.config.useExternalBookConverter &&
  8. inputFiles.sourceFileType && inputFiles.sourceFileType.ext == 'mobi');
  9. }
  10. async run(data, opts) {
  11. if (!await this.check(data, opts))
  12. return false;
  13. await this.checkExternalConverterPresent();
  14. const {inputFiles, callback, abort} = opts;
  15. const outFile = `${inputFiles.filesDir}/${path.basename(inputFiles.sourceFile)}`;
  16. const mobiFile = `${outFile}.mobi`;
  17. const fb2File = `${outFile}.fb2`;
  18. await fs.copy(inputFiles.sourceFile, mobiFile);
  19. let perc = 0;
  20. await this.execConverter(this.calibrePath, [mobiFile, fb2File, '-vv'], () => {
  21. perc = (perc < 100 ? perc + 1 : 50);
  22. callback(perc);
  23. }, abort);
  24. return await fs.readFile(fb2File);
  25. }
  26. }
  27. module.exports = ConvertMobi;