Use abiword --to parameter instead of abicommand for document conversions.

This commit is contained in:
Randy 2011-08-03 15:57:11 -07:00
parent 5b5230b28c
commit e8d84b6058
3 changed files with 63 additions and 62 deletions

View File

@ -23,11 +23,20 @@ var padManager = require("../db/PadManager");
var async = require("async"); var async = require("async");
var fs = require("fs"); var fs = require("fs");
var settings = require('../utils/Settings'); var settings = require('../utils/Settings');
var os = require('os');
//load abiword only if its enabled //load abiword only if its enabled
if(settings.abiword != null) if(settings.abiword != null)
var abiword = require("../utils/Abiword"); var abiword = require("../utils/Abiword");
var tempDirectory = "/tmp";
//tempDirectory changes if the operating system is windows
if(os.type().indexOf("Windows") > -1)
{
tempDirectory = "c:\\Temp";
}
/** /**
* do a requested export * do a requested export
*/ */
@ -76,7 +85,7 @@ exports.doExport = function(req, res, padId, type)
else else
{ {
randNum = Math.floor(Math.random()*new Date().getTime()); randNum = Math.floor(Math.random()*new Date().getTime());
srcFile = "/tmp/eplite_export_" + randNum + ".html"; srcFile = tempDirectory + "/eplite_export_" + randNum + ".html";
fs.writeFile(srcFile, html, callback); fs.writeFile(srcFile, html, callback);
} }
}, },
@ -86,7 +95,7 @@ exports.doExport = function(req, res, padId, type)
//ensure html can be collected by the garbage collector //ensure html can be collected by the garbage collector
html = null; html = null;
destFile = "/tmp/eplite_export_" + randNum + "." + type; destFile = tempDirectory + "/eplite_export_" + randNum + "." + type;
abiword.convertFile(srcFile, destFile, type, callback); abiword.convertFile(srcFile, destFile, type, callback);
}, },
//send the file //send the file
@ -104,7 +113,11 @@ exports.doExport = function(req, res, padId, type)
}, },
function(callback) function(callback)
{ {
fs.unlink(destFile, callback); //100ms delay to accomidate for slow windows fs
setTimeout(function()
{
fs.unlink(destFile, callback);
}, 100);
} }
], callback); ], callback);
} }

View File

@ -24,11 +24,20 @@ var async = require("async");
var fs = require("fs"); var fs = require("fs");
var settings = require('../utils/Settings'); var settings = require('../utils/Settings');
var formidable = require('formidable'); var formidable = require('formidable');
var os = require("os");
//load abiword only if its enabled //load abiword only if its enabled
if(settings.abiword != null) if(settings.abiword != null)
var abiword = require("../utils/Abiword"); var abiword = require("../utils/Abiword");
var tempDirectory = "/tmp/";
//tempDirectory changes if the operating system is windows
if(os.type().indexOf("Windows") > -1)
{
tempDirectory = "c:\\Temp\\";
}
/** /**
* do a requested import * do a requested import
*/ */
@ -48,6 +57,7 @@ exports.doImport = function(req, res, padId)
{ {
var form = new formidable.IncomingForm(); var form = new formidable.IncomingForm();
form.keepExtensions = true; form.keepExtensions = true;
form.uploadDir = tempDirectory;
form.parse(req, function(err, fields, files) form.parse(req, function(err, fields, files)
{ {
@ -94,7 +104,7 @@ exports.doImport = function(req, res, padId)
function(callback) function(callback)
{ {
var randNum = Math.floor(Math.random()*new Date().getTime()); var randNum = Math.floor(Math.random()*new Date().getTime());
destFile = "/tmp/eplite_import_" + randNum + ".txt"; destFile = tempDirectory + "eplite_import_" + randNum + ".txt";
abiword.convertFile(srcFile, destFile, "txt", callback); abiword.convertFile(srcFile, destFile, "txt", callback);
}, },
@ -114,7 +124,13 @@ exports.doImport = function(req, res, padId)
fs.readFile(destFile, "utf8", function(err, _text) fs.readFile(destFile, "utf8", function(err, _text)
{ {
text = _text; text = _text;
callback(err);
//node on windows has a delay on releasing of the file lock.
//We add a 100ms delay to work around this
setTimeout(function()
{
callback(err);
}, 100);
}); });
}, },

View File

@ -20,73 +20,45 @@
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");
//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 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) function doConvertTask(task, callback)
{ {
abiword.stdin.write("convert " + task.srcFile + " " + task.destFile + " " + task.type + "\n"); //span an abiword process to perform the conversion
var abiword = spawn(settings.abiword, ["--to=" + task.destFile, task.srcFile]);
//create a callback that calls the task callback and the caller callback //delegate the processing of stdout to another function
stdoutCallback = function (err) 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)
{
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 != "")
{
console.log(stdoutBuffer);
}
callback(); callback();
task.callback(err); });
};
} }
exports.convertFile = function(srcFile, destFile, type, callback) exports.convertFile = function(srcFile, destFile, type, callback)
{ {
queue.push({"srcFile": srcFile, "destFile": destFile, "type": type, "callback": callback}); doConvertTask({"srcFile": srcFile, "destFile": destFile, "type": type}, callback);
}; };