ConvertBase.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const fs = require('fs-extra');
  2. const iconv = require('iconv-lite');
  3. const chardet = require('chardet');
  4. const he = require('he');
  5. const textUtils = require('./textUtils');
  6. const utils = require('../utils');
  7. let execConverterCounter = 0;
  8. class ConvertBase {
  9. constructor(config) {
  10. this.config = config;
  11. this.calibrePath = `${config.dataDir}/calibre/ebook-convert`;
  12. this.sofficePath = '/usr/bin/soffice';
  13. this.pdfToHtmlPath = '/usr/bin/pdftohtml';
  14. }
  15. async run(data, opts) {// eslint-disable-line no-unused-vars
  16. //override
  17. }
  18. async checkExternalConverterPresent() {
  19. if (!await fs.pathExists(this.calibrePath))
  20. throw new Error('Внешний конвертер calibre не найден');
  21. if (!await fs.pathExists(this.sofficePath))
  22. throw new Error('Внешний конвертер LibreOffice не найден');
  23. if (!await fs.pathExists(this.pdfToHtmlPath))
  24. throw new Error('Внешний конвертер pdftohtml не найден');
  25. }
  26. async execConverter(path, args, onData) {
  27. execConverterCounter++;
  28. try {
  29. if (execConverterCounter > 10)
  30. throw new Error('Слишком большая очередь конвертирования. Пожалуйста, попробуйте позже.');
  31. const result = await utils.spawnProcess(path, {args, onData});
  32. if (result.code != 0) {
  33. let error = result.code;
  34. if (this.config.branch == 'development')
  35. error = `exec: ${path}, stdout: ${result.stdout}, stderr: ${result.stderr}`;
  36. throw new Error(`Внешний конвертер завершился с ошибкой: ${error}`);
  37. }
  38. } catch(e) {
  39. if (e.status == 'killed') {
  40. throw new Error('Слишком долгое ожидание конвертера');
  41. } else if (e.status == 'error') {
  42. throw new Error(e.error);
  43. } else {
  44. throw new Error(e);
  45. }
  46. } finally {
  47. execConverterCounter--;
  48. }
  49. }
  50. decode(data) {
  51. let selected = textUtils.getEncoding(data);
  52. if (selected == 'ISO-8859-5') {
  53. const charsetAll = chardet.detectAll(data.slice(0, 20000));
  54. for (const charset of charsetAll) {
  55. if (charset.name.indexOf('ISO-8859') < 0) {
  56. selected = charset.name;
  57. break;
  58. }
  59. }
  60. }
  61. if (selected.toLowerCase() != 'utf-8')
  62. return iconv.decode(data, selected);
  63. else
  64. return data;
  65. }
  66. repSpaces(text) {
  67. return text.replace(/&nbsp;|[\t\n\r]/g, ' ');
  68. }
  69. escapeEntities(text) {
  70. return he.escape(he.decode(text));
  71. }
  72. formatFb2(fb2) {
  73. let out = '<?xml version="1.0" encoding="utf-8"?>';
  74. out += '<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink">';
  75. out += this.formatFb2Node(fb2);
  76. out += '</FictionBook>';
  77. return out;
  78. }
  79. formatFb2Node(node, name) {
  80. let out = '';
  81. if (Array.isArray(node)) {
  82. for (const n of node) {
  83. out += this.formatFb2Node(n);
  84. }
  85. } else if (typeof node == 'string') {
  86. if (name)
  87. out += `<${name}>${this.repSpaces(node)}</${name}>`;
  88. else
  89. out += this.repSpaces(node);
  90. } else {
  91. if (node._n)
  92. name = node._n;
  93. let attrs = '';
  94. if (node._attrs) {
  95. for (let attrName in node._attrs) {
  96. attrs += ` ${attrName}="${node._attrs[attrName]}"`;
  97. }
  98. }
  99. let tOpen = '';
  100. let tBody = '';
  101. let tClose = '';
  102. if (name)
  103. tOpen += `<${name}${attrs}>`;
  104. if (node.hasOwnProperty('_t'))
  105. tBody += this.repSpaces(node._t);
  106. for (let nodeName in node) {
  107. if (nodeName && nodeName[0] == '_' && nodeName != '_a')
  108. continue;
  109. const n = node[nodeName];
  110. tBody += this.formatFb2Node(n, nodeName);
  111. }
  112. if (name)
  113. tClose += `</${name}>`;
  114. if (attrs == '' && name == 'p' && tBody.trim() == '')
  115. out += '<empty-line/>'
  116. else
  117. out += `${tOpen}${tBody}${tClose}`;
  118. }
  119. return out;
  120. }
  121. }
  122. module.exports = ConvertBase;