mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-02-01 03:12:42 +01:00
use abiword with abicommand under unix, spawn abiword processes under windows
This commit is contained in:
parent
e8d84b6058
commit
53242d4660
1 changed files with 105 additions and 27 deletions
|
@ -20,12 +20,17 @@
|
||||||
|
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
var spawn = require('child_process').spawn;
|
var spawn = require('child_process').spawn;
|
||||||
|
var async = require("async");
|
||||||
var settings = require("./Settings");
|
var settings = require("./Settings");
|
||||||
|
var os = require('os');
|
||||||
|
|
||||||
var stdoutBuffer = "";
|
//on windows we have to spawn a process for each convertion, cause the plugin abicommand doesn't exist on this platform
|
||||||
|
if(os.type().indexOf("Windows") > -1)
|
||||||
function doConvertTask(task, callback)
|
|
||||||
{
|
{
|
||||||
|
var stdoutBuffer = "";
|
||||||
|
|
||||||
|
function doConvertTask(task, callback)
|
||||||
|
{
|
||||||
//span an abiword process to perform the conversion
|
//span an abiword process to perform the conversion
|
||||||
var abiword = spawn(settings.abiword, ["--to=" + task.destFile, task.srcFile]);
|
var abiword = spawn(settings.abiword, ["--to=" + task.destFile, task.srcFile]);
|
||||||
|
|
||||||
|
@ -56,9 +61,82 @@ function doConvertTask(task, callback)
|
||||||
|
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.convertFile = function(srcFile, destFile, type, callback)
|
exports.convertFile = function(srcFile, destFile, type, callback)
|
||||||
{
|
{
|
||||||
doConvertTask({"srcFile": srcFile, "destFile": destFile, "type": type}, callback);
|
doConvertTask({"srcFile": srcFile, "destFile": destFile, "type": type}, callback);
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
//on unix operating systems, we can start abiword with abicommand and communicate with it via stdin/stdout
|
||||||
|
//thats much faster, about factor 10
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//Queue with the converts we have to do
|
||||||
|
var queue = async.queue(doConvertTask, 1);
|
||||||
|
|
||||||
|
//spawn the abiword process
|
||||||
|
var abiword = spawn(settings.abiword, ["--plugin", "AbiCommand"]);
|
||||||
|
|
||||||
|
//append error messages to the buffer
|
||||||
|
abiword.stderr.on('data', function (data)
|
||||||
|
{
|
||||||
|
stdoutBuffer += data.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
//throw exceptions if abiword is dieing
|
||||||
|
abiword.on('exit', function (code)
|
||||||
|
{
|
||||||
|
throw "Abiword died with exit code " + code;
|
||||||
|
});
|
||||||
|
|
||||||
|
//delegate the processing of stdout to a other function
|
||||||
|
abiword.stdout.on('data',onAbiwordStdout);
|
||||||
|
|
||||||
|
var stdoutCallback = null;
|
||||||
|
var stdoutBuffer = "";
|
||||||
|
var firstPrompt = true;
|
||||||
|
|
||||||
|
function onAbiwordStdout(data)
|
||||||
|
{
|
||||||
|
//add data to buffer
|
||||||
|
stdoutBuffer+=data.toString();
|
||||||
|
|
||||||
|
//we're searching for the prompt, cause this means everything we need is in the buffer
|
||||||
|
if(stdoutBuffer.search("AbiWord:>") != -1)
|
||||||
|
{
|
||||||
|
//filter the feedback message
|
||||||
|
var err = stdoutBuffer.search("OK") != -1 ? null : stdoutBuffer;
|
||||||
|
|
||||||
|
//reset the buffer
|
||||||
|
stdoutBuffer = "";
|
||||||
|
|
||||||
|
//call the callback with the error message
|
||||||
|
//skip the first prompt
|
||||||
|
if(stdoutCallback != null && !firstPrompt)
|
||||||
|
{
|
||||||
|
stdoutCallback(err);
|
||||||
|
stdoutCallback = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
firstPrompt = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function doConvertTask(task, callback)
|
||||||
|
{
|
||||||
|
abiword.stdin.write("convert " + task.srcFile + " " + task.destFile + " " + task.type + "\n");
|
||||||
|
|
||||||
|
//create a callback that calls the task callback and the caller callback
|
||||||
|
stdoutCallback = function (err)
|
||||||
|
{
|
||||||
|
callback();
|
||||||
|
task.callback(err);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.convertFile = function(srcFile, destFile, type, callback)
|
||||||
|
{
|
||||||
|
queue.push({"srcFile": srcFile, "destFile": destFile, "type": type, "callback": callback});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue