ConvertBase.js 5.1 KB

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