ConvertBase.js 3.9 KB

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