From ed8aff22d431dc80926300a019bc9e0439e99370 Mon Sep 17 00:00:00 2001 From: Peter 'Pita' Martischka Date: Mon, 27 Feb 2012 00:22:53 +0100 Subject: [PATCH 1/3] restart abiword on crash and give the user feedback about bad import files --- node/handler/ImportHandler.js | 22 +++++++--- node/utils/Abiword.js | 83 ++++++++++++++++++----------------- static/js/pad_impexp.js | 65 +++++++-------------------- 3 files changed, 75 insertions(+), 95 deletions(-) diff --git a/node/handler/ImportHandler.js b/node/handler/ImportHandler.js index 06a2ce5c..3dccb8af 100644 --- a/node/handler/ImportHandler.js +++ b/node/handler/ImportHandler.js @@ -115,7 +115,15 @@ exports.doImport = function(req, res, padId) { var randNum = Math.floor(Math.random()*0xFFFFFFFF); destFile = tempDirectory + "eplite_import_" + randNum + ".txt"; - abiword.convertFile(srcFile, destFile, "txt", callback); + abiword.convertFile(srcFile, destFile, "txt", function(err){ + //catch convert errors + if(err){ + console.warn("Converting Error:", err); + return callback("convertFailed"); + } else { + callback(); + } + }); }, //get the pad object @@ -176,16 +184,18 @@ exports.doImport = function(req, res, padId) } ], function(err) { - //the upload failed, there is nothing we can do, send a 500 - if(err == "uploadFailed") + var status = "ok"; + + //check for known errors and replace the status + if(err == "uploadFailed" || err == "convertFailed") { - res.send(500); - return; + status = err; + err = null; } ERR(err); //close the connection - res.send("ok"); + res.send("", 200); }); } diff --git a/node/utils/Abiword.js b/node/utils/Abiword.js index c53a6ab3..27138e64 100644 --- a/node/utils/Abiword.js +++ b/node/utils/Abiword.js @@ -53,7 +53,7 @@ if(os.type().indexOf("Windows") > -1) abiword.on('exit', function (code) { if(code != 0) { - throw "Abiword died with exit code " + code; + return callback("Abiword died with exit code " + code); } if(stdoutBuffer != "") @@ -75,52 +75,54 @@ if(os.type().indexOf("Windows") > -1) else { //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 abiword; var stdoutCallback = null; - var stdoutBuffer = ""; - var firstPrompt = true; + var spawnAbiword = function (){ + abiword = spawn(settings.abiword, ["--plugin", "AbiCommand"]); + 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) + //append error messages to the buffer + abiword.stderr.on('data', function (data) { - //filter the feedback message - var err = stdoutBuffer.search("OK") != -1 ? null : stdoutBuffer; + stdoutBuffer += data.toString(); + }); + + //abiword died, let's restart abiword and return an error with the callback + abiword.on('exit', function (code) + { + spawnAbiword(); + stdoutCallback("Abiword died with exit code " + code); + }); + + //delegate the processing of stdout to a other function + abiword.stdout.on('data',function (data) + { + //add data to buffer + stdoutBuffer+=data.toString(); - //reset the buffer - stdoutBuffer = ""; - - //call the callback with the error message - //skip the first prompt - if(stdoutCallback != null && !firstPrompt) + //we're searching for the prompt, cause this means everything we need is in the buffer + if(stdoutBuffer.search("AbiWord:>") != -1) { - stdoutCallback(err); - stdoutCallback = null; + //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; } - - firstPrompt = false; - } + }); } + spawnAbiword(); doConvertTask = function(task, callback) { @@ -130,6 +132,7 @@ else stdoutCallback = function (err) { callback(); + console.log("queue continue"); task.callback(err); }; } diff --git a/static/js/pad_impexp.js b/static/js/pad_impexp.js index 6fe42c70..7ec0b344 100644 --- a/static/js/pad_impexp.js +++ b/static/js/pad_impexp.js @@ -95,11 +95,6 @@ var padimpexp = (function() }, 0); $('#importarrow').stop(true, true).hide(); $('#importstatusball').show(); - - $("#import .importframe").load(function() - { - importDone(); - }); } return ret; } @@ -107,8 +102,6 @@ var padimpexp = (function() function importFailed(msg) { importErrorMessage(msg); - importDone(); - addImportFrames(); } function importDone() @@ -120,6 +113,7 @@ var padimpexp = (function() }, 0); $('#importstatusball').hide(); importClearTimeout(); + addImportFrames(); } function importClearTimeout() @@ -131,11 +125,19 @@ var padimpexp = (function() } } - function importErrorMessage(msg) + function importErrorMessage(status) { + var msg=""; + + if(status === "convertFailed"){ + msg = "We were not able to import this file. Please use a different document format or copy paste manually"; + } else if(status === "uploadFailed"){ + msg = "The upload failed, please try again"; + } + function showError(fade) { - $('#importmessagefail').html('Import failed: ' + (msg || 'Please try a different file.'))[(fade ? "fadeIn" : "show")](); + $('#importmessagefail').html('Import failed: ' + (msg || 'Please copy paste'))[(fade ? "fadeIn" : "show")](); } if ($('#importexport .importmessage').is(':visible')) @@ -175,39 +177,6 @@ var padimpexp = (function() importDone(); } - function importApplicationSuccessful(data, textStatus) - { - if (data.substr(0, 2) == "ok") - { - if ($('#importexport .importmessage').is(':visible')) - { - $('#importexport .importmessage').hide(); - } - $('#importmessagesuccess').html('Import successful!').show(); - $('#importformfilediv').hide(); - window.setTimeout(function() - { - $('#importmessagesuccess').fadeOut("slow", function() - { - $('#importformfilediv').show(); - }); - if (hidePanelCall) - { - hidePanelCall(); - } - }, 3000); - } - else if (data.substr(0, 4) == "fail") - { - importErrorMessage("Couldn't update pad contents. This can happen if your web browser has \"cookies\" disabled."); - } - else if (data.substr(0, 4) == "msg:") - { - importErrorMessage(data.substr(4)); - } - importDone(); - } - ///// export function cantExport() @@ -290,16 +259,14 @@ var padimpexp = (function() $('#importform').submit(fileInputSubmit); $('.disabledexport').click(cantExport); }, - handleFrameCall: function(callName, argsArray) + handleFrameCall: function(status) { - if (callName == 'importFailed') + if (status !== "ok") { - importFailed(argsArray[0]); - } - else if (callName == 'importSuccessful') - { - importSuccessful(argsArray[0]); + importFailed(status); } + + importDone(); }, disable: function() { From 973aad5c96e5731ad6891f0d507b630d76c63c1b Mon Sep 17 00:00:00 2001 From: Peter 'Pita' Martischka Date: Mon, 27 Feb 2012 14:13:47 +0100 Subject: [PATCH 2/3] Fix doesPadExist check --- node/db/PadManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/db/PadManager.js b/node/db/PadManager.js index 12682612..4e3a3199 100644 --- a/node/db/PadManager.js +++ b/node/db/PadManager.js @@ -115,7 +115,7 @@ exports.doesPadExists = function(padId, callback) db.get("pad:"+padId, function(err, value) { if(ERR(err, callback)) return; - callback(null, value != null); + callback(null, value != null && value.atext); }); } From 0c770526982104d392284c4b1e2612794bf09e67 Mon Sep 17 00:00:00 2001 From: Peter 'Pita' Martischka Date: Mon, 27 Feb 2012 14:20:33 +0100 Subject: [PATCH 3/3] Fixed #299 and #338 --- node/handler/ImportHandler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/handler/ImportHandler.js b/node/handler/ImportHandler.js index 3dccb8af..ed5eb05e 100644 --- a/node/handler/ImportHandler.js +++ b/node/handler/ImportHandler.js @@ -82,7 +82,7 @@ exports.doImport = function(req, res, padId) //this allows us to accept source code files like .c or .java function(callback) { - var fileEnding = srcFile.split(".")[1].toLowerCase(); + var fileEnding = (srcFile.split(".")[1] || "").toLowerCase(); var knownFileEndings = ["txt", "doc", "docx", "pdf", "odt", "html", "htm"]; //find out if this is a known file ending