2021-01-21 21:06:52 +00:00
|
|
|
'use strict';
|
2011-07-19 19:48:11 +01:00
|
|
|
/**
|
|
|
|
* Controls the communication with the Abiword application
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 15:26:41 +01:00
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2011-07-19 19:48:11 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const spawn = require('child_process').spawn;
|
|
|
|
const async = require('async');
|
|
|
|
const settings = require('./Settings');
|
|
|
|
const os = require('os');
|
|
|
|
|
|
|
|
let doConvertTask;
|
|
|
|
|
2021-01-21 21:06:52 +00:00
|
|
|
// on windows we have to spawn a process for each convertion,
|
|
|
|
// cause the plugin abicommand doesn't exist on this platform
|
2020-11-23 13:24:19 -05:00
|
|
|
if (os.type().indexOf('Windows') > -1) {
|
2021-01-21 21:06:52 +00:00
|
|
|
doConvertTask = (task, callback) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
const abiword = spawn(settings.abiword, [`--to=${task.destFile}`, task.srcFile]);
|
2021-03-17 18:23:32 -04:00
|
|
|
let stdoutBuffer = '';
|
2021-03-17 18:17:07 -04:00
|
|
|
abiword.stdout.on('data', (data) => { stdoutBuffer += data.toString(); });
|
|
|
|
abiword.stderr.on('data', (data) => { stdoutBuffer += data.toString(); });
|
2020-11-23 13:24:19 -05:00
|
|
|
abiword.on('exit', (code) => {
|
2021-03-17 18:28:39 -04:00
|
|
|
if (code !== 0) return callback(new Error(`Abiword died with exit code ${code}`));
|
2021-01-21 21:06:52 +00:00
|
|
|
if (stdoutBuffer !== '') {
|
2011-08-04 13:35:42 +01:00
|
|
|
console.log(stdoutBuffer);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
2013-03-24 01:18:44 +01:00
|
|
|
};
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2021-01-21 21:06:52 +00:00
|
|
|
exports.convertFile = (srcFile, destFile, type, callback) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
doConvertTask({srcFile, destFile, type}, callback);
|
2011-08-04 13:35:42 +01:00
|
|
|
};
|
2021-01-21 21:06:52 +00:00
|
|
|
// on unix operating systems, we can start abiword with abicommand and
|
|
|
|
// communicate with it via stdin/stdout
|
|
|
|
// thats much faster, about factor 10
|
|
|
|
} else {
|
2020-11-23 13:24:19 -05:00
|
|
|
let abiword;
|
|
|
|
let stdoutCallback = null;
|
2021-01-21 21:06:52 +00:00
|
|
|
const spawnAbiword = () => {
|
2020-11-23 13:24:19 -05:00
|
|
|
abiword = spawn(settings.abiword, ['--plugin', 'AbiCommand']);
|
|
|
|
let stdoutBuffer = '';
|
|
|
|
let firstPrompt = true;
|
2021-03-17 18:17:07 -04:00
|
|
|
abiword.stderr.on('data', (data) => { stdoutBuffer += data.toString(); });
|
2020-11-23 13:24:19 -05:00
|
|
|
abiword.on('exit', (code) => {
|
2012-02-27 00:22:53 +01:00
|
|
|
spawnAbiword();
|
2021-03-17 18:28:39 -04:00
|
|
|
stdoutCallback(new Error(`Abiword died with exit code ${code}`));
|
2012-02-27 00:22:53 +01:00
|
|
|
});
|
2020-11-23 13:24:19 -05:00
|
|
|
abiword.stdout.on('data', (data) => {
|
|
|
|
stdoutBuffer += data.toString();
|
|
|
|
// we're searching for the prompt, cause this means everything we need is in the buffer
|
2021-01-21 21:06:52 +00:00
|
|
|
if (stdoutBuffer.search('AbiWord:>') !== -1) {
|
2021-03-17 18:28:39 -04:00
|
|
|
const err = stdoutBuffer.search('OK') !== -1 ? null : new Error(stdoutBuffer);
|
2020-11-23 13:24:19 -05:00
|
|
|
stdoutBuffer = '';
|
|
|
|
if (stdoutCallback != null && !firstPrompt) {
|
2012-02-27 00:22:53 +01:00
|
|
|
stdoutCallback(err);
|
|
|
|
stdoutCallback = null;
|
|
|
|
}
|
|
|
|
firstPrompt = false;
|
2011-08-04 13:35:42 +01:00
|
|
|
}
|
2012-02-27 00:22:53 +01:00
|
|
|
});
|
2013-03-24 01:18:44 +01:00
|
|
|
};
|
2012-02-27 00:22:53 +01:00
|
|
|
spawnAbiword();
|
2011-07-19 19:48:11 +01:00
|
|
|
|
2021-01-21 21:06:52 +00:00
|
|
|
doConvertTask = (task, callback) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
abiword.stdin.write(`convert ${task.srcFile} ${task.destFile} ${task.type}\n`);
|
2021-01-21 21:06:52 +00:00
|
|
|
stdoutCallback = (err) => {
|
2011-08-04 13:35:42 +01:00
|
|
|
callback();
|
2020-11-23 13:24:19 -05:00
|
|
|
console.log('queue continue');
|
|
|
|
try {
|
2013-04-15 23:01:08 +01:00
|
|
|
task.callback(err);
|
2020-11-23 13:24:19 -05:00
|
|
|
} catch (e) {
|
|
|
|
console.error('Abiword File failed to convert', e);
|
2013-04-15 23:01:08 +01:00
|
|
|
}
|
2011-08-04 13:35:42 +01:00
|
|
|
};
|
2013-03-24 01:18:44 +01:00
|
|
|
};
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const queue = async.queue(doConvertTask, 1);
|
2021-01-21 21:06:52 +00:00
|
|
|
exports.convertFile = (srcFile, destFile, type, callback) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
queue.push({srcFile, destFile, type, callback});
|
2011-08-04 13:35:42 +01:00
|
|
|
};
|
2011-07-19 19:48:11 +01:00
|
|
|
}
|