ConvertBase.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. let q = null;
  29. try {
  30. q = await queue.get(() => {onData();});
  31. } catch (e) {
  32. throw new Error('Слишком большая очередь конвертирования. Пожалуйста, попробуйте позже.');
  33. }
  34. try {
  35. const result = await utils.spawnProcess(path, {args, onData, abort});
  36. if (result.code != 0) {
  37. let error = result.code;
  38. if (this.config.branch == 'development')
  39. error = `exec: ${path}, stdout: ${result.stdout}, stderr: ${result.stderr}`;
  40. throw new Error(`Внешний конвертер завершился с ошибкой: ${error}`);
  41. }
  42. } catch(e) {
  43. if (e.status == 'killed') {
  44. throw new Error('Слишком долгое ожидание конвертера');
  45. } else if (e.status == 'abort') {
  46. throw new Error('abort');
  47. } else if (e.status == 'error') {
  48. throw new Error(e.error);
  49. } else {
  50. throw new Error(e);
  51. }
  52. } finally {
  53. q.ret();
  54. }
  55. }
  56. decode(data) {
  57. let selected = textUtils.getEncoding(data);
  58. if (selected == 'ISO-8859-5') {
  59. const charsetAll = chardet.detectAll(data.slice(0, 20000));
  60. for (const charset of charsetAll) {
  61. if (charset.name.indexOf('ISO-8859') < 0) {
  62. selected = charset.name;
  63. break;
  64. }
  65. }
  66. }
  67. if (selected.toLowerCase() != 'utf-8')
  68. return iconv.decode(data, selected);
  69. else
  70. return data;
  71. }
  72. repSpaces(text) {
  73. return text.replace(/&nbsp;|[\t\n\r]/g, ' ');
  74. }
  75. escapeEntities(text) {
  76. return he.escape(he.decode(text.replace(/&nbsp;/g, ' ')));
  77. }
  78. formatFb2(fb2) {
  79. let out = '<?xml version="1.0" encoding="utf-8"?>';
  80. out += '<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink">';
  81. out += this.formatFb2Node(fb2);
  82. out += '</FictionBook>';
  83. return out;
  84. }
  85. formatFb2Node(node, name) {
  86. let out = '';
  87. if (Array.isArray(node)) {
  88. for (const n of node) {
  89. out += this.formatFb2Node(n);
  90. }
  91. } else if (typeof node == 'string') {
  92. if (name)
  93. out += `<${name}>${this.repSpaces(node)}</${name}>`;
  94. else
  95. out += this.repSpaces(node);
  96. } else {
  97. if (node._n)
  98. name = node._n;
  99. let attrs = '';
  100. if (node._attrs) {
  101. for (let attrName in node._attrs) {
  102. attrs += ` ${attrName}="${node._attrs[attrName]}"`;
  103. }
  104. }
  105. let tOpen = '';
  106. let tBody = '';
  107. let tClose = '';
  108. if (name)
  109. tOpen += `<${name}${attrs}>`;
  110. if (node.hasOwnProperty('_t'))
  111. tBody += this.repSpaces(node._t);
  112. for (let nodeName in node) {
  113. if (nodeName && nodeName[0] == '_' && nodeName != '_a')
  114. continue;
  115. const n = node[nodeName];
  116. tBody += this.formatFb2Node(n, nodeName);
  117. }
  118. if (name)
  119. tClose += `</${name}>`;
  120. if (attrs == '' && name == 'p' && tBody.trim() == '')
  121. out += '<empty-line/>'
  122. else
  123. out += `${tOpen}${tBody}${tClose}`;
  124. }
  125. return out;
  126. }
  127. }
  128. module.exports = ConvertBase;