mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-21 16:06:16 -04:00
Merge pull request #2418 from ether/etherpad-export-and-import
Full Pad portability (Export/Import)
This commit is contained in:
commit
036b7d2890
9 changed files with 261 additions and 64 deletions
|
@ -4,6 +4,7 @@
|
|||
|
||||
/*
|
||||
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
||||
* 2014 John McLear (Etherpad Foundation / McLear Ltd)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -21,6 +22,7 @@
|
|||
var ERR = require("async-stacktrace");
|
||||
var exporthtml = require("../utils/ExportHtml");
|
||||
var exporttxt = require("../utils/ExportTxt");
|
||||
var exportEtherpad = require("../utils/ExportEtherpad");
|
||||
var async = require("async");
|
||||
var fs = require("fs");
|
||||
var settings = require('../utils/Settings');
|
||||
|
@ -52,14 +54,20 @@ exports.doExport = function(req, res, padId, type)
|
|||
// if fileName is set then set it to the padId, note that fileName is returned as an array.
|
||||
if(hookFileName.length) fileName = hookFileName;
|
||||
|
||||
|
||||
//tell the browser that this is a downloadable file
|
||||
res.attachment(fileName + "." + type);
|
||||
|
||||
//if this is a plain text export, we can do this directly
|
||||
// We have to over engineer this because tabs are stored as attributes and not plain text
|
||||
|
||||
if(type == "txt")
|
||||
if(type == "etherpad"){
|
||||
exportEtherpad.getPadRaw(padId, function(err, pad){
|
||||
if(!err){
|
||||
res.send(pad);
|
||||
// return;
|
||||
}
|
||||
});
|
||||
}
|
||||
else if(type == "txt")
|
||||
{
|
||||
var txt;
|
||||
var randNum;
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
/*
|
||||
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
||||
* 2012 Iván Eixarch
|
||||
* 2014 John McLear (Etherpad Foundation / McLear Ltd)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -29,6 +30,7 @@ var ERR = require("async-stacktrace")
|
|||
, formidable = require('formidable')
|
||||
, os = require("os")
|
||||
, importHtml = require("../utils/ImportHtml")
|
||||
, importEtherpad = require("../utils/ImportEtherpad")
|
||||
, log4js = require("log4js")
|
||||
, hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks.js");
|
||||
|
||||
|
@ -53,7 +55,8 @@ exports.doImport = function(req, res, padId)
|
|||
var srcFile, destFile
|
||||
, pad
|
||||
, text
|
||||
, importHandledByPlugin;
|
||||
, importHandledByPlugin
|
||||
, directDatabaseAccess;
|
||||
|
||||
var randNum = Math.floor(Math.random()*0xFFFFFFFF);
|
||||
|
||||
|
@ -83,7 +86,7 @@ exports.doImport = function(req, res, padId)
|
|||
//this allows us to accept source code files like .c or .java
|
||||
function(callback) {
|
||||
var fileEnding = path.extname(srcFile).toLowerCase()
|
||||
, knownFileEndings = [".txt", ".doc", ".docx", ".pdf", ".odt", ".html", ".htm"]
|
||||
, knownFileEndings = [".txt", ".doc", ".docx", ".pdf", ".odt", ".html", ".htm", ".etherpad"]
|
||||
, fileEndingKnown = (knownFileEndings.indexOf(fileEnding) > -1);
|
||||
|
||||
//if the file ending is known, continue as normal
|
||||
|
@ -116,9 +119,33 @@ exports.doImport = function(req, res, padId)
|
|||
}
|
||||
});
|
||||
},
|
||||
function(callback) {
|
||||
var fileEnding = path.extname(srcFile).toLowerCase()
|
||||
var fileIsEtherpad = (fileEnding === ".etherpad");
|
||||
|
||||
if(fileIsEtherpad){
|
||||
// we do this here so we can see if the pad has quit ea few edits
|
||||
padManager.getPad(padId, function(err, _pad){
|
||||
var headCount = _pad.head;
|
||||
if(headCount >= 10){
|
||||
apiLogger.warn("Direct database Import attempt of a pad that already has content, we wont be doing this")
|
||||
return callback("padHasData");
|
||||
}else{
|
||||
fs.readFile(srcFile, "utf8", function(err, _text){
|
||||
directDatabaseAccess = true;
|
||||
importEtherpad.setPadRaw(padId, _text, function(err){
|
||||
callback();
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}else{
|
||||
callback();
|
||||
}
|
||||
},
|
||||
//convert file to html
|
||||
function(callback) {
|
||||
if(!importHandledByPlugin){
|
||||
if(!importHandledByPlugin || !directDatabaseAccess){
|
||||
var fileEnding = path.extname(srcFile).toLowerCase();
|
||||
var fileIsHTML = (fileEnding === ".html" || fileEnding === ".htm");
|
||||
if (abiword && !fileIsHTML) {
|
||||
|
@ -141,24 +168,28 @@ exports.doImport = function(req, res, padId)
|
|||
},
|
||||
|
||||
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 (!abiword){
|
||||
if(!directDatabaseAccess) {
|
||||
// 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");
|
||||
}
|
||||
});
|
||||
if (isAscii) {
|
||||
callback();
|
||||
} else {
|
||||
callback("uploadFailed");
|
||||
}
|
||||
});
|
||||
}else{
|
||||
callback();
|
||||
}
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
|
@ -175,64 +206,90 @@ exports.doImport = function(req, res, padId)
|
|||
|
||||
//read the text
|
||||
function(callback) {
|
||||
fs.readFile(destFile, "utf8", function(err, _text){
|
||||
if(ERR(err, callback)) return;
|
||||
text = _text;
|
||||
// Title needs to be stripped out else it appends it to the pad..
|
||||
text = text.replace("<title>", "<!-- <title>");
|
||||
text = text.replace("</title>","</title>-->");
|
||||
|
||||
//node on windows has a delay on releasing of the file lock.
|
||||
//We add a 100ms delay to work around this
|
||||
if(os.type().indexOf("Windows") > -1){
|
||||
setTimeout(function() {callback();}, 100);
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
if(!directDatabaseAccess){
|
||||
fs.readFile(destFile, "utf8", function(err, _text){
|
||||
if(ERR(err, callback)) return;
|
||||
text = _text;
|
||||
// Title needs to be stripped out else it appends it to the pad..
|
||||
text = text.replace("<title>", "<!-- <title>");
|
||||
text = text.replace("</title>","</title>-->");
|
||||
|
||||
//node on windows has a delay on releasing of the file lock.
|
||||
//We add a 100ms delay to work around this
|
||||
if(os.type().indexOf("Windows") > -1){
|
||||
setTimeout(function() {callback();}, 100);
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}else{
|
||||
callback();
|
||||
}
|
||||
},
|
||||
|
||||
//change text of the pad and broadcast the changeset
|
||||
function(callback) {
|
||||
var fileEnding = path.extname(srcFile).toLowerCase();
|
||||
if (abiword || fileEnding == ".htm" || fileEnding == ".html") {
|
||||
try{
|
||||
importHtml.setPadHTML(pad, text);
|
||||
}catch(e){
|
||||
apiLogger.warn("Error importing, possibly caused by malformed HTML");
|
||||
if(!directDatabaseAccess){
|
||||
var fileEnding = path.extname(srcFile).toLowerCase();
|
||||
if (abiword || fileEnding == ".htm" || fileEnding == ".html") {
|
||||
try{
|
||||
importHtml.setPadHTML(pad, text);
|
||||
}catch(e){
|
||||
apiLogger.warn("Error importing, possibly caused by malformed HTML");
|
||||
}
|
||||
} else {
|
||||
pad.setText(text);
|
||||
}
|
||||
} else {
|
||||
pad.setText(text);
|
||||
}
|
||||
padMessageHandler.updatePadClients(pad, callback);
|
||||
|
||||
// Load the Pad into memory then brodcast updates to all clients
|
||||
padManager.unloadPad(padId);
|
||||
padManager.getPad(padId, function(err, _pad){
|
||||
var pad = _pad;
|
||||
padManager.unloadPad(padId);
|
||||
|
||||
// direct Database Access means a pad user should perform a switchToPad
|
||||
// and not attempt to recieve updated pad data..
|
||||
if(!directDatabaseAccess){
|
||||
padMessageHandler.updatePadClients(pad, function(){
|
||||
callback();
|
||||
});
|
||||
}else{
|
||||
callback();
|
||||
}
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
//clean up temporary files
|
||||
function(callback) {
|
||||
//for node < 0.7 compatible
|
||||
var fileExists = fs.exists || path.exists;
|
||||
async.parallel([
|
||||
function(callback){
|
||||
fileExists (srcFile, function(exist) { (exist)? fs.unlink(srcFile, callback): callback(); });
|
||||
},
|
||||
function(callback){
|
||||
fileExists (destFile, function(exist) { (exist)? fs.unlink(destFile, callback): callback(); });
|
||||
}
|
||||
], callback);
|
||||
if(!directDatabaseAccess){
|
||||
//for node < 0.7 compatible
|
||||
var fileExists = fs.exists || path.exists;
|
||||
async.parallel([
|
||||
function(callback){
|
||||
fileExists (srcFile, function(exist) { (exist)? fs.unlink(srcFile, callback): callback(); });
|
||||
},
|
||||
function(callback){
|
||||
fileExists (destFile, function(exist) { (exist)? fs.unlink(destFile, callback): callback(); });
|
||||
}
|
||||
], callback);
|
||||
}else{
|
||||
callback();
|
||||
}
|
||||
}
|
||||
], function(err) {
|
||||
|
||||
var status = "ok";
|
||||
|
||||
//check for known errors and replace the status
|
||||
if(err == "uploadFailed" || err == "convertFailed")
|
||||
if(err == "uploadFailed" || err == "convertFailed" || err == "padHasData")
|
||||
{
|
||||
status = err;
|
||||
err = null;
|
||||
}
|
||||
|
||||
ERR(err);
|
||||
|
||||
|
||||
//close the connection
|
||||
res.send(
|
||||
"<head> \
|
||||
|
@ -243,7 +300,7 @@ exports.doImport = function(req, res, padId)
|
|||
if(navigator.userAgent.indexOf('MSIE') === -1){ \
|
||||
document.domain = document.domain; \
|
||||
} \
|
||||
var impexp = window.parent.padimpexp.handleFrameCall('" + status + "'); \
|
||||
var impexp = window.parent.padimpexp.handleFrameCall('" + directDatabaseAccess +"', '" + status + "'); \
|
||||
}) \
|
||||
</script>"
|
||||
, 200);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue