mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-22 23:42:54 +01:00
* refactor: move src/cacheCreate.js to src/cache/lru.js * fix: call new library location for lru cache creator * feat: add ttl cache * fix: update upload throttler to use ttl cache instead of lru cache * chore: add missing dependency * fix: avoid pubsub conflicts * fix: use get instead of peek, which is not available in ttl-cache
30 lines
778 B
JavaScript
30 lines
778 B
JavaScript
'use strict';
|
|
|
|
const cacheCreate = require('../cache/ttl');
|
|
const meta = require('../meta');
|
|
const helpers = require('./helpers');
|
|
const user = require('../user');
|
|
|
|
const cache = cacheCreate({
|
|
ttl: meta.config.uploadRateLimitCooldown * 1000,
|
|
});
|
|
|
|
exports.clearCache = function () {
|
|
cache.clear();
|
|
};
|
|
|
|
exports.ratelimit = helpers.try(async (req, res, next) => {
|
|
const { uid } = req;
|
|
if (!meta.config.uploadRateLimitThreshold || (uid && await user.isAdminOrGlobalMod(uid))) {
|
|
return next();
|
|
}
|
|
|
|
const count = (cache.get(`${req.ip}:uploaded_file_count`) || 0) + req.files.files.length;
|
|
if (count > meta.config.uploadRateLimitThreshold) {
|
|
return next(new Error(['[[error:upload-ratelimit-reached]]']));
|
|
}
|
|
cache.set(`${req.ip}:uploaded_file_count`, count);
|
|
next();
|
|
});
|
|
|