Update and return the storage limit

This commit is contained in:
yflory 2017-05-11 16:12:44 +02:00
parent b76dcba1d6
commit 57ee7de7d4
4 changed files with 94 additions and 6 deletions

View File

@ -116,6 +116,12 @@ module.exports = {
'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.
These status of these alternatives are specified in their READMEs,

62
rpc.js
View File

@ -7,10 +7,14 @@ var Nacl = require("tweetnacl");
var Fs = require("fs");
var Path = require("path");
var Https = require("https");
var RPC = module.exports;
var Store = require("./storage/file");
var config = require('./config');
var DEFAULT_LIMIT = 100;
var isValidChannel = function (chan) {
return /^[a-fA-F0-9]/.test(chan) ||
@ -454,8 +458,42 @@ var isPrivilegedUser = function (publicKey, cb) {
});
};
var getLimit = function (cb) {
cb = cb; // TODO
var limits = {};
var updateLimits = function (publicKey, cb) {
if (typeof cb !== "function") { cb = function () {}; }
var domain = config.domain;
var options = {
host: 'accounts.cryptpad.fr',
path: '/api/getAuthorized?domain=' + encodeURIComponent(domain)
};
var callback = function (response) {
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) {
l = typeof limits[publicKey] === "number" ? limits[publicKey] : DEFAULT_LIMIT;
}
cb(void 0, l);
} catch (e) {
cb(e);
}
});
};
Https.get(options, callback).on('error', function (e) {
console.error(e);
cb(e);
});
};
var getLimit = function (publicKey, cb) {
return void cb(null, typeof limits[publicKey] === "number" ? limits[publicKey] : DEFAULT_LIMIT);
};
var safeMkdir = function (path, cb) {
@ -714,10 +752,16 @@ RPC.create = function (config /*:typeof(ConfigType)*/, cb /*:(?Error, ?Function)
});
case 'GET_FILE_SIZE':
return void getFileSize(ctx.store, msg[1], Respond);
case 'GET_LIMIT': // TODO implement this and cache it per-user
return void getLimit(function (e, limit) {
case 'UPDATE_LIMITS':
return void updateLimits(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;
Respond('NOT_IMPLEMENTED');
Respond(void 0, limit);
});
case 'GET_MULTIPLE_FILE_SIZE':
return void getMultipleFileSize(ctx.store, msg[1], function (e, dict) {
@ -775,6 +819,14 @@ RPC.create = function (config /*:typeof(ConfigType)*/, cb /*:(?Error, ?Function)
handleMessage(session.privilege);
};
var updateLimitDaily = function () {
updateLimits(function (e) {
if (e) { console.error('Error updating the storage limits', e); }
});
};
updateLimitDaily();
setInterval(updateLimitDaily, 24*3600*1000);
Store.create({
filePath: pinPath,
}, 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) {
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) {

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);
});
};