diff options
Diffstat (limited to 'node/db/PadManager.js')
-rw-r--r-- | node/db/PadManager.js | 54 |
1 files changed, 50 insertions, 4 deletions
diff --git a/node/db/PadManager.js b/node/db/PadManager.js index 04ab7eb7..f0ce8818 100644 --- a/node/db/PadManager.js +++ b/node/db/PadManager.js @@ -19,6 +19,7 @@ */ require("../db/Pad"); +var db = require("./DB").db; /** * A Array with all known Pads @@ -30,8 +31,40 @@ globalPads = []; * @param id A String with the id of the pad * @param {Function} callback */ -exports.getPad = function(id, callback) +exports.getPad = function(id, text, callback) { + //check if this is a valid padId + if(!exports.isValidPadId(id)) + { + callback({stop: id + " is not a valid padId"}); + return; + } + + //make text an optional parameter + if(typeof text == "function") + { + callback = text; + text = null; + } + + //check if this is a valid text + if(text != null) + { + //check if text is a string + if(typeof text != "string") + { + callback({stop: "text is not a string"}); + return; + } + + //check if text is less than 100k chars + if(text.length > 100000) + { + callback({stop: "text must be less than 100k chars"}); + return; + } + } + var pad = globalPads[id]; //return pad if its already loaded @@ -45,7 +78,7 @@ exports.getPad = function(id, callback) pad = new Pad(id); //initalize the pad - pad.init(function(err) + pad.init(text, function(err) { if(err) { @@ -58,6 +91,19 @@ exports.getPad = function(id, callback) } }); } - - //globalPads[id].timestamp = new Date().getTime(); } + +//checks if a pad exists +exports.doesPadExists = function(padId, callback) +{ + db.get("pad:"+padId, function(err, value) + { + callback(err, value != null); + }); +} + +exports.isValidPadId = function(padId) +{ + return /^([0-9]+\$)?[^$]{1,50}$/.test(padId); +} + |