diff --git a/server/core/Reader/BookConverter/ConvertDjvu.js b/server/core/Reader/BookConverter/ConvertDjvu.js index ec645223..22f899f2 100644 --- a/server/core/Reader/BookConverter/ConvertDjvu.js +++ b/server/core/Reader/BookConverter/ConvertDjvu.js @@ -2,9 +2,9 @@ const fs = require('fs-extra'); const path = require('path'); const utils = require('../../utils'); -const ConvertBase = require('./ConvertBase'); +const ConvertJpegPng = require('./ConvertJpegPng'); -class ConvertDjvu extends ConvertBase { +class ConvertDjvu extends ConvertJpegPng { check(data, opts) { const {inputFiles} = opts; @@ -16,7 +16,7 @@ class ConvertDjvu extends ConvertBase { if (!this.check(data, opts)) return false; - const {inputFiles, callback, abort, uploadFileName} = opts; + const {inputFiles, callback, abort} = opts; const ddjvuPath = '/usr/bin/ddjvu'; if (!await fs.pathExists(ddjvuPath)) @@ -31,8 +31,8 @@ class ConvertDjvu extends ConvertBase { throw new Error('Внешний конвертер mogrifyPath не найден'); const dir = `${inputFiles.filesDir}/`; - const inpFile = `${dir}${path.basename(inputFiles.sourceFile)}`; - const tifFile = `${inpFile}.tif`; + const baseFile = `${dir}${path.basename(inputFiles.sourceFile)}`; + const tifFile = `${baseFile}.tif`; //конвертируем в tiff let perc = 0; @@ -44,7 +44,7 @@ class ConvertDjvu extends ConvertBase { const tifFileSize = (await fs.stat(tifFile)).size; let limitSize = 4*this.config.maxUploadFileSize; if (tifFileSize > limitSize) { - throw new Error(`Файл для конвертирования слишком большой|FORLOG| ${tifFileSize} > ${limitSize}`); + throw new Error(`Файл для конвертирования слишком большой|FORLOG| tifFileSize: ${tifFileSize} > ${limitSize}`); } //разбиваем на файлы @@ -58,20 +58,7 @@ class ConvertDjvu extends ConvertBase { callback(perc); }, abort); - //читаем изображения - limitSize = 2*this.config.maxUploadFileSize; - let imagesSize = 0; - - const loadImage = async(image) => { - image.data = (await fs.readFile(image.file)).toString('base64'); - image.name = path.basename(image.file); - - imagesSize += image.data.length; - if (imagesSize > limitSize) { - throw new Error(`Файл для конвертирования слишком большой|FORLOG| imagesSize: ${imagesSize} > ${limitSize}`); - } - } - + //ищем изображения let files = []; await utils.findFiles(async(file) => { if (path.extname(file) == '.jpg') @@ -80,39 +67,8 @@ class ConvertDjvu extends ConvertBase { files.sort((a, b) => a.base.localeCompare(b.base)); - let images = []; - let loading = []; - files.forEach(f => { - const image = {file: f.name}; - images.push(image); - loading.push(loadImage(image)); - }); - - await Promise.all(loading); - - //формируем fb2 - let titleInfo = {}; - let desc = {_n: 'description', 'title-info': titleInfo}; - let pars = []; - let body = {_n: 'body', section: {_a: [pars]}}; - let binary = []; - let fb2 = [desc, body, binary]; - - let title = ''; - if (uploadFileName) - title = uploadFileName; - - titleInfo['book-title'] = title; - - for (const image of images) { - const img = {_n: 'binary', _attrs: {id: image.name, 'content-type': 'image/jpeg'}, _t: image.data}; - binary.push(img); - - pars.push({_n: 'p', _t: ''}); - pars.push({_n: 'image', _attrs: {'l:href': `#${image.name}`}}); - } - - return this.formatFb2(fb2); + await utils.sleep(100); + return await super.run(data, Object.assign({}, opts, {imageFiles: files.map(f => f.name)})); } } diff --git a/server/core/Reader/BookConverter/ConvertJpegPng.js b/server/core/Reader/BookConverter/ConvertJpegPng.js new file mode 100644 index 00000000..8a8debeb --- /dev/null +++ b/server/core/Reader/BookConverter/ConvertJpegPng.js @@ -0,0 +1,95 @@ +const fs = require('fs-extra'); +const path = require('path'); +//const utils = require('../../utils'); + +const ConvertBase = require('./ConvertBase'); + +class ConvertJpegPng extends ConvertBase { + check(data, opts) { + const {inputFiles} = opts; + + return this.config.useExternalBookConverter && + inputFiles.sourceFileType && + (inputFiles.sourceFileType.ext == 'jpg' || inputFiles.sourceFileType.ext == 'png' ); + } + + async run(data, opts) { + const {inputFiles, uploadFileName, imageFiles} = opts; + + if (!imageFiles) { + if (!this.check(data, opts)) + return false; + } + + let files = []; + if (imageFiles) { + files = imageFiles; + } else { + const imageFile = `${inputFiles.filesDir}/${path.basename(inputFiles.sourceFile)}.${inputFiles.sourceFileType.ext}`; + await fs.copy(inputFiles.sourceFile, imageFile); + files.push(imageFile); + } + + //читаем изображения + const limitSize = 2*this.config.maxUploadFileSize; + let imagesSize = 0; + + const loadImage = async(image) => { + const src = path.parse(image.src); + let type = 'unknown'; + switch (src.ext) { + case '.jpg': type = 'image/jpeg'; break; + case '.png': type = 'image/png'; break; + } + if (type != 'unknown') { + image.data = (await fs.readFile(image.src)).toString('base64'); + image.type = type; + image.name = src.base; + + imagesSize += image.data.length; + if (imagesSize > limitSize) { + throw new Error(`Файл для конвертирования слишком большой|FORLOG| imagesSize: ${imagesSize} > ${limitSize}`); + } + } + } + + let images = []; + let loading = []; + files.forEach(f => { + const image = {src: f}; + images.push(image); + loading.push(loadImage(image)); + }); + + await Promise.all(loading); + + //формируем fb2 + let titleInfo = {}; + let desc = {_n: 'description', 'title-info': titleInfo}; + let pars = []; + let body = {_n: 'body', section: {_a: [pars]}}; + let binary = []; + let fb2 = [desc, body, binary]; + + let title = ''; + if (uploadFileName) + title = uploadFileName; + + titleInfo['book-title'] = title; + + for (const image of images) { + if (image.type) { + const img = {_n: 'binary', _attrs: {id: image.name, 'content-type': image.type}, _t: image.data}; + binary.push(img); + + pars.push({_n: 'p', _t: ''}); + pars.push({_n: 'image', _attrs: {'l:href': `#${image.name}`}}); + } + } + pars.push({_n: 'p', _t: ''}); + + return this.formatFb2(fb2); + } +} + +module.exports = ConvertJpegPng; diff --git a/server/core/Reader/BookConverter/index.js b/server/core/Reader/BookConverter/index.js index ae753bac..2f9e9abb 100644 --- a/server/core/Reader/BookConverter/index.js +++ b/server/core/Reader/BookConverter/index.js @@ -3,6 +3,7 @@ const FileDetector = require('../../FileDetector'); //порядок важен const convertClassFactory = [ + require('./ConvertJpegPng'), require('./ConvertEpub'), require('./ConvertDjvu'), require('./ConvertPdf'),