ConvertJpegPng.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const fs = require('fs-extra');
  2. const path = require('path');
  3. //const utils = require('../../utils');
  4. const ConvertBase = require('./ConvertBase');
  5. class ConvertJpegPng extends ConvertBase {
  6. check(data, opts) {
  7. const {inputFiles} = opts;
  8. return this.config.useExternalBookConverter &&
  9. inputFiles.sourceFileType &&
  10. (inputFiles.sourceFileType.ext == 'jpg' || inputFiles.sourceFileType.ext == 'png' );
  11. }
  12. async run(data, opts) {
  13. const {inputFiles, uploadFileName, imageFiles} = opts;
  14. if (!imageFiles) {
  15. if (!this.check(data, opts))
  16. return false;
  17. }
  18. let files = [];
  19. if (imageFiles) {
  20. files = imageFiles;
  21. } else {
  22. const imageFile = `${inputFiles.filesDir}/${path.basename(inputFiles.sourceFile)}.${inputFiles.sourceFileType.ext}`;
  23. await fs.copy(inputFiles.sourceFile, imageFile);
  24. files.push({src: imageFile});
  25. }
  26. //читаем изображения
  27. const limitSize = 2*this.config.maxUploadFileSize;
  28. let imagesSize = 0;
  29. const loadImage = async(image) => {
  30. const src = path.parse(image.src);
  31. let type = 'unknown';
  32. switch (src.ext) {
  33. case '.jpg': type = 'image/jpeg'; break;
  34. case '.png': type = 'image/png'; break;
  35. }
  36. if (type != 'unknown') {
  37. image.data = (await fs.readFile(image.src)).toString('base64');
  38. image.type = type;
  39. image.name = src.base;
  40. imagesSize += image.data.length;
  41. if (imagesSize > limitSize) {
  42. throw new Error(`Файл для конвертирования слишком большой|FORLOG| imagesSize: ${imagesSize} > ${limitSize}`);
  43. }
  44. }
  45. }
  46. let images = [];
  47. let loading = [];
  48. files.forEach(img => {
  49. images.push(img);
  50. loading.push(loadImage(img));
  51. });
  52. await Promise.all(loading);
  53. //формируем fb2
  54. let titleInfo = {};
  55. let desc = {_n: 'description', 'title-info': titleInfo};
  56. let pars = [];
  57. let body = {_n: 'body', section: {_a: [pars]}};
  58. let binary = [];
  59. let fb2 = [desc, body, binary];
  60. let title = '';
  61. if (uploadFileName)
  62. title = uploadFileName;
  63. titleInfo['book-title'] = title;
  64. for (const image of images) {
  65. if (image.type) {
  66. const img = {_n: 'binary', _attrs: {id: image.name, 'content-type': image.type}, _t: image.data};
  67. binary.push(img);
  68. const attrs = {'l:href': `#${image.name}`};
  69. if (image.alt) {
  70. image.alt = (image.alt.length > 256 ? image.alt.substring(0, 256) : image.alt);
  71. attrs.alt = image.alt;
  72. }
  73. pars.push({_n: 'p', _t: ''});
  74. pars.push({_n: 'image', _attrs: attrs});
  75. }
  76. }
  77. pars.push({_n: 'p', _t: ''});
  78. return this.formatFb2(fb2);
  79. }
  80. }
  81. module.exports = ConvertJpegPng;