restart abiword on crash and give the user feedback about bad import files

pull/503/merge
Peter 'Pita' Martischka 2012-02-27 00:22:53 +01:00
parent 00c3281a02
commit ed8aff22d4
3 changed files with 75 additions and 95 deletions

View File

@ -115,7 +115,15 @@ exports.doImport = function(req, res, padId)
{ {
var randNum = Math.floor(Math.random()*0xFFFFFFFF); var randNum = Math.floor(Math.random()*0xFFFFFFFF);
destFile = tempDirectory + "eplite_import_" + randNum + ".txt"; 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 //get the pad object
@ -176,16 +184,18 @@ exports.doImport = function(req, res, padId)
} }
], function(err) ], function(err)
{ {
//the upload failed, there is nothing we can do, send a 500 var status = "ok";
if(err == "uploadFailed")
//check for known errors and replace the status
if(err == "uploadFailed" || err == "convertFailed")
{ {
res.send(500); status = err;
return; err = null;
} }
ERR(err); ERR(err);
//close the connection //close the connection
res.send("ok"); res.send("<script>document.domain = document.domain; var impexp = window.top.require('/pad_impexp').padimpexp.handleFrameCall('" + status + "'); </script>", 200);
}); });
} }

View File

@ -53,7 +53,7 @@ if(os.type().indexOf("Windows") > -1)
abiword.on('exit', function (code) abiword.on('exit', function (code)
{ {
if(code != 0) { if(code != 0) {
throw "Abiword died with exit code " + code; return callback("Abiword died with exit code " + code);
} }
if(stdoutBuffer != "") if(stdoutBuffer != "")
@ -75,52 +75,54 @@ if(os.type().indexOf("Windows") > -1)
else else
{ {
//spawn the abiword process //spawn the abiword process
var abiword = spawn(settings.abiword, ["--plugin", "AbiCommand"]); var abiword;
//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 stdoutCallback = null;
var stdoutBuffer = ""; var spawnAbiword = function (){
var firstPrompt = true; abiword = spawn(settings.abiword, ["--plugin", "AbiCommand"]);
var stdoutBuffer = "";
var firstPrompt = true;
function onAbiwordStdout(data) //append error messages to the buffer
{ abiword.stderr.on('data', function (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 stdoutBuffer += data.toString();
var err = stdoutBuffer.search("OK") != -1 ? null : stdoutBuffer; });
//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 //we're searching for the prompt, cause this means everything we need is in the buffer
stdoutBuffer = ""; if(stdoutBuffer.search("AbiWord:>") != -1)
//call the callback with the error message
//skip the first prompt
if(stdoutCallback != null && !firstPrompt)
{ {
stdoutCallback(err); //filter the feedback message
stdoutCallback = null; 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) doConvertTask = function(task, callback)
{ {
@ -130,6 +132,7 @@ else
stdoutCallback = function (err) stdoutCallback = function (err)
{ {
callback(); callback();
console.log("queue continue");
task.callback(err); task.callback(err);
}; };
} }

View File

@ -95,11 +95,6 @@ var padimpexp = (function()
}, 0); }, 0);
$('#importarrow').stop(true, true).hide(); $('#importarrow').stop(true, true).hide();
$('#importstatusball').show(); $('#importstatusball').show();
$("#import .importframe").load(function()
{
importDone();
});
} }
return ret; return ret;
} }
@ -107,8 +102,6 @@ var padimpexp = (function()
function importFailed(msg) function importFailed(msg)
{ {
importErrorMessage(msg); importErrorMessage(msg);
importDone();
addImportFrames();
} }
function importDone() function importDone()
@ -120,6 +113,7 @@ var padimpexp = (function()
}, 0); }, 0);
$('#importstatusball').hide(); $('#importstatusball').hide();
importClearTimeout(); importClearTimeout();
addImportFrames();
} }
function importClearTimeout() 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) function showError(fade)
{ {
$('#importmessagefail').html('<strong style="color: red">Import failed:</strong> ' + (msg || 'Please try a different file.'))[(fade ? "fadeIn" : "show")](); $('#importmessagefail').html('<strong style="color: red">Import failed:</strong> ' + (msg || 'Please copy paste'))[(fade ? "fadeIn" : "show")]();
} }
if ($('#importexport .importmessage').is(':visible')) if ($('#importexport .importmessage').is(':visible'))
@ -175,39 +177,6 @@ var padimpexp = (function()
importDone(); importDone();
} }
function importApplicationSuccessful(data, textStatus)
{
if (data.substr(0, 2) == "ok")
{
if ($('#importexport .importmessage').is(':visible'))
{
$('#importexport .importmessage').hide();
}
$('#importmessagesuccess').html('<strong style="color: green">Import successful!</strong>').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 ///// export
function cantExport() function cantExport()
@ -290,16 +259,14 @@ var padimpexp = (function()
$('#importform').submit(fileInputSubmit); $('#importform').submit(fileInputSubmit);
$('.disabledexport').click(cantExport); $('.disabledexport').click(cantExport);
}, },
handleFrameCall: function(callName, argsArray) handleFrameCall: function(status)
{ {
if (callName == 'importFailed') if (status !== "ok")
{ {
importFailed(argsArray[0]); importFailed(status);
}
else if (callName == 'importSuccessful')
{
importSuccessful(argsArray[0]);
} }
importDone();
}, },
disable: function() disable: function()
{ {