Рефакторинг, добавлена поддержка jpeg, png
This commit is contained in:
@@ -2,9 +2,9 @@ const fs = require('fs-extra');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const utils = require('../../utils');
|
const utils = require('../../utils');
|
||||||
|
|
||||||
const ConvertBase = require('./ConvertBase');
|
const ConvertJpegPng = require('./ConvertJpegPng');
|
||||||
|
|
||||||
class ConvertDjvu extends ConvertBase {
|
class ConvertDjvu extends ConvertJpegPng {
|
||||||
check(data, opts) {
|
check(data, opts) {
|
||||||
const {inputFiles} = opts;
|
const {inputFiles} = opts;
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ class ConvertDjvu extends ConvertBase {
|
|||||||
if (!this.check(data, opts))
|
if (!this.check(data, opts))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
const {inputFiles, callback, abort, uploadFileName} = opts;
|
const {inputFiles, callback, abort} = opts;
|
||||||
|
|
||||||
const ddjvuPath = '/usr/bin/ddjvu';
|
const ddjvuPath = '/usr/bin/ddjvu';
|
||||||
if (!await fs.pathExists(ddjvuPath))
|
if (!await fs.pathExists(ddjvuPath))
|
||||||
@@ -31,8 +31,8 @@ class ConvertDjvu extends ConvertBase {
|
|||||||
throw new Error('Внешний конвертер mogrifyPath не найден');
|
throw new Error('Внешний конвертер mogrifyPath не найден');
|
||||||
|
|
||||||
const dir = `${inputFiles.filesDir}/`;
|
const dir = `${inputFiles.filesDir}/`;
|
||||||
const inpFile = `${dir}${path.basename(inputFiles.sourceFile)}`;
|
const baseFile = `${dir}${path.basename(inputFiles.sourceFile)}`;
|
||||||
const tifFile = `${inpFile}.tif`;
|
const tifFile = `${baseFile}.tif`;
|
||||||
|
|
||||||
//конвертируем в tiff
|
//конвертируем в tiff
|
||||||
let perc = 0;
|
let perc = 0;
|
||||||
@@ -44,7 +44,7 @@ class ConvertDjvu extends ConvertBase {
|
|||||||
const tifFileSize = (await fs.stat(tifFile)).size;
|
const tifFileSize = (await fs.stat(tifFile)).size;
|
||||||
let limitSize = 4*this.config.maxUploadFileSize;
|
let limitSize = 4*this.config.maxUploadFileSize;
|
||||||
if (tifFileSize > limitSize) {
|
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);
|
callback(perc);
|
||||||
}, abort);
|
}, 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 = [];
|
let files = [];
|
||||||
await utils.findFiles(async(file) => {
|
await utils.findFiles(async(file) => {
|
||||||
if (path.extname(file) == '.jpg')
|
if (path.extname(file) == '.jpg')
|
||||||
@@ -80,39 +67,8 @@ class ConvertDjvu extends ConvertBase {
|
|||||||
|
|
||||||
files.sort((a, b) => a.base.localeCompare(b.base));
|
files.sort((a, b) => a.base.localeCompare(b.base));
|
||||||
|
|
||||||
let images = [];
|
await utils.sleep(100);
|
||||||
let loading = [];
|
return await super.run(data, Object.assign({}, opts, {imageFiles: files.map(f => f.name)}));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
95
server/core/Reader/BookConverter/ConvertJpegPng.js
Normal file
95
server/core/Reader/BookConverter/ConvertJpegPng.js
Normal file
@@ -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;
|
||||||
@@ -3,6 +3,7 @@ const FileDetector = require('../../FileDetector');
|
|||||||
|
|
||||||
//порядок важен
|
//порядок важен
|
||||||
const convertClassFactory = [
|
const convertClassFactory = [
|
||||||
|
require('./ConvertJpegPng'),
|
||||||
require('./ConvertEpub'),
|
require('./ConvertEpub'),
|
||||||
require('./ConvertDjvu'),
|
require('./ConvertDjvu'),
|
||||||
require('./ConvertPdf'),
|
require('./ConvertPdf'),
|
||||||
|
|||||||
Reference in New Issue
Block a user