ConvertBase.js 5.4 KB

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