Merge branch 'limit' into staging

This commit is contained in:
ansuz 2017-05-12 14:21:46 +02:00
commit 7dd5907ff0
4 changed files with 112 additions and 6 deletions

View File

@ -116,6 +116,12 @@ module.exports = {
'contact', 'contact',
], ],
/* Domain
* If you want to have enable payments on your CryptPad instance, it has to be able to tell
* our account server what is your domain
*/
// domain: 'https://cryptpad.fr',
/* /*
You have the option of specifying an alternative storage adaptor. You have the option of specifying an alternative storage adaptor.
These status of these alternatives are specified in their READMEs, These status of these alternatives are specified in their READMEs,

80
rpc.js
View File

@ -7,11 +7,14 @@ var Nacl = require("tweetnacl");
var Fs = require("fs"); var Fs = require("fs");
var Path = require("path"); var Path = require("path");
var Https = require("https");
var RPC = module.exports; var RPC = module.exports;
var Store = require("./storage/file"); var Store = require("./storage/file");
var DEFAULT_LIMIT = 100;
var isValidChannel = function (chan) { var isValidChannel = function (chan) {
return /^[a-fA-F0-9]/.test(chan) || return /^[a-fA-F0-9]/.test(chan) ||
[32, 48].indexOf(chan.length) !== -1; [32, 48].indexOf(chan.length) !== -1;
@ -454,8 +457,61 @@ var isPrivilegedUser = function (publicKey, cb) {
}); });
}; };
var getLimit = function (cb) { // The limits object contains storage limits for all the publicKey that have paid
cb = cb; // TODO // To each key is associated an object containing the 'limit' value and a 'note' explaining that limit
var limits = {};
var updateLimits = function (config, publicKey, cb) {
if (typeof cb !== "function") { cb = function () {}; }
var body = JSON.stringify({
domain: config.domain,
subdomain: config.subdomain
});
var options = {
host: 'accounts.cryptpad.fr',
path: '/api/getauthorized',
method: 'POST',
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(body)
}
};
var req = Https.request(options, function (response) {
if (!('' + req.statusCode).match(/^2\d\d$/)) {
return void cb('SERVER ERROR ' + req.statusCode);
}
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
try {
var json = JSON.parse(str);
limits = json;
var l;
if (publicKey) {
var limit = limits[publicKey];
l = limit && typeof limit.limit === "number" ? limit.limit : DEFAULT_LIMIT;
}
cb(void 0, l);
} catch (e) {
cb(e);
}
});
});
req.on('error', function (e) {
if (!config.domain) { return cb(); }
cb(e);
});
req.end(body);
};
var getLimit = function (publicKey, cb) {
var limit = limits[publicKey];
return limit && typeof limit.limit === "number" ? limit.limit : DEFAULT_LIMIT;
}; };
var safeMkdir = function (path, cb) { var safeMkdir = function (path, cb) {
@ -714,10 +770,16 @@ RPC.create = function (config /*:typeof(ConfigType)*/, cb /*:(?Error, ?Function)
}); });
case 'GET_FILE_SIZE': case 'GET_FILE_SIZE':
return void getFileSize(ctx.store, msg[1], Respond); return void getFileSize(ctx.store, msg[1], Respond);
case 'GET_LIMIT': // TODO implement this and cache it per-user case 'UPDATE_LIMITS':
return void getLimit(function (e, limit) { return void updateLimits(config, safeKey, function (e, limit) {
if (e) { return void Respond(e); }
Respond(void 0, limit);
});
case 'GET_LIMIT':
return void getLimit(safeKey, function (e, limit) {
if (e) { return void Respond(e); }
limit = limit; limit = limit;
Respond('NOT_IMPLEMENTED'); Respond(void 0, limit);
}); });
case 'GET_MULTIPLE_FILE_SIZE': case 'GET_MULTIPLE_FILE_SIZE':
return void getMultipleFileSize(ctx.store, msg[1], function (e, dict) { return void getMultipleFileSize(ctx.store, msg[1], function (e, dict) {
@ -775,6 +837,14 @@ RPC.create = function (config /*:typeof(ConfigType)*/, cb /*:(?Error, ?Function)
handleMessage(session.privilege); handleMessage(session.privilege);
}; };
var updateLimitDaily = function () {
updateLimits(config, undefined, function (e) {
if (e) { console.error('Error updating the storage limits', e); }
});
};
updateLimitDaily();
setInterval(updateLimitDaily, 24*3600*1000);
Store.create({ Store.create({
filePath: pinPath, filePath: pinPath,
}, function (s) { }, function (s) {

View File

@ -744,8 +744,15 @@ define([
}); });
}; };
common.updatePinLimit = function (cb) {
if (!pinsReady()) { return void cb('[RPC_NOT_READY]'); }
rpc.getFileListSize(cb);
};
common.getPinLimit = function (cb) { common.getPinLimit = function (cb) {
cb(void 0, typeof(AppConfig.pinLimit) === 'number'? AppConfig.pinLimit: 1000); if (!pinsReady()) { return void cb('[RPC_NOT_READY]'); }
rpc.getFileListSize(cb);
//cb(void 0, typeof(AppConfig.pinLimit) === 'number'? AppConfig.pinLimit: 1000);
}; };
common.isOverPinLimit = function (cb) { common.isOverPinLimit = function (cb) {

View File

@ -121,6 +121,29 @@ define([
}); });
}; };
// Update the limit value for all the users and return the limit for your publicKey
exp.updatePinLimits = function (cb) {
rpc.send('UPDATE_LIMITS', undefined, function (e, response) {
if (e) { return void cb(e); }
if (response && typeof response === "number") {
cb (void 0, response);
} else {
cb('INVALID_RESPONSE');
}
});
};
// Get the storage limit associated with your publicKey
exp.getLimit = function (cb) {
rpc.send('GET_LIMIT', undefined, function (e, response) {
if (e) { return void cb(e); }
if (response && typeof response === "number") {
cb (void 0, response);
} else {
cb('INVALID_RESPONSE');
}
});
};
cb(e, exp); cb(e, exp);
}); });
}; };