Добавил конвертер для msdoc
This commit is contained in:
@@ -32,13 +32,20 @@ class ConvertBase {
|
|||||||
async execConverter(path, args, onData) {
|
async execConverter(path, args, onData) {
|
||||||
try {
|
try {
|
||||||
const result = await utils.spawnProcess(path, {args, onData});
|
const result = await utils.spawnProcess(path, {args, onData});
|
||||||
if (result.code != 0)
|
if (result.code != 0) {
|
||||||
throw new Error(`Внешний конвертер завершился с ошибкой: ${result.code}`);
|
let error = result.code;
|
||||||
|
if (this.config.branch == 'development')
|
||||||
|
error = `exec: ${path}, stdout: ${result.stdout}, stderr: ${result.stderr}`;
|
||||||
|
throw new Error(`Внешний конвертер завершился с ошибкой: ${error}`);
|
||||||
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
if (e.status == 'killed')
|
if (e.status == 'killed') {
|
||||||
throw new Error('Слишком долгое ожидание конвертера');
|
throw new Error('Слишком долгое ожидание конвертера');
|
||||||
else
|
} else if (e.status == 'error') {
|
||||||
throw new Error(e.error);
|
throw new Error(e.error);
|
||||||
|
} else {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
35
server/core/BookConverter/ConvertDoc.js
Normal file
35
server/core/BookConverter/ConvertDoc.js
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
const fs = require('fs-extra');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const ConvertDocX = require('./ConvertDocX');
|
||||||
|
|
||||||
|
class ConvertDoc extends ConvertDocX {
|
||||||
|
check(data, opts) {
|
||||||
|
const {inputFiles} = opts;
|
||||||
|
|
||||||
|
return this.config.useExternalBookConverter &&
|
||||||
|
inputFiles.sourceFileType && inputFiles.sourceFileType.ext == 'msi';
|
||||||
|
}
|
||||||
|
|
||||||
|
async run(data, opts) {
|
||||||
|
if (!this.check(data, opts))
|
||||||
|
return false;
|
||||||
|
await this.checkExternalConverterPresent();
|
||||||
|
|
||||||
|
const {inputFiles, callback} = opts;
|
||||||
|
|
||||||
|
const outFile = `${inputFiles.fileListDir}/${path.basename(inputFiles.sourceFile)}`;
|
||||||
|
const docFile = `${outFile}.doc`;
|
||||||
|
const docxFile = `${outFile}.docx`;
|
||||||
|
const fb2File = `${outFile}.fb2`;
|
||||||
|
|
||||||
|
await fs.copy(inputFiles.sourceFile, docFile);
|
||||||
|
await this.execConverter(this.sofficePath, ['--headless', '--convert-to', 'docx', '--outdir', inputFiles.fileListDir, docFile], (data) => {
|
||||||
|
console.log(data.toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
return await super.convert(docxFile, fb2File, callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ConvertDoc;
|
||||||
@@ -20,6 +20,16 @@ class ConvertDocX extends ConvertBase {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async convert(docxFile, fb2File, callback) {
|
||||||
|
let perc = 0;
|
||||||
|
await this.execConverter(this.calibrePath, [docxFile, fb2File], () => {
|
||||||
|
perc = (perc < 100 ? perc + 5 : 50);
|
||||||
|
callback(perc);
|
||||||
|
});
|
||||||
|
|
||||||
|
return await fs.readFile(fb2File);
|
||||||
|
}
|
||||||
|
|
||||||
async run(data, opts) {
|
async run(data, opts) {
|
||||||
if (!this.check(data, opts))
|
if (!this.check(data, opts))
|
||||||
return false;
|
return false;
|
||||||
@@ -33,13 +43,7 @@ class ConvertDocX extends ConvertBase {
|
|||||||
|
|
||||||
await fs.copy(inputFiles.sourceFile, docxFile);
|
await fs.copy(inputFiles.sourceFile, docxFile);
|
||||||
|
|
||||||
let perc = 0;
|
return await this.convert(docxFile, fb2File, callback);
|
||||||
await this.execConverter(this.calibrePath, [docxFile, fb2File], () => {
|
|
||||||
perc = (perc < 100 ? perc + 5 : 50);
|
|
||||||
callback(perc);
|
|
||||||
});
|
|
||||||
|
|
||||||
return await fs.readFile(fb2File);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ const FileDetector = require('../FileDetector');
|
|||||||
//порядок важен
|
//порядок важен
|
||||||
const convertClassFactory = [
|
const convertClassFactory = [
|
||||||
require('./ConvertDocX'),
|
require('./ConvertDocX'),
|
||||||
|
require('./ConvertDoc'),
|
||||||
require('./ConvertFb2'),
|
require('./ConvertFb2'),
|
||||||
require('./ConvertSamlib'),
|
require('./ConvertSamlib'),
|
||||||
require('./ConvertHtml'),
|
require('./ConvertHtml'),
|
||||||
|
|||||||
Reference in New Issue
Block a user