Подготовка к работе с внешними конвертерами
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
const fs = require('fs-extra');
|
||||
const iconv = require('iconv-lite');
|
||||
const chardet = require('chardet');
|
||||
const textUtils = require('./textUtils');
|
||||
@@ -5,12 +6,27 @@ const textUtils = require('./textUtils');
|
||||
class ConvertBase {
|
||||
constructor(config) {
|
||||
this.config = config;
|
||||
|
||||
this.calibrePath = `${config.dataDir}/calibre/ebook-convert`;
|
||||
this.sofficePath = '/usr/bin/soffice';
|
||||
this.pdfToHtmlPath = '/usr/bin/pdftohtml';
|
||||
}
|
||||
|
||||
run(data, opts) {// eslint-disable-line no-unused-vars
|
||||
async run(data, opts) {// eslint-disable-line no-unused-vars
|
||||
//override
|
||||
}
|
||||
|
||||
async checkExternalConverterPresent() {
|
||||
if (!await fs.pathExists(this.calibrePath))
|
||||
throw new Error('Внешний конвертер calibre не найден');
|
||||
|
||||
if (!await fs.pathExists(this.sofficePath))
|
||||
throw new Error('Внешний конвертер LibreOffice не найден');
|
||||
|
||||
if (!await fs.pathExists(this.pdfToHtmlPath))
|
||||
throw new Error('Внешний конвертер pdftohtml не найден');
|
||||
}
|
||||
|
||||
decode(data) {
|
||||
let selected = textUtils.getEncoding(data);
|
||||
|
||||
|
||||
@@ -2,16 +2,29 @@ const ConvertBase = require('./ConvertBase');
|
||||
|
||||
class ConvertDocX extends ConvertBase {
|
||||
check(data, opts) {
|
||||
const {fileType} = opts;
|
||||
const {inputFiles} = opts;
|
||||
|
||||
return (fileType && fileType.ext == 'docx' && this.config.useExternalBookConverter);
|
||||
if (this.config.useExternalBookConverter &&
|
||||
inputFiles.sourceFileType && inputFiles.sourceFileType.ext == 'zip') {
|
||||
//ищем файл '[Content_Types].xml'
|
||||
for (const file of inputFiles.fileList) {
|
||||
if (file == '[Content_Types].xml') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
run(data, opts) {
|
||||
async run(data, opts) {
|
||||
if (!this.check(data, opts))
|
||||
return false;
|
||||
await this.checkExternalConverterPresent();
|
||||
|
||||
return data;
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ class ConvertFb2 extends ConvertBase {
|
||||
return (dataType && dataType.ext == 'xml' && data.toString().indexOf('<FictionBook') >= 0);
|
||||
}
|
||||
|
||||
run(data, opts) {
|
||||
async run(data, opts) {
|
||||
if (!this.check(data, opts))
|
||||
return false;
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ class ConvertHtml extends ConvertBase {
|
||||
return false;
|
||||
}
|
||||
|
||||
run(data, opts) {
|
||||
async run(data, opts) {
|
||||
const checkResult = this.check(data, opts);
|
||||
if (!checkResult)
|
||||
return false;
|
||||
|
||||
@@ -19,7 +19,7 @@ class ConvertSamlib extends ConvertBase {
|
||||
return false;
|
||||
}
|
||||
|
||||
run(data, opts) {
|
||||
async run(data, opts) {
|
||||
const checkResult = this.check(data, opts);
|
||||
if (!checkResult)
|
||||
return false;
|
||||
|
||||
@@ -3,7 +3,7 @@ const FileDetector = require('../FileDetector');
|
||||
|
||||
//порядок важен
|
||||
const convertClassFactory = [
|
||||
//require('./ConvertDocX'),
|
||||
require('./ConvertDocX'),
|
||||
require('./ConvertFb2'),
|
||||
require('./ConvertSamlib'),
|
||||
require('./ConvertHtml'),
|
||||
@@ -25,7 +25,7 @@ class BookConverter {
|
||||
const data = await fs.readFile(inputFiles.selectedFile);
|
||||
let result = false;
|
||||
for (const convert of this.convertFactory) {
|
||||
result = convert.run(data, {inputFiles, url, callback, dataType: selectedFileType});
|
||||
result = await convert.run(data, {inputFiles, url, callback, dataType: selectedFileType});
|
||||
if (result) {
|
||||
await fs.writeFile(outputFile, result);
|
||||
break;
|
||||
|
||||
@@ -19,8 +19,9 @@ class FileDecompressor {
|
||||
|
||||
let result = {
|
||||
sourceFile: filename,
|
||||
sourceFileType: fileType,
|
||||
selectedFile: filename,
|
||||
fileType: fileType,
|
||||
fileListDir: outputDir,
|
||||
fileList: []
|
||||
};
|
||||
|
||||
@@ -58,10 +59,9 @@ class FileDecompressor {
|
||||
if (files.length) {
|
||||
//ищем файл с максимальным размером
|
||||
for (let file of files) {
|
||||
const path = `${outputDir}/${file.path}`;
|
||||
fileList.push(path);
|
||||
fileList.push(file.path);
|
||||
if (file.data.length > max) {
|
||||
sel = path;
|
||||
sel = `${outputDir}/${file.path}`;
|
||||
max = file.data.length;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user