mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-24 01:16:15 -04:00
import/export: conversion to Promises/async
NB1: needs additional review and testing - no abiword available on my test bed NB2: in ImportHandler.js, directly delete the file, and handle the eventual error later: checking before for existence is prone to race conditions, and does not handle any errors anyway.
This commit is contained in:
parent
5192a0c498
commit
62345ac8f7
8 changed files with 379 additions and 570 deletions
|
@ -15,59 +15,48 @@
|
|||
*/
|
||||
|
||||
|
||||
var async = require("async");
|
||||
var db = require("../db/DB").db;
|
||||
var ERR = require("async-stacktrace");
|
||||
const thenify = require("thenify").withCallback;
|
||||
let db = require("../db/DB");
|
||||
|
||||
exports.getPadRaw = thenify(function(padId, callback){
|
||||
async.waterfall([
|
||||
function(cb){
|
||||
db.get("pad:"+padId, cb);
|
||||
},
|
||||
function(padcontent,cb){
|
||||
var records = ["pad:"+padId];
|
||||
for (var i = 0; i <= padcontent.head; i++) {
|
||||
records.push("pad:"+padId+":revs:" + i);
|
||||
}
|
||||
exports.getPadRaw = async function(padId) {
|
||||
|
||||
for (var i = 0; i <= padcontent.chatHead; i++) {
|
||||
records.push("pad:"+padId+":chat:" + i);
|
||||
}
|
||||
let padKey = "pad:" + padId;
|
||||
let padcontent = await db.get(padKey);
|
||||
|
||||
var data = {};
|
||||
|
||||
async.forEachSeries(Object.keys(records), function(key, r){
|
||||
|
||||
// For each piece of info about a pad.
|
||||
db.get(records[key], function(err, entry){
|
||||
data[records[key]] = entry;
|
||||
|
||||
// Get the Pad Authors
|
||||
if(entry.pool && entry.pool.numToAttrib){
|
||||
var authors = entry.pool.numToAttrib;
|
||||
async.forEachSeries(Object.keys(authors), function(k, c){
|
||||
if(authors[k][0] === "author"){
|
||||
var authorId = authors[k][1];
|
||||
|
||||
// Get the author info
|
||||
db.get("globalAuthor:"+authorId, function(e, authorEntry){
|
||||
if(authorEntry && authorEntry.padIDs) authorEntry.padIDs = padId;
|
||||
if(!e) data["globalAuthor:"+authorId] = authorEntry;
|
||||
});
|
||||
|
||||
}
|
||||
// console.log("authorsK", authors[k]);
|
||||
c(null);
|
||||
});
|
||||
}
|
||||
r(null); // callback;
|
||||
});
|
||||
}, function(err){
|
||||
cb(err, data);
|
||||
})
|
||||
let records = [ padKey ];
|
||||
for (let i = 0; i <= padcontent.head; i++) {
|
||||
records.push(padKey + ":revs:" + i);
|
||||
}
|
||||
], function(err, data){
|
||||
callback(null, data);
|
||||
});
|
||||
});
|
||||
|
||||
for (let i = 0; i <= padcontent.chatHead; i++) {
|
||||
records.push(padKey + ":chat:" + i);
|
||||
}
|
||||
|
||||
let data = {};
|
||||
for (let key of records) {
|
||||
|
||||
// For each piece of info about a pad.
|
||||
let entry = data[key] = await db.get(key);
|
||||
|
||||
// Get the Pad Authors
|
||||
if (entry.pool && entry.pool.numToAttrib) {
|
||||
let authors = entry.pool.numToAttrib;
|
||||
|
||||
for (let k of Object.keys(authors)) {
|
||||
if (authors[k][0] === "author") {
|
||||
let authorId = authors[k][1];
|
||||
|
||||
// Get the author info
|
||||
let authorEntry = await db.get("globalAuthor:" + authorId);
|
||||
if (authorEntry) {
|
||||
data["globalAuthor:" + authorId] = authorEntry;
|
||||
if (authorEntry.padIDs) {
|
||||
authorEntry.padIDs = padId;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
|
|
@ -14,61 +14,29 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
var async = require("async");
|
||||
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
||||
var padManager = require("../db/PadManager");
|
||||
var ERR = require("async-stacktrace");
|
||||
var _ = require('underscore');
|
||||
var Security = require('ep_etherpad-lite/static/js/security');
|
||||
var hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks');
|
||||
var eejs = require('ep_etherpad-lite/node/eejs');
|
||||
var _analyzeLine = require('./ExportHelper')._analyzeLine;
|
||||
var _encodeWhitespace = require('./ExportHelper')._encodeWhitespace;
|
||||
const thenify = require("thenify").withCallback;
|
||||
|
||||
function getPadHTML(pad, revNum, callback)
|
||||
async function getPadHTML(pad, revNum)
|
||||
{
|
||||
var atext = pad.atext;
|
||||
var html;
|
||||
async.waterfall([
|
||||
let atext = pad.atext;
|
||||
|
||||
// fetch revision atext
|
||||
function (callback)
|
||||
{
|
||||
if (revNum != undefined)
|
||||
{
|
||||
pad.getInternalRevisionAText(revNum, function (err, revisionAtext)
|
||||
{
|
||||
if(ERR(err, callback)) return;
|
||||
atext = revisionAtext;
|
||||
callback();
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
callback(null);
|
||||
}
|
||||
},
|
||||
if (revNum != undefined) {
|
||||
atext = await pad.getInternalRevisionAText(revNum);
|
||||
}
|
||||
|
||||
// convert atext to html
|
||||
|
||||
|
||||
function (callback)
|
||||
{
|
||||
html = getHTMLFromAtext(pad, atext);
|
||||
callback(null);
|
||||
}],
|
||||
// run final callback
|
||||
|
||||
|
||||
function (err)
|
||||
{
|
||||
if(ERR(err, callback)) return;
|
||||
callback(null, html);
|
||||
});
|
||||
return getHTMLFromAtext(pad, atext);
|
||||
}
|
||||
|
||||
exports.getPadHTML = thenify(getPadHTML);
|
||||
exports.getPadHTML = getPadHTML;
|
||||
exports.getHTMLFromAtext = getHTMLFromAtext;
|
||||
|
||||
function getHTMLFromAtext(pad, atext, authorColors)
|
||||
|
@ -82,15 +50,16 @@ function getHTMLFromAtext(pad, atext, authorColors)
|
|||
|
||||
// prepare tags stored as ['tag', true] to be exported
|
||||
hooks.aCallAll("exportHtmlAdditionalTags", pad, function(err, newProps){
|
||||
newProps.forEach(function (propName, i){
|
||||
newProps.forEach(function (propName, i) {
|
||||
tags.push(propName);
|
||||
props.push(propName);
|
||||
});
|
||||
});
|
||||
|
||||
// prepare tags stored as ['tag', 'value'] to be exported. This will generate HTML
|
||||
// with tags like <span data-tag="value">
|
||||
hooks.aCallAll("exportHtmlAdditionalTagsWithData", pad, function(err, newProps){
|
||||
newProps.forEach(function (propName, i){
|
||||
newProps.forEach(function (propName, i) {
|
||||
tags.push('span data-' + propName[0] + '="' + propName[1] + '"');
|
||||
props.push(propName);
|
||||
});
|
||||
|
@ -454,38 +423,31 @@ function getHTMLFromAtext(pad, atext, authorColors)
|
|||
|
||||
hooks.aCallAll("getLineHTMLForExport", context);
|
||||
pieces.push(context.lineContent, "<br>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return pieces.join('');
|
||||
}
|
||||
|
||||
exports.getPadHTMLDocument = thenify(function (padId, revNum, callback)
|
||||
exports.getPadHTMLDocument = async function (padId, revNum)
|
||||
{
|
||||
padManager.getPad(padId, function (err, pad)
|
||||
{
|
||||
if(ERR(err, callback)) return;
|
||||
let pad = await padManager.getPad(padId);
|
||||
|
||||
var stylesForExportCSS = "";
|
||||
// Include some Styles into the Head for Export
|
||||
hooks.aCallAll("stylesForExport", padId, function(err, stylesForExport){
|
||||
stylesForExport.forEach(function(css){
|
||||
stylesForExportCSS += css;
|
||||
});
|
||||
|
||||
getPadHTML(pad, revNum, function (err, html)
|
||||
{
|
||||
if(ERR(err, callback)) return;
|
||||
var exportedDoc = eejs.require("ep_etherpad-lite/templates/export_html.html", {
|
||||
body: html,
|
||||
padId: Security.escapeHTML(padId),
|
||||
extraCSS: stylesForExportCSS
|
||||
});
|
||||
callback(null, exportedDoc);
|
||||
});
|
||||
});
|
||||
// Include some Styles into the Head for Export
|
||||
let stylesForExportCSS = "";
|
||||
let stylesForExport = await hooks.aCallAll("stylesForExport", padId);
|
||||
stylesForExport.forEach(function(css){
|
||||
stylesForExportCSS += css;
|
||||
});
|
||||
});
|
||||
|
||||
let html = await getPadHTML(pad, revNum);
|
||||
|
||||
return eejs.require("ep_etherpad-lite/templates/export_html.html", {
|
||||
body: html,
|
||||
padId: Security.escapeHTML(padId),
|
||||
extraCSS: stylesForExportCSS
|
||||
});
|
||||
}
|
||||
|
||||
// copied from ACE
|
||||
var _REGEX_WORDCHAR = /[\u0030-\u0039\u0041-\u005A\u0061-\u007A\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF\u0100-\u1FFF\u3040-\u9FFF\uF900-\uFDFF\uFE70-\uFEFE\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFDC]/;
|
||||
|
|
|
@ -18,46 +18,22 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
var async = require("async");
|
||||
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
||||
var padManager = require("../db/PadManager");
|
||||
var ERR = require("async-stacktrace");
|
||||
var _analyzeLine = require('./ExportHelper')._analyzeLine;
|
||||
|
||||
// This is slightly different than the HTML method as it passes the output to getTXTFromAText
|
||||
function getPadTXT(pad, revNum, callback)
|
||||
var getPadTXT = async function(pad, revNum)
|
||||
{
|
||||
var atext = pad.atext;
|
||||
var html;
|
||||
async.waterfall([
|
||||
let atext = pad.atext;
|
||||
|
||||
// fetch revision atext
|
||||
function(callback) {
|
||||
if (revNum != undefined) {
|
||||
pad.getInternalRevisionAText(revNum, function(err, revisionAtext) {
|
||||
if (ERR(err, callback)) return;
|
||||
|
||||
atext = revisionAtext;
|
||||
callback();
|
||||
});
|
||||
} else {
|
||||
callback(null);
|
||||
}
|
||||
},
|
||||
if (revNum != undefined) {
|
||||
// fetch revision atext
|
||||
atext = await pad.getInternalRevisionAText(revNum);
|
||||
}
|
||||
|
||||
// convert atext to html
|
||||
function(callback) {
|
||||
// only this line is different to the HTML function
|
||||
html = getTXTFromAtext(pad, atext);
|
||||
callback(null);
|
||||
}],
|
||||
|
||||
// run final callback
|
||||
function(err) {
|
||||
if (ERR(err, callback)) return;
|
||||
|
||||
callback(null, html);
|
||||
});
|
||||
return getTXTFromAtext(pad, atext);
|
||||
}
|
||||
|
||||
// This is different than the functionality provided in ExportHtml as it provides formatting
|
||||
|
@ -244,15 +220,8 @@ function getTXTFromAtext(pad, atext, authorColors)
|
|||
|
||||
exports.getTXTFromAtext = getTXTFromAtext;
|
||||
|
||||
exports.getPadTXTDocument = function(padId, revNum, callback)
|
||||
exports.getPadTXTDocument = async function(padId, revNum)
|
||||
{
|
||||
padManager.getPad(padId, function(err, pad) {
|
||||
if (ERR(err, callback)) return;
|
||||
|
||||
getPadTXT(pad, revNum, function(err, html) {
|
||||
if (ERR(err, callback)) return;
|
||||
|
||||
callback(null, html);
|
||||
});
|
||||
});
|
||||
};
|
||||
let pad = await padManager.getPad(padId);
|
||||
return getPadTXT(pad, revNum);
|
||||
}
|
||||
|
|
|
@ -15,43 +15,44 @@
|
|||
*/
|
||||
|
||||
var log4js = require('log4js');
|
||||
var async = require("async");
|
||||
var db = require("../db/DB").db;
|
||||
const thenify = require("thenify").withCallback;
|
||||
const db = require("../db/DB");
|
||||
|
||||
exports.setPadRaw = thenify(function(padId, records, callback)
|
||||
exports.setPadRaw = function(padId, records)
|
||||
{
|
||||
records = JSON.parse(records);
|
||||
|
||||
async.eachSeries(Object.keys(records), function(key, cb) {
|
||||
var value = records[key];
|
||||
Object.keys(records).forEach(async function(key) {
|
||||
let value = records[key];
|
||||
|
||||
if (!value) {
|
||||
return setImmediate(cb);
|
||||
return;
|
||||
}
|
||||
|
||||
let newKey;
|
||||
|
||||
if (value.padIDs) {
|
||||
// Author data - rewrite author pad ids
|
||||
value.padIDs[padId] = 1;
|
||||
var newKey = key;
|
||||
newKey = key;
|
||||
|
||||
// Does this author already exist?
|
||||
db.get(key, function(err, author) {
|
||||
if (author) {
|
||||
// Yes, add the padID to the author
|
||||
if (Object.prototype.toString.call(author) === '[object Array]') {
|
||||
author.padIDs.push(padId);
|
||||
}
|
||||
value = author;
|
||||
} else {
|
||||
// No, create a new array with the author info in
|
||||
value.padIDs = [padId];
|
||||
let author = await db.get(key);
|
||||
|
||||
if (author) {
|
||||
// Yes, add the padID to the author
|
||||
if (Object.prototype.toString.call(author) === '[object Array]') {
|
||||
author.padIDs.push(padId);
|
||||
}
|
||||
});
|
||||
|
||||
value = author;
|
||||
} else {
|
||||
// No, create a new array with the author info in
|
||||
value.padIDs = [ padId ];
|
||||
}
|
||||
} else {
|
||||
// Not author data, probably pad data
|
||||
// we can split it to look to see if it's pad data
|
||||
var oldPadId = key.split(":");
|
||||
let oldPadId = key.split(":");
|
||||
|
||||
// we know it's pad data
|
||||
if (oldPadId[0] === "pad") {
|
||||
|
@ -59,16 +60,11 @@ exports.setPadRaw = thenify(function(padId, records, callback)
|
|||
oldPadId[1] = padId;
|
||||
|
||||
// and create the value
|
||||
var newKey = oldPadId.join(":"); // create the new key
|
||||
newKey = oldPadId.join(":"); // create the new key
|
||||
}
|
||||
}
|
||||
|
||||
// Write the value to the server
|
||||
db.set(newKey, value);
|
||||
|
||||
setImmediate(cb);
|
||||
},
|
||||
function() {
|
||||
callback(null, true);
|
||||
await db.set(newKey, value);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -18,9 +18,8 @@ var log4js = require('log4js');
|
|||
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
|
||||
var contentcollector = require("ep_etherpad-lite/static/js/contentcollector");
|
||||
var cheerio = require("cheerio");
|
||||
const thenify = require("thenify").withCallback;
|
||||
|
||||
function setPadHTML(pad, html, callback)
|
||||
exports.setPadHTML = function(pad, html)
|
||||
{
|
||||
var apiLogger = log4js.getLogger("ImportHtml");
|
||||
|
||||
|
@ -44,7 +43,7 @@ function setPadHTML(pad, html, callback)
|
|||
apiLogger.warn("HTML was not properly formed", e);
|
||||
|
||||
// don't process the HTML because it was bad
|
||||
return callback(e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
var result = cc.finish();
|
||||
|
@ -52,7 +51,7 @@ function setPadHTML(pad, html, callback)
|
|||
apiLogger.debug('Lines:');
|
||||
|
||||
var i;
|
||||
for (i = 0; i < result.lines.length; i += 1) {
|
||||
for (i = 0; i < result.lines.length; i++) {
|
||||
apiLogger.debug('Line ' + (i + 1) + ' text: ' + result.lines[i]);
|
||||
apiLogger.debug('Line ' + (i + 1) + ' attributes: ' + result.lineAttribs[i]);
|
||||
}
|
||||
|
@ -92,7 +91,4 @@ function setPadHTML(pad, html, callback)
|
|||
apiLogger.debug('The changeset: ' + theChangeset);
|
||||
pad.setText("\n");
|
||||
pad.appendRevision(theChangeset);
|
||||
callback(null);
|
||||
}
|
||||
|
||||
exports.setPadHTML = thenify(setPadHTML);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue