2011-07-19 18:48:11 +00:00
|
|
|
/**
|
|
|
|
* Controls the communication with the Abiword application
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 2011 Peter 'Pita' Martischka
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var spawn = require('child_process').spawn;
|
2011-07-27 18:29:44 +00:00
|
|
|
var settings = require("./Settings");
|
2011-07-19 18:48:11 +00:00
|
|
|
|
|
|
|
var stdoutBuffer = "";
|
|
|
|
|
2011-08-03 22:57:11 +00:00
|
|
|
function doConvertTask(task, callback)
|
2011-07-19 18:48:11 +00:00
|
|
|
{
|
2011-08-03 22:57:11 +00:00
|
|
|
//span an abiword process to perform the conversion
|
|
|
|
var abiword = spawn(settings.abiword, ["--to=" + task.destFile, task.srcFile]);
|
2011-07-19 18:48:11 +00:00
|
|
|
|
2011-08-03 22:57:11 +00:00
|
|
|
//delegate the processing of stdout to another function
|
|
|
|
abiword.stdout.on('data', function (data)
|
|
|
|
{
|
|
|
|
//add data to buffer
|
|
|
|
stdoutBuffer+=data.toString();
|
|
|
|
});
|
|
|
|
|
|
|
|
//append error messages to the buffer
|
|
|
|
abiword.stderr.on('data', function (data)
|
2011-07-19 18:48:11 +00:00
|
|
|
{
|
2011-08-03 22:57:11 +00:00
|
|
|
stdoutBuffer += data.toString();
|
|
|
|
});
|
|
|
|
|
|
|
|
//throw exceptions if abiword is dieing
|
|
|
|
abiword.on('exit', function (code)
|
|
|
|
{
|
|
|
|
if(code != 0) {
|
|
|
|
throw "Abiword died with exit code " + code;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(stdoutBuffer != "")
|
2011-07-19 18:48:11 +00:00
|
|
|
{
|
2011-08-03 22:57:11 +00:00
|
|
|
console.log(stdoutBuffer);
|
2011-07-19 18:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
2011-08-03 22:57:11 +00:00
|
|
|
});
|
2011-07-19 18:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.convertFile = function(srcFile, destFile, type, callback)
|
|
|
|
{
|
2011-08-03 22:57:11 +00:00
|
|
|
doConvertTask({"srcFile": srcFile, "destFile": destFile, "type": type}, callback);
|
2011-07-19 18:48:11 +00:00
|
|
|
};
|