Import html by default and allow basic import/export features without abiword

This commit is contained in:
Iván Eixarch 2012-11-23 22:55:25 +01:00
parent ef3231efd2
commit cc60b82a6e
3 changed files with 40 additions and 40 deletions

View File

@ -23,6 +23,7 @@ var ERR = require("async-stacktrace")
, padMessageHandler = require("./PadMessageHandler") , padMessageHandler = require("./PadMessageHandler")
, async = require("async") , async = require("async")
, fs = require("fs") , fs = require("fs")
, path = require("path")
, settings = require('../utils/Settings') , settings = require('../utils/Settings')
, formidable = require('formidable') , formidable = require('formidable')
, os = require("os") , os = require("os")
@ -42,7 +43,7 @@ exports.doImport = function(req, res, padId)
//set html in the pad //set html in the pad
var srcFile, destFile var srcFile, destFile
, pad; , pad
, text; , text;
async.series([ async.series([
@ -69,16 +70,9 @@ exports.doImport = function(req, res, padId)
//ensure this is a file ending we know, else we change the file ending to .txt //ensure this is a file ending we know, else we change the file ending to .txt
//this allows us to accept source code files like .c or .java //this allows us to accept source code files like .c or .java
function(callback) { function(callback) {
var fileEnding = (srcFile.split(".")[1] || "").toLowerCase(); var fileEnding = path.extname(srcFile).toLowerCase()
var knownFileEndings = ["txt", "doc", "docx", "pdf", "odt", "html", "htm"]; , knownFileEndings = [".txt", ".doc", ".docx", ".pdf", ".odt", ".html", ".htm"]
, fileEndingKnown = (knownFileEndings.indexOf(fileEnding) > -1);
//find out if this is a known file ending
var fileEndingKnown = false;
for(var i in knownFileEndings) {
if(fileEnding == knownFileEndings[i]){
fileEndingKnown = true;
}
}
//if the file ending is known, continue as normal //if the file ending is known, continue as normal
if(fileEndingKnown) { if(fileEndingKnown) {
@ -87,25 +81,31 @@ exports.doImport = function(req, res, padId)
//we need to rename this file with a .txt ending //we need to rename this file with a .txt ending
else { else {
var oldSrcFile = srcFile; var oldSrcFile = srcFile;
srcFile = srcFile.split(".")[0] + ".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 text //convert file to html
function(callback) { function(callback) {
var randNum = Math.floor(Math.random()*0xFFFFFFFF); var randNum = Math.floor(Math.random()*0xFFFFFFFF);
destFile = os.tmpDir() + "/eplite_import_" + randNum + ".htm"; destFile = path.join(os.tmpDir(), "eplite_import_" + randNum + ".htm");
abiword.convertFile(srcFile, destFile, "htm", function(err) {
//catch convert errors if (abiword) {
if(err) { abiword.convertFile(srcFile, destFile, "htm", function(err) {
console.warn("Converting Error:", err); //catch convert errors
return callback("convertFailed"); if(err) {
} else { console.warn("Converting Error:", err);
callback(); return callback("convertFailed");
} } else {
}); callback();
}
});
} else {
// if no abiword only rename
fs.rename(srcFile, destFile, callback);
}
}, },
//get the pad object //get the pad object
@ -125,28 +125,35 @@ exports.doImport = function(req, res, padId)
//node on windows has a delay on releasing of the file lock. //node on windows has a delay on releasing of the file lock.
//We add a 100ms delay to work around this //We add a 100ms delay to work around this
if(os.type().indexOf("Windows") > -1){ if(os.type().indexOf("Windows") > -1){
setTimeout(function(){callback();}, 100); setTimeout(function() {callback();}, 100);
} else { } else {
callback(); callback();
} }
}); });
}, },
//change text of the pad and broadcast the changeset //change text of the pad and broadcast the changeset
function(callback) { function(callback) {
importHtml.setPadHTML(pad, text); var fileEnding = path.extname(srcFile).toLowerCase();
if (abiword || fileEnding == ".htm" || fileEnding == ".html") {
importHtml.setPadHTML(pad, text);
} else {
pad.setText(text);
}
padMessageHandler.updatePadClients(pad, callback); padMessageHandler.updatePadClients(pad, callback);
}, },
//clean up temporary files //clean up temporary files
function(callback) { function(callback) {
//for node < 0.7 compatible
var fileExists = fs.exists || path.exists;
async.parallel([ async.parallel([
function(callback){ function(callback){
fs.unlink(srcFile, callback); fileExists (srcFile, function(exist) { (exist)? fs.unlink(srcFile, callback): callback(); });
}, },
function(callback){ function(callback){
fs.unlink(destFile, callback); fileExists (destFile, function(exist) { (exist)? fs.unlink(destFile, callback): callback(); });
} }
], callback); ], callback);
} }
@ -164,7 +171,6 @@ exports.doImport = function(req, res, padId)
ERR(err); ERR(err);
//close the connection //close the connection
res.send("<script type='text/javascript' src='/static/js/jquery.js'></script><script> if ( (!$.browser.msie) && (!($.browser.mozilla && $.browser.version.indexOf(\"1.8.\") == 0)) ){document.domain = document.domain;}var impexp = window.top.require('/pad_impexp').padimpexp.handleFrameCall('" + status + "');</script>", 200); res.send("<script type='text/javascript' src='/static/js/jquery.js'></script><script> if ( (!$.browser.msie) && (!($.browser.mozilla && $.browser.version.indexOf(\"1.8.\") == 0)) ){document.domain = document.domain;}var impexp = window.top.require('/pad_impexp').padimpexp.handleFrameCall('" + status + "');</script>", 200);
}); });
} }

View File

@ -28,12 +28,6 @@ exports.expressCreateServer = function (hook_name, args, cb) {
//handle import requests //handle import requests
args.app.post('/p/:pad/import', function(req, res, next) { args.app.post('/p/:pad/import', function(req, res, next) {
//if abiword is disabled, skip handling this request
if(settings.abiword == null) {
next();
return;
}
hasPadAccess(req, res, function() { hasPadAccess(req, res, function() {
importHandler.doImport(req, res, req.params.pad); importHandler.doImport(req, res, req.params.pad);
}); });

View File

@ -225,8 +225,8 @@ var padimpexp = (function()
$("#exportworda").remove(); $("#exportworda").remove();
$("#exportpdfa").remove(); $("#exportpdfa").remove();
$("#exportopena").remove(); $("#exportopena").remove();
$(".importformdiv").remove();
$("#import").html("Import is not available. To enable import please install abiword"); $("#importform").attr('action', pad_root_url + "/import");
} }
else if(clientVars.abiwordAvailable == "withoutPDF") else if(clientVars.abiwordAvailable == "withoutPDF")
{ {