From ee8ba7537179e37451c43bf642147e691c37cd21 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Fri, 4 Dec 2020 18:24:08 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D1=8B=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=BD=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D1=82=D0=B5=D1=80=D0=B0=20Djvu,=20=D1=82=D0=B5?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D1=8C=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= =?UTF-8?q?=D0=B5=D1=82=20=D0=B1=D1=8B=D1=81=D1=82=D1=80=D0=B5=D0=B5=20?= =?UTF-8?q?=D0=B8=20=D0=B1=D0=B5=D0=B7=20=D0=BF=D1=80=D0=BE=D0=BC=D0=B5?= =?UTF-8?q?=D0=B6=D1=83=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D0=B2=D0=B5=D1=80=D1=82=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B2=20pdf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/Reader/BookConverter/ConvertBase.js | 4 - .../core/Reader/BookConverter/ConvertDjvu.js | 82 ++++++++++++++++--- 2 files changed, 70 insertions(+), 16 deletions(-) diff --git a/server/core/Reader/BookConverter/ConvertBase.js b/server/core/Reader/BookConverter/ConvertBase.js index 9fd6ceee..047587d5 100644 --- a/server/core/Reader/BookConverter/ConvertBase.js +++ b/server/core/Reader/BookConverter/ConvertBase.js @@ -15,7 +15,6 @@ class ConvertBase { this.calibrePath = `${config.dataDir}/calibre/ebook-convert`; this.sofficePath = '/usr/bin/soffice'; this.pdfToHtmlPath = '/usr/bin/pdftohtml'; - this.ddjvuPath = '/usr/bin/ddjvu'; } async run(data, opts) {// eslint-disable-line no-unused-vars @@ -31,9 +30,6 @@ class ConvertBase { if (!await fs.pathExists(this.pdfToHtmlPath)) throw new Error('Внешний конвертер pdftohtml не найден'); - - if (!await fs.pathExists(this.ddjvuPath)) - throw new Error('Внешний конвертер ddjvu не найден'); } async execConverter(path, args, onData, abort) { diff --git a/server/core/Reader/BookConverter/ConvertDjvu.js b/server/core/Reader/BookConverter/ConvertDjvu.js index b6890c6c..fb17ab8b 100644 --- a/server/core/Reader/BookConverter/ConvertDjvu.js +++ b/server/core/Reader/BookConverter/ConvertDjvu.js @@ -1,9 +1,10 @@ const fs = require('fs-extra'); const path = require('path'); +const utils = require('../../utils'); -const ConvertPdf = require('./ConvertPdf'); +const ConvertHtml = require('./ConvertHtml'); -class ConvertRtf extends ConvertPdf { +class ConvertDjvu extends ConvertHtml { check(data, opts) { const {inputFiles} = opts; @@ -14,26 +15,83 @@ class ConvertRtf extends ConvertPdf { async run(data, opts) { if (!this.check(data, opts)) return false; - await this.checkExternalConverterPresent(); - const {inputFiles, callback, abort} = opts; + const {inputFiles, callback, abort, uploadFileName} = opts; - const outFile = `${inputFiles.filesDir}/${path.basename(inputFiles.sourceFile)}`; - const pdfFile = `${outFile}.pdf`; + const ddjvuPath = '/usr/bin/ddjvu'; + if (!await fs.pathExists(ddjvuPath)) + throw new Error('Внешний конвертер ddjvu не найден'); + const tiffsplitPath = '/usr/bin/tiffsplit'; + if (!await fs.pathExists(tiffsplitPath)) + throw new Error('Внешний конвертер tiffsplitPath не найден'); + + const mogrifyPath = '/usr/bin/mogrify'; + if (!await fs.pathExists(mogrifyPath)) + throw new Error('Внешний конвертер mogrifyPath не найден'); + + const dir = `${inputFiles.filesDir}/`; + const inpFile = `${dir}${path.basename(inputFiles.sourceFile)}`; + const tifFile = `${inpFile}.tif`; + + //конвертируем в tiff let perc = 0; - await this.execConverter(this.ddjvuPath, ['-format=pdf', '-quality=85', '-verbose', inputFiles.sourceFile, pdfFile], () => { + await this.execConverter(ddjvuPath, ['-format=tiff', '-quality=50', '-verbose', inputFiles.sourceFile, tifFile], () => { perc = (perc < 100 ? perc + 1 : 40); callback(perc); }, abort); - const pdfFileSize = (await fs.stat(pdfFile)).size; - if (pdfFileSize > 2*this.config.maxUploadFileSize) { - throw new Error(`Файл для конвертирования слишком большой|FORLOG| ${pdfFileSize} > ${2*this.config.maxUploadFileSize}`); + const tifFileSize = (await fs.stat(tifFile)).size; + const limitSize = 3*this.config.maxUploadFileSize; + if (tifFileSize > limitSize) { + throw new Error(`Файл для конвертирования слишком большой|FORLOG| ${tifFileSize} > ${limitSize}`); } - return await super.run(null, Object.assign({}, opts, {pdfFile, skipCheck: true})); + //разбиваем на файлы + await this.execConverter(tiffsplitPath, [tifFile, dir], null, abort); + + await fs.remove(tifFile); + + //конвертируем в jpg + await this.execConverter(mogrifyPath, ['-quality', '20', '-verbose', '-format', 'jpg', `${dir}*.tif`], () => { + perc = (perc < 100 ? perc + 1 : 40); + callback(perc); + }, abort); + + //читаем изображения + const loadImage = async(image) => { + image.data = (await fs.readFile(image.file)).toString('base64'); + image.name = path.basename(image.file); + } + + let files = []; + await utils.findFiles(async(file) => { + if (path.extname(file) == '.jpg') + files.push({name: file, base: path.basename(file)}); + }, dir); + + 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); + + //формируем текст + let title = ''; + if (uploadFileName) + title = uploadFileName; + let text = `${title}`; + for (const image of images) { + text += `${image.data}`; + } + return await super.run(Buffer.from(text), {skipCheck: true, isText: true, cutTitle: true}); } } -module.exports = ConvertRtf; +module.exports = ConvertDjvu;