diff --git a/node/handler/ExportHandler.js b/node/handler/ExportHandler.js index e128fd7b..674950f4 100644 --- a/node/handler/ExportHandler.js +++ b/node/handler/ExportHandler.js @@ -23,11 +23,20 @@ var padManager = require("../db/PadManager"); var async = require("async"); var fs = require("fs"); var settings = require('../utils/Settings'); +var os = require('os'); //load abiword only if its enabled if(settings.abiword != null) 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 */ @@ -52,7 +61,7 @@ exports.doExport = function(req, res, padId, type) var html; var randNum; var srcFile, destFile; - + async.series([ //render the html document function(callback) @@ -76,7 +85,7 @@ exports.doExport = function(req, res, padId, type) else { 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); } }, @@ -86,7 +95,7 @@ exports.doExport = function(req, res, padId, type) //ensure html can be collected by the garbage collector html = null; - destFile = "/tmp/eplite_export_" + randNum + "." + type; + destFile = tempDirectory + "/eplite_export_" + randNum + "." + type; abiword.convertFile(srcFile, destFile, type, callback); }, //send the file @@ -104,7 +113,11 @@ exports.doExport = function(req, res, padId, type) }, function(callback) { - fs.unlink(destFile, callback); + //100ms delay to accomidate for slow windows fs + setTimeout(function() + { + fs.unlink(destFile, callback); + }, 100); } ], callback); } diff --git a/node/handler/ImportHandler.js b/node/handler/ImportHandler.js index 489ee1b0..c02c9f50 100644 --- a/node/handler/ImportHandler.js +++ b/node/handler/ImportHandler.js @@ -24,10 +24,19 @@ var async = require("async"); var fs = require("fs"); var settings = require('../utils/Settings'); var formidable = require('formidable'); +var os = require("os"); //load abiword only if its enabled if(settings.abiword != null) 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 @@ -48,6 +57,7 @@ exports.doImport = function(req, res, padId) { var form = new formidable.IncomingForm(); form.keepExtensions = true; + form.uploadDir = tempDirectory; form.parse(req, function(err, fields, files) { @@ -94,7 +104,7 @@ exports.doImport = function(req, res, padId) function(callback) { 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); }, @@ -114,7 +124,13 @@ exports.doImport = function(req, res, padId) fs.readFile(destFile, "utf8", function(err, _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); }); }, diff --git a/node/utils/Abiword.js b/node/utils/Abiword.js index edac4648..326cb767 100644 --- a/node/utils/Abiword.js +++ b/node/utils/Abiword.js @@ -20,73 +20,45 @@ var util = require('util'); var spawn = require('child_process').spawn; -var async = require("async"); 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 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"); + //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 - stdoutCallback = function (err) + //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) + { + 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(); - task.callback(err); - }; + }); } 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); };