ConvertDocX.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const fs = require('fs-extra');
  2. const path = require('path');
  3. const ConvertBase = require('./ConvertBase');
  4. class ConvertDocX extends ConvertBase {
  5. check(data, opts) {
  6. const {inputFiles} = opts;
  7. if (this.config.useExternalBookConverter &&
  8. inputFiles.sourceFileType && inputFiles.sourceFileType.ext == 'zip') {
  9. //ищем файл '[Content_Types].xml'
  10. for (const file of inputFiles.files) {
  11. if (file.path == '[Content_Types].xml') {
  12. return true;
  13. }
  14. }
  15. }
  16. return false;
  17. }
  18. async convert(docxFile, fb2File, callback) {
  19. let perc = 0;
  20. await this.execConverter(this.calibrePath, [docxFile, fb2File], () => {
  21. perc = (perc < 100 ? perc + 5 : 50);
  22. callback(perc);
  23. });
  24. return await fs.readFile(fb2File);
  25. }
  26. async run(data, opts) {
  27. if (!this.check(data, opts))
  28. return false;
  29. await this.checkExternalConverterPresent();
  30. const {inputFiles, callback} = opts;
  31. const outFile = `${inputFiles.filesDir}/${path.basename(inputFiles.sourceFile)}`;
  32. const docxFile = `${outFile}.docx`;
  33. const fb2File = `${outFile}.fb2`;
  34. await fs.copy(inputFiles.sourceFile, docxFile);
  35. return await this.convert(docxFile, fb2File, callback);
  36. }
  37. }
  38. module.exports = ConvertDocX;