diff --git a/NetfluxWebsocketSrv.js b/NetfluxWebsocketSrv.js index d3ba89694..b8c4f5a2a 100644 --- a/NetfluxWebsocketSrv.js +++ b/NetfluxWebsocketSrv.js @@ -230,7 +230,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..cff3dfeb0 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) { + var 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; 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 () { diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 625568a24..65c7a7fa1 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 = common.parsePadUrl(href); + if (!parsed || !parsed.hash) { return; } + + parsed = common.parseHash(parsed.hash); + + var channel = parsed.channel; + if (!channel) { return; } + + var hex = common.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"; 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); + }); }); }); });