ConvertDjvu.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const fs = require('fs-extra');
  2. const path = require('path');
  3. const utils = require('../../utils');
  4. const ConvertHtml = require('./ConvertHtml');
  5. class ConvertDjvu extends ConvertHtml {
  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. const loadImage = async(image) => {
  48. image.data = (await fs.readFile(image.file)).toString('base64');
  49. image.name = path.basename(image.file);
  50. }
  51. let files = [];
  52. await utils.findFiles(async(file) => {
  53. if (path.extname(file) == '.jpg')
  54. files.push({name: file, base: path.basename(file)});
  55. }, dir);
  56. files.sort((a, b) => a.base.localeCompare(b.base));
  57. let images = [];
  58. let loading = [];
  59. files.forEach(f => {
  60. const image = {file: f.name};
  61. images.push(image);
  62. loading.push(loadImage(image));
  63. });
  64. await Promise.all(loading);
  65. //формируем текст
  66. limitSize = 2*this.config.maxUploadFileSize;
  67. let title = '';
  68. if (uploadFileName)
  69. title = uploadFileName;
  70. let text = `<title>${title}</title>`;
  71. for (const image of images) {
  72. text += `<fb2-image type="image/jpeg" name="${image.name}">${image.data}</fb2-image>`;
  73. if (text.length > limitSize) {
  74. throw new Error(`Файл для конвертирования слишком большой|FORLOG| text.length: ${text.length} > ${limitSize}`);
  75. }
  76. }
  77. return await super.run(Buffer.from(text), {skipCheck: true, isText: true, cutTitle: true});
  78. }
  79. }
  80. module.exports = ConvertDjvu;