From d0466a205231320e2a964fea06e7f798e07ed8b0 Mon Sep 17 00:00:00 2001 From: ansuz Date: Wed, 15 Mar 2017 15:46:23 +0100 Subject: [PATCH 1/5] implement 'getChannelSize' api in storage --- storage/file.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/storage/file.js b/storage/file.js index 1584f4a59..ab2bce617 100644 --- a/storage/file.js +++ b/storage/file.js @@ -216,6 +216,14 @@ var getMessages = function (env, chanName, handler, cb) { }); }; +var channelBytes = function (env, chanName, cb) { + var path = mkPath(env, chanName); + Fs.stat(path, function (err, stats) { + if (err) { return void cb(err); } + cb(void 0, stats.size); + }); +}; + module.exports.create = function (conf, cb) { var env = { root: conf.filePath || './datastore', @@ -248,6 +256,9 @@ module.exports.create = function (conf, cb) { flushUnusedChannels: function (cb) { flushUnusedChannels(env, cb); }, + getChannelSize: function (chanName, cb) { + channelBytes(env, chanName, cb); + }, }); }); setInterval(function () { From 61fc4a3f8698f6320b07c98ba99243d015f85fe6 Mon Sep 17 00:00:00 2001 From: ansuz Date: Wed, 15 Mar 2017 15:55:25 +0100 Subject: [PATCH 2/5] respond to GET_FILE_SIZE rpc calls --- NetfluxWebsocketSrv.js | 2 +- rpc.js | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/NetfluxWebsocketSrv.js b/NetfluxWebsocketSrv.js index 0108659fb..2d894a5c0 100644 --- a/NetfluxWebsocketSrv.js +++ b/NetfluxWebsocketSrv.js @@ -214,7 +214,7 @@ const handleMessage = function (ctx, user, msg) { var rpc_call = parsed.slice(1); // slice off the sequence number and pass in the rest of the message - ctx.rpc(rpc_call, function (err, output) { + ctx.rpc(ctx, rpc_call, function (err, output) { if (err) { console.error('[' + err + ']', output); // TODO make this disableable sendMsg(ctx, user, [seq, 'ACK']); diff --git a/rpc.js b/rpc.js index 66973039b..053d8cf6e 100644 --- a/rpc.js +++ b/rpc.js @@ -11,11 +11,15 @@ var getHash = function (ctx, cb) { }; var getTotalSize = function (ctx, cb) { }; var getFileSize = function (ctx, cb) { }; +var isValidChannel = function (chan) { + return /^[a-fA-F0-9]/.test(chan); +}; + RPC.create = function (config, cb) { // load pin-store... console.log('loading rpc module...'); - rpc = function (msg, respond) { + rpc = function (ctx, msg, respond) { switch (msg[0]) { case 'ECHO': respond(void 0, msg); @@ -25,7 +29,14 @@ RPC.create = function (config, cb) { case 'GET_HASH': case 'GET_TOTAL_SIZE': case 'GET_FILE_SIZE': - + if (!isValidChannel(msg[1])) { + return void respond('INVALID_CHAN'); + } + + return void ctx.store.getChannelSize(msg[1], function (e, size) { + if (e) { return void respond(e.code); } + respond(void 0, size); + }); default: respond('UNSUPPORTED_RPC_CALL', msg); break; From 7187d3c5502609b1360ce8a39b97d5101bdba586 Mon Sep 17 00:00:00 2001 From: ansuz Date: Wed, 15 Mar 2017 15:55:55 +0100 Subject: [PATCH 3/5] implement getUserChannelList --- www/common/cryptpad-common.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 190b05508..f0b63d83e 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -987,6 +987,28 @@ define([ }); */ }; + + var getUserChannelList = common.getUserChannelList = function () { + var store = common.getStore(); + var proxy = store.getProxy(); + var fo = proxy.fo; + + var list = fo.getFilesDataFiles().map(function (href) { + var parsed = Cryptpad.parsePadUrl(href); + if (!parsed || !parsed.hash) { return; } + + parsed = Cryptpad.parseHash(parsed.hash); + + var channel = parsed.channel; + if (!channel) { return; } + + var hex = Cryptpad.base64ToHex(channel); + return hex; + }).filter(function (x) { return x; }).sort(); + + return list; + }; + var createButton = common.createButton = function (type, rightside, data, callback) { var button; var size = "17px"; From bd9efd7c045fdec74995e323201cdc2652bb39b1 Mon Sep 17 00:00:00 2001 From: ansuz Date: Wed, 15 Mar 2017 15:56:32 +0100 Subject: [PATCH 4/5] provide examples of valid and invalid rpc calls --- www/examples/rpc/main.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/www/examples/rpc/main.js b/www/examples/rpc/main.js index 528290d66..37b1751d7 100644 --- a/www/examples/rpc/main.js +++ b/www/examples/rpc/main.js @@ -30,6 +30,20 @@ define([ if (e) { return void console.error(e); } console.log(msg); }); + + var list = Cryptpad.getUserChannelList(); + if (list.length) { + rpc.send('GET_FILE_SIZE', list[0], function (e, msg) { + if (e) { + return void console.error(e); + } + console.log(msg); + }); + } + rpc.send('GET_FILE_SIZE', 'pewpew', function (e, msg) { + if (e) { return void console.error(e); } + console.log(msg); + }); }); }); }); From 15a67afc754e78f423718ee429c3da673423ca80 Mon Sep 17 00:00:00 2001 From: ansuz Date: Wed, 15 Mar 2017 16:04:52 +0100 Subject: [PATCH 5/5] jshint compliance --- rpc.js | 2 +- www/common/cryptpad-common.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rpc.js b/rpc.js index 053d8cf6e..cff3dfeb0 100644 --- a/rpc.js +++ b/rpc.js @@ -19,7 +19,7 @@ RPC.create = function (config, cb) { // load pin-store... console.log('loading rpc module...'); - rpc = function (ctx, msg, respond) { + var rpc = function (ctx, msg, respond) { switch (msg[0]) { case 'ECHO': respond(void 0, msg); diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index f0b63d83e..cde8840ac 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -994,15 +994,15 @@ define([ var fo = proxy.fo; var list = fo.getFilesDataFiles().map(function (href) { - var parsed = Cryptpad.parsePadUrl(href); + var parsed = common.parsePadUrl(href); if (!parsed || !parsed.hash) { return; } - parsed = Cryptpad.parseHash(parsed.hash); + parsed = common.parseHash(parsed.hash); var channel = parsed.channel; if (!channel) { return; } - var hex = Cryptpad.base64ToHex(channel); + var hex = common.base64ToHex(channel); return hex; }).filter(function (x) { return x; }).sort();