Добавил конвертер для msdoc

This commit is contained in:
Book Pauk
2019-02-27 21:14:47 +07:00
parent a5fe61078d
commit 3253858c7f
4 changed files with 58 additions and 11 deletions

View File

@@ -32,13 +32,20 @@ class ConvertBase {
async execConverter(path, args, onData) {
try {
const result = await utils.spawnProcess(path, {args, onData});
if (result.code != 0)
throw new Error(`Внешний конвертер завершился с ошибкой: ${result.code}`);
if (result.code != 0) {
let error = result.code;
if (this.config.branch == 'development')
error = `exec: ${path}, stdout: ${result.stdout}, stderr: ${result.stderr}`;
throw new Error(`Внешний конвертер завершился с ошибкой: ${error}`);
}
} catch(e) {
if (e.status == 'killed')
if (e.status == 'killed') {
throw new Error('Слишком долгое ожидание конвертера');
else
} else if (e.status == 'error') {
throw new Error(e.error);
} else {
throw new Error(e);
}
}
}

View 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;

View File

@@ -20,6 +20,16 @@ class ConvertDocX extends ConvertBase {
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) {
if (!this.check(data, opts))
return false;
@@ -33,13 +43,7 @@ class ConvertDocX extends ConvertBase {
await fs.copy(inputFiles.sourceFile, docxFile);
let perc = 0;
await this.execConverter(this.calibrePath, [docxFile, fb2File], () => {
perc = (perc < 100 ? perc + 5 : 50);
callback(perc);
});
return await fs.readFile(fb2File);
return await this.convert(docxFile, fb2File, callback);
}
}

View File

@@ -4,6 +4,7 @@ const FileDetector = require('../FileDetector');
//порядок важен
const convertClassFactory = [
require('./ConvertDocX'),
require('./ConvertDoc'),
require('./ConvertFb2'),
require('./ConvertSamlib'),
require('./ConvertHtml'),