pad.libre-service.eu-etherpad/src/node/handler/ImportHandler.js

171 lines
4.9 KiB
JavaScript
Raw Normal View History

2011-07-21 21:13:58 +02:00
/**
* Handles the import requests
*/
/*
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
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")
, settings = require('../utils/Settings')
, formidable = require('formidable')
, os = require("os")
, importHtml = require("../utils/ImportHtml");
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");
2011-07-21 21:13:58 +02:00
/**
* do a requested import
*/
exports.doImport = function(req, res, padId)
{
//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
, pad;
, 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-11-23 19:06:18 +01:00
form.parse(req, function(err, fields, files) {
//the upload failed, stop at this point
2012-11-23 19:06:18 +01:00
if(err || files.file === undefined) {
console.warn("Uploading Error: " + err.stack);
callback("uploadFailed");
}
//everything ok, continue
2012-11-23 19:06:18 +01:00
else {
//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-02-27 14:20:33 +01:00
var fileEnding = (srcFile.split(".")[1] || "").toLowerCase();
2011-07-24 22:07:42 +02:00
var knownFileEndings = ["txt", "doc", "docx", "pdf", "odt", "html", "htm"];
//find out if this is a known file ending
var fileEndingKnown = false;
2012-11-23 19:06:18 +01:00
for(var i in knownFileEndings) {
if(fileEnding == knownFileEndings[i]){
2011-07-24 22:07:42 +02:00
fileEndingKnown = true;
}
}
//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;
srcFile = srcFile.split(".")[0] + ".txt";
fs.rename(oldSrcFile, srcFile, callback);
}
},
2011-07-21 21:13:58 +02:00
//convert file to text
2012-11-23 19:06:18 +01:00
function(callback) {
var randNum = Math.floor(Math.random()*0xFFFFFFFF);
destFile = os.tmpDir() + "/eplite_import_" + randNum + ".htm";
2012-11-23 19:06:18 +01:00
abiword.convertFile(srcFile, destFile, "htm", function(err) {
//catch convert errors
2012-11-23 19:06:18 +01:00
if(err) {
console.warn("Converting Error:", err);
return callback("convertFailed");
} 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){
if(ERR(err, callback)) return;
2011-07-21 21:13:58 +02:00
pad = _pad;
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){
if(ERR(err, callback)) return;
2011-07-21 21:13:58 +02:00
text = _text;
//node on windows has a delay on releasing of the file lock.
//We add a 100ms delay to work around this
2012-11-23 19:06:18 +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) {
importHtml.setPadHTML(pad, 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) {
2011-07-21 21:13:58 +02:00
async.parallel([
2012-11-23 19:06:18 +01:00
function(callback){
2011-07-21 21:13:58 +02:00
fs.unlink(srcFile, callback);
},
2012-11-23 19:06:18 +01:00
function(callback){
2011-07-21 21:13:58 +02:00
fs.unlink(destFile, callback);
}
], callback);
}
2012-11-23 19:06:18 +01:00
], function(err) {
var status = "ok";
//check for known errors and replace the status
if(err == "uploadFailed" || err == "convertFailed")
{
status = err;
err = null;
}
ERR(err);
2011-07-24 21:53:28 +02:00
2011-07-21 21:13:58 +02:00
//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);
2011-07-21 21:13:58 +02:00
});
}