Merge pull request #2030 from ether/import_hook

Working import hooks
This commit is contained in:
John McLear 2013-12-09 10:22:40 -08:00
commit 438f3fc717

View file

@ -29,8 +29,8 @@ var ERR = require("async-stacktrace")
, formidable = require('formidable') , formidable = require('formidable')
, os = require("os") , os = require("os")
, importHtml = require("../utils/ImportHtml") , importHtml = require("../utils/ImportHtml")
, log4js = require('log4js'); , log4js = require("log4js")
, hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks.js");
//load abiword only if its enabled //load abiword only if its enabled
if(settings.abiword != null) if(settings.abiword != null)
@ -52,7 +52,10 @@ exports.doImport = function(req, res, padId)
var srcFile, destFile var srcFile, destFile
, pad , pad
, text; , text
, importHandledByPlugin;
var randNum = Math.floor(Math.random()*0xFFFFFFFF);
async.series([ async.series([
//save the uploaded file to /tmp //save the uploaded file to /tmp
@ -91,16 +94,26 @@ exports.doImport = function(req, res, padId)
else { else {
var oldSrcFile = srcFile; var oldSrcFile = srcFile;
srcFile = path.join(path.dirname(srcFile),path.basename(srcFile, fileEnding)+".txt"); srcFile = path.join(path.dirname(srcFile),path.basename(srcFile, fileEnding)+".txt");
fs.rename(oldSrcFile, srcFile, callback); fs.rename(oldSrcFile, srcFile, callback);
} }
}, },
//convert file to html
function(callback){ function(callback){
var randNum = Math.floor(Math.random()*0xFFFFFFFF);
destFile = path.join(tmpDirectory, "eplite_import_" + randNum + ".htm"); destFile = path.join(tmpDirectory, "eplite_import_" + randNum + ".htm");
// Logic for allowing external Import Plugins
hooks.aCallAll("import", {srcFile: srcFile, destFile: destFile}, function(err, result){
if(ERR(err, callback)) return callback();
if(result.length > 0){ // This feels hacky and wrong..
importHandledByPlugin = true;
callback();
}else{
callback();
}
});
},
//convert file to html
function(callback) {
if(!importHandledByPlugin){
if (abiword) { if (abiword) {
abiword.convertFile(srcFile, destFile, "htm", function(err) { abiword.convertFile(srcFile, destFile, "htm", function(err) {
//catch convert errors //catch convert errors
@ -115,6 +128,9 @@ exports.doImport = function(req, res, padId)
// if no abiword only rename // if no abiword only rename
fs.rename(srcFile, destFile, callback); fs.rename(srcFile, destFile, callback);
} }
}else{
callback();
}
}, },
function(callback) { function(callback) {