2011-07-21 21:13:58 +02:00
|
|
|
/**
|
|
|
|
* Handles the import requests
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 16:26:41 +02:00
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2012-11-27 00:11:45 +01:00
|
|
|
* 2012 Iván Eixarch
|
2011-07-21 21:13:58 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-11-23 19:06:18 +01:00
|
|
|
var ERR = require("async-stacktrace")
|
|
|
|
, padManager = require("../db/PadManager")
|
|
|
|
, padMessageHandler = require("./PadMessageHandler")
|
|
|
|
, async = require("async")
|
|
|
|
, fs = require("fs")
|
2012-11-23 22:55:25 +01:00
|
|
|
, path = require("path")
|
2012-11-23 19:06:18 +01:00
|
|
|
, settings = require('../utils/Settings')
|
|
|
|
, formidable = require('formidable')
|
|
|
|
, os = require("os")
|
2013-09-27 15:47:34 +02:00
|
|
|
, importHtml = require("../utils/ImportHtml")
|
|
|
|
, log4js = require('log4js');
|
|
|
|
|
2012-11-23 18:18:03 +01:00
|
|
|
|
2011-07-21 21:13:58 +02:00
|
|
|
//load abiword only if its enabled
|
|
|
|
if(settings.abiword != null)
|
2011-07-27 19:52:23 +02:00
|
|
|
var abiword = require("../utils/Abiword");
|
2012-12-06 11:49:04 +01:00
|
|
|
|
|
|
|
//for node 0.6 compatibily, os.tmpDir() only works from 0.8
|
|
|
|
var tmpDirectory = process.env.TEMP || process.env.TMPDIR || process.env.TMP || '/tmp';
|
2011-07-21 21:13:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* do a requested import
|
|
|
|
*/
|
|
|
|
exports.doImport = function(req, res, padId)
|
|
|
|
{
|
2013-09-27 15:47:34 +02:00
|
|
|
var apiLogger = log4js.getLogger("ImportHandler");
|
|
|
|
|
2011-07-21 21:13:58 +02:00
|
|
|
//pipe to a file
|
2012-11-23 19:06:18 +01:00
|
|
|
//convert file to html via abiword
|
|
|
|
//set html in the pad
|
2011-07-21 21:13:58 +02:00
|
|
|
|
2012-11-23 19:06:18 +01:00
|
|
|
var srcFile, destFile
|
2012-11-23 22:55:25 +01:00
|
|
|
, pad
|
2012-11-23 19:06:18 +01:00
|
|
|
, text;
|
2011-07-21 21:13:58 +02:00
|
|
|
|
|
|
|
async.series([
|
|
|
|
//save the uploaded file to /tmp
|
2012-11-23 19:06:18 +01:00
|
|
|
function(callback) {
|
2011-07-21 21:13:58 +02:00
|
|
|
var form = new formidable.IncomingForm();
|
|
|
|
form.keepExtensions = true;
|
2012-12-06 11:49:04 +01:00
|
|
|
form.uploadDir = tmpDirectory;
|
2011-07-21 21:13:58 +02:00
|
|
|
|
2012-11-23 19:06:18 +01:00
|
|
|
form.parse(req, function(err, fields, files) {
|
2011-10-19 21:46:28 +02:00
|
|
|
//the upload failed, stop at this point
|
2012-11-23 19:06:18 +01:00
|
|
|
if(err || files.file === undefined) {
|
2013-03-19 18:40:39 +01:00
|
|
|
if(err) console.warn("Uploading Error: " + err.stack);
|
2011-10-19 21:46:28 +02:00
|
|
|
callback("uploadFailed");
|
|
|
|
}
|
|
|
|
//everything ok, continue
|
2012-11-23 19:06:18 +01:00
|
|
|
else {
|
2011-10-19 21:46:28 +02:00
|
|
|
//save the path of the uploaded file
|
|
|
|
srcFile = files.file.path;
|
|
|
|
callback();
|
|
|
|
}
|
2011-07-21 21:13:58 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-07-24 22:07:42 +02:00
|
|
|
//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
|
2012-11-23 19:06:18 +01:00
|
|
|
function(callback) {
|
2012-11-23 22:55:25 +01:00
|
|
|
var fileEnding = path.extname(srcFile).toLowerCase()
|
|
|
|
, knownFileEndings = [".txt", ".doc", ".docx", ".pdf", ".odt", ".html", ".htm"]
|
|
|
|
, fileEndingKnown = (knownFileEndings.indexOf(fileEnding) > -1);
|
2011-07-24 22:07:42 +02:00
|
|
|
|
|
|
|
//if the file ending is known, continue as normal
|
2012-11-23 19:06:18 +01:00
|
|
|
if(fileEndingKnown) {
|
2011-07-24 22:07:42 +02:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
//we need to rename this file with a .txt ending
|
2012-11-23 19:06:18 +01:00
|
|
|
else {
|
2011-07-24 22:07:42 +02:00
|
|
|
var oldSrcFile = srcFile;
|
2012-11-23 22:55:25 +01:00
|
|
|
srcFile = path.join(path.dirname(srcFile),path.basename(srcFile, fileEnding)+".txt");
|
2011-07-24 22:07:42 +02:00
|
|
|
|
|
|
|
fs.rename(oldSrcFile, srcFile, callback);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-11-23 22:55:25 +01:00
|
|
|
//convert file to html
|
2012-11-23 19:06:18 +01:00
|
|
|
function(callback) {
|
2011-11-10 11:37:12 +01:00
|
|
|
var randNum = Math.floor(Math.random()*0xFFFFFFFF);
|
2012-12-06 11:49:04 +01:00
|
|
|
destFile = path.join(tmpDirectory, "eplite_import_" + randNum + ".htm");
|
2012-11-23 22:55:25 +01:00
|
|
|
|
|
|
|
if (abiword) {
|
|
|
|
abiword.convertFile(srcFile, destFile, "htm", function(err) {
|
|
|
|
//catch convert errors
|
|
|
|
if(err) {
|
|
|
|
console.warn("Converting Error:", err);
|
|
|
|
return callback("convertFailed");
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// if no abiword only rename
|
|
|
|
fs.rename(srcFile, destFile, callback);
|
|
|
|
}
|
2011-07-21 21:13:58 +02:00
|
|
|
},
|
|
|
|
|
2013-09-10 21:58:28 +02:00
|
|
|
function(callback) {
|
|
|
|
if (!abiword) {
|
|
|
|
// Read the file with no encoding for raw buffer access.
|
|
|
|
fs.readFile(destFile, function(err, buf) {
|
|
|
|
if (err) throw err;
|
|
|
|
var isAscii = true;
|
|
|
|
// Check if there are only ascii chars in the uploaded file
|
|
|
|
for (var i=0, len=buf.length; i<len; i++) {
|
|
|
|
if (buf[i] > 240) {
|
|
|
|
isAscii=false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isAscii) {
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
callback("uploadFailed");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-07-21 21:13:58 +02:00
|
|
|
//get the pad object
|
2012-11-23 19:06:18 +01:00
|
|
|
function(callback) {
|
|
|
|
padManager.getPad(padId, function(err, _pad){
|
2011-12-04 16:50:02 +01:00
|
|
|
if(ERR(err, callback)) return;
|
2011-07-21 21:13:58 +02:00
|
|
|
pad = _pad;
|
2011-12-04 16:50:02 +01:00
|
|
|
callback();
|
2011-07-21 21:13:58 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
//read the text
|
2012-11-23 19:06:18 +01:00
|
|
|
function(callback) {
|
|
|
|
fs.readFile(destFile, "utf8", function(err, _text){
|
2011-12-04 16:50:02 +01:00
|
|
|
if(ERR(err, callback)) return;
|
2011-07-21 21:13:58 +02:00
|
|
|
text = _text;
|
2013-09-22 00:29:32 +02:00
|
|
|
// Title needs to be stripped out else it appends it to the pad..
|
|
|
|
text = text.replace("<title>", "<!-- <title>");
|
|
|
|
text = text.replace("</title>-->");
|
|
|
|
|
2011-08-04 00:57:11 +02:00
|
|
|
//node on windows has a delay on releasing of the file lock.
|
|
|
|
//We add a 100ms delay to work around this
|
2012-11-23 22:55:25 +01:00
|
|
|
if(os.type().indexOf("Windows") > -1){
|
|
|
|
setTimeout(function() {callback();}, 100);
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
2011-07-21 21:13:58 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
//change text of the pad and broadcast the changeset
|
2012-11-23 19:06:18 +01:00
|
|
|
function(callback) {
|
2012-11-23 22:55:25 +01:00
|
|
|
var fileEnding = path.extname(srcFile).toLowerCase();
|
|
|
|
if (abiword || fileEnding == ".htm" || fileEnding == ".html") {
|
2013-09-27 15:47:34 +02:00
|
|
|
try{
|
|
|
|
importHtml.setPadHTML(pad, text);
|
|
|
|
}catch(e){
|
|
|
|
apiLogger.warn("Error importing, possibly caused by malformed HTML");
|
|
|
|
}
|
2012-11-23 22:55:25 +01:00
|
|
|
} else {
|
|
|
|
pad.setText(text);
|
|
|
|
}
|
2011-07-21 21:13:58 +02:00
|
|
|
padMessageHandler.updatePadClients(pad, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
//clean up temporary files
|
2012-11-23 19:06:18 +01:00
|
|
|
function(callback) {
|
2012-11-23 22:55:25 +01:00
|
|
|
//for node < 0.7 compatible
|
|
|
|
var fileExists = fs.exists || path.exists;
|
2011-07-21 21:13:58 +02:00
|
|
|
async.parallel([
|
2012-11-23 19:06:18 +01:00
|
|
|
function(callback){
|
2012-11-23 22:55:25 +01:00
|
|
|
fileExists (srcFile, function(exist) { (exist)? fs.unlink(srcFile, callback): callback(); });
|
2011-07-21 21:13:58 +02:00
|
|
|
},
|
2012-11-23 19:06:18 +01:00
|
|
|
function(callback){
|
2012-11-23 22:55:25 +01:00
|
|
|
fileExists (destFile, function(exist) { (exist)? fs.unlink(destFile, callback): callback(); });
|
2011-07-21 21:13:58 +02:00
|
|
|
}
|
|
|
|
], callback);
|
|
|
|
}
|
2012-11-23 19:06:18 +01:00
|
|
|
], function(err) {
|
|
|
|
|
2012-02-27 00:22:53 +01:00
|
|
|
var status = "ok";
|
|
|
|
|
|
|
|
//check for known errors and replace the status
|
|
|
|
if(err == "uploadFailed" || err == "convertFailed")
|
2011-10-19 21:46:28 +02:00
|
|
|
{
|
2012-02-27 00:22:53 +01:00
|
|
|
status = err;
|
|
|
|
err = null;
|
2011-10-19 21:46:28 +02:00
|
|
|
}
|
|
|
|
|
2011-12-04 16:50:02 +01:00
|
|
|
ERR(err);
|
2011-07-24 21:53:28 +02:00
|
|
|
|
2011-07-21 21:13:58 +02:00
|
|
|
//close the connection
|
2013-04-02 16:42:50 +02:00
|
|
|
res.send("<head><script type='text/javascript' src='../../static/js/jquery.js'></script><script type='text/javascript' src='../../static/js/jquery_browser.js'></script></head><script>$(window).load(function(){if ( (!$.browser.msie) && (!($.browser.mozilla && $.browser.version.indexOf(\"1.8.\") == 0)) ){document.domain = document.domain;}var impexp = window.parent.padimpexp.handleFrameCall('" + status + "');})</script>", 200);
|
2011-07-21 21:13:58 +02:00
|
|
|
});
|
|
|
|
}
|
2012-12-10 12:34:16 +01:00
|
|
|
|