ConvertDjvu.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const fs = require('fs-extra');
  2. const path = require('path');
  3. const utils = require('../../utils');
  4. const ConvertBase = require('./ConvertBase');
  5. class ConvertDjvu extends ConvertBase {
  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, uploadFileName} = opts;
  15. const ddjvuPath = '/usr/bin/ddjvu';
  16. if (!await fs.pathExists(ddjvuPath))
  17. throw new Error('Внешний конвертер ddjvu не найден');
  18. const tiffsplitPath = '/usr/bin/tiffsplit';
  19. if (!await fs.pathExists(tiffsplitPath))
  20. throw new Error('Внешний конвертер tiffsplitPath не найден');
  21. const mogrifyPath = '/usr/bin/mogrify';
  22. if (!await fs.pathExists(mogrifyPath))
  23. throw new Error('Внешний конвертер mogrifyPath не найден');
  24. const dir = `${inputFiles.filesDir}/`;
  25. const inpFile = `${dir}${path.basename(inputFiles.sourceFile)}`;
  26. const tifFile = `${inpFile}.tif`;
  27. //конвертируем в tiff
  28. let perc = 0;
  29. await this.execConverter(ddjvuPath, ['-format=tiff', '-quality=50', '-verbose', inputFiles.sourceFile, tifFile], () => {
  30. perc = (perc < 100 ? perc + 1 : 40);
  31. callback(perc);
  32. }, abort);
  33. const tifFileSize = (await fs.stat(tifFile)).size;
  34. let limitSize = 3*this.config.maxUploadFileSize;
  35. if (tifFileSize > limitSize) {
  36. throw new Error(`Файл для конвертирования слишком большой|FORLOG| ${tifFileSize} > ${limitSize}`);
  37. }
  38. //разбиваем на файлы
  39. await this.execConverter(tiffsplitPath, [tifFile, dir], null, abort);
  40. await fs.remove(tifFile);
  41. //конвертируем в jpg
  42. await this.execConverter(mogrifyPath, ['-quality', '20', '-scale', '2048', '-verbose', '-format', 'jpg', `${dir}*.tif`], () => {
  43. perc = (perc < 100 ? perc + 1 : 40);
  44. callback(perc);
  45. }, abort);
  46. //читаем изображения
  47. limitSize = 2*this.config.maxUploadFileSize;
  48. let imagesSize = 0;
  49. const loadImage = async(image) => {
  50. image.data = (await fs.readFile(image.file)).toString('base64');
  51. image.name = path.basename(image.file);
  52. imagesSize += image.data.length;
  53. if (imagesSize > limitSize) {
  54. throw new Error(`Файл для конвертирования слишком большой|FORLOG| imagesSize: ${imagesSize} > ${limitSize}`);
  55. }
  56. }
  57. let files = [];
  58. await utils.findFiles(async(file) => {
  59. if (path.extname(file) == '.jpg')
  60. files.push({name: file, base: path.basename(file)});
  61. }, dir);
  62. files.sort((a, b) => a.base.localeCompare(b.base));
  63. let images = [];
  64. let loading = [];
  65. files.forEach(f => {
  66. const image = {file: f.name};
  67. images.push(image);
  68. loading.push(loadImage(image));
  69. });
  70. await Promise.all(loading);
  71. //формируем fb2
  72. let titleInfo = {};
  73. let desc = {_n: 'description', 'title-info': titleInfo};
  74. let pars = [];
  75. let body = {_n: 'body', section: {_a: [pars]}};
  76. let binary = [];
  77. let fb2 = [desc, body, binary];
  78. let title = '';
  79. if (uploadFileName)
  80. title = uploadFileName;
  81. titleInfo['book-title'] = title;
  82. for (const image of images) {
  83. const img = {_n: 'binary', _attrs: {id: image.name, 'content-type': 'image/jpeg'}, _t: image.data};
  84. binary.push(img);
  85. pars.push({_n: 'p', _t: ''});
  86. pars.push({_n: 'image', _attrs: {'l:href': `#${image.name}`}});
  87. }
  88. return this.formatFb2(fb2);
  89. }
  90. }
  91. module.exports = ConvertDjvu;