ConvertBase.js 4.2 KB

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