ConvertDjvu.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const fs = require('fs-extra');
  2. const path = require('path');
  3. const utils = require('../../utils');
  4. const ConvertJpegPng = require('./ConvertJpegPng');
  5. class ConvertDjvu extends ConvertJpegPng {
  6. check(data, opts) {
  7. const {inputFiles} = opts;
  8. return this.config.useExternalBookConverter &&
  9. inputFiles.sourceFileType && inputFiles.sourceFileType.ext == 'djvu';
  10. }
  11. async run(data, opts) {
  12. if (!this.check(data, opts))
  13. return false;
  14. const {inputFiles, callback, abort} = opts;
  15. const ddjvuPath = '/usr/bin/ddjvu';
  16. if (!await fs.pathExists(ddjvuPath))
  17. throw new Error('Внешний конвертер ddjvu не найден');
  18. const djvusedPath = '/usr/bin/djvused';
  19. if (!await fs.pathExists(djvusedPath))
  20. throw new Error('Внешний конвертер djvused не найден');
  21. const tiffsplitPath = '/usr/bin/tiffsplit';
  22. if (!await fs.pathExists(tiffsplitPath))
  23. throw new Error('Внешний конвертер tiffsplitPath не найден');
  24. const mogrifyPath = '/usr/bin/mogrify';
  25. if (!await fs.pathExists(mogrifyPath))
  26. throw new Error('Внешний конвертер mogrifyPath не найден');
  27. const dir = `${inputFiles.filesDir}/`;
  28. const baseFile = `${dir}${path.basename(inputFiles.sourceFile)}`;
  29. const tifFile = `${baseFile}.tif`;
  30. //конвертируем в tiff
  31. let perc = 0;
  32. await this.execConverter(ddjvuPath, ['-format=tiff', '-quality=50', '-verbose', inputFiles.sourceFile, tifFile], () => {
  33. perc = (perc < 100 ? perc + 1 : 40);
  34. callback(perc);
  35. }, abort);
  36. const tifFileSize = (await fs.stat(tifFile)).size;
  37. let limitSize = 4*this.config.maxUploadFileSize;
  38. if (tifFileSize > limitSize) {
  39. throw new Error(`Файл для конвертирования слишком большой|FORLOG| tifFileSize: ${tifFileSize} > ${limitSize}`);
  40. }
  41. //разбиваем на файлы
  42. await this.execConverter(tiffsplitPath, [tifFile, dir], null, abort);
  43. await fs.remove(tifFile);
  44. //конвертируем в jpg
  45. await this.execConverter(mogrifyPath, ['-quality', '20', '-scale', '2048>', '-verbose', '-format', 'jpg', `${dir}*.tif`], () => {
  46. perc = (perc < 100 ? perc + 1 : 40);
  47. callback(perc);
  48. }, abort);
  49. //ищем изображения
  50. let files = [];
  51. await utils.findFiles(async(file) => {
  52. if (path.extname(file) == '.jpg')
  53. files.push({name: file, base: path.basename(file)});
  54. }, dir);
  55. files.sort((a, b) => a.base.localeCompare(b.base));
  56. //схема документа (outline)
  57. const djvusedResult = await this.execConverter(djvusedPath, ['-u', '-e', 'print-outline', inputFiles.sourceFile]);
  58. const outline = [];
  59. const lines = djvusedResult.stdout.match(/\(".*"\s*?"#\d+".*?\)/g);
  60. if (lines) {
  61. lines.forEach(l => {
  62. const m = l.match(/"(.*)"\s*?"#(\d+)"/);
  63. if (m) {
  64. outline[m[2]] = m[1];
  65. }
  66. });
  67. }
  68. await utils.sleep(100);
  69. let i = 0;
  70. const imageFiles = files.map(f => {
  71. i++;
  72. let alt = (outline[i] ? outline[i] : '');
  73. return {src: f.name, alt};
  74. });
  75. return await super.run(data, Object.assign({}, opts, {imageFiles}));
  76. }
  77. }
  78. module.exports = ConvertDjvu;