support batch getFileSize calls

This commit is contained in:
ansuz 2017-04-21 14:51:00 +02:00
parent 1af621bfe5
commit c51073f211
2 changed files with 54 additions and 1 deletions

40
rpc.js
View file

@ -212,14 +212,45 @@ var getChannelList = function (store, Sessions, publicKey, cb) {
var getFileSize = function (store, channel, cb) {
if (!isValidChannel(channel)) { return void cb('INVALID_CHAN'); }
if (typeof(store.getChannelSize) !== 'function') {
return cb('GET_CHANNEL_SIZE_UNSUPPORTED');
}
// TODO don't blow up if their store doesn't have this API
return void store.getChannelSize(channel, function (e, size) {
if (e) { return void cb(e.code); }
cb(void 0, size);
});
};
var getMultipleFileSize = function (store, channels, cb) {
if (!isValidChannel(channel)) { }
if (!Array.isArray(channels)) { return cb('INVALID_LIST'); }
if (typeof(store.getChannelSize) !== 'function') {
return cb('GET_CHANNEL_SIZE_UNSUPPORTED');
}
var i = channels.length;
var counts = {};
var done = function () {
i--;
if (i === 0) { return cb(void 0, counts); }
};
channels.forEach(function (channel) {
store.getChannelSize(channel, function (e, size) {
if (e) {
counts[channel] = -1;
return done();
}
counts[channel] = size;
done();
});
});
};
var getTotalSize = function (pinStore, messageStore, Sessions, publicKey, cb) {
var bytes = 0;
@ -430,6 +461,13 @@ RPC.create = function (config, cb) {
});
case 'GET_FILE_SIZE':
return void getFileSize(ctx.store, msg[1], Respond);
case 'GET_MULTIPLE_FILE_SIZE':
return void getMultipleFileSize(ctx.store, msg[1], function (e, dict) {
if (e) { return void Respond(e); }
Respond(void 0, dict);
});
return void Respond('NOT_IMPLEMENTED');
break;
default:
return void Respond('UNSUPPORTED_RPC_CALL', msg);
}

View file

@ -93,6 +93,21 @@ define([
});
};
// take a list of channels and return a dictionary of their sizes
exp.getMultipleFileSize = function (files, cb) {
if (!Array.isArray(files)) {
return window.setTimeout(function () {
cb('[TypeError] pin expects an array');
});
}
rpc.send('GET_MULTIPLE_FILE_SIZE', files, function (e, res) {
if (e) { return void cb(e); }
if (typeof(res) !== 'object') {
return void cb('INVALID_RESPONSE');
}
})
};
// get the combined size of all channels (in bytes) for all the
// channels which the server has pinned for your publicKey
exp.getFileListSize = function (cb) {