Рефакторинг, добавлена поддержка jpeg, png
This commit is contained in:
@@ -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)}));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user