mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-19 05:52:57 +01:00
36 lines
867 B
JavaScript
36 lines
867 B
JavaScript
'use strict';
|
|
|
|
const cacheCreate = require('../cache/ttl');
|
|
const meta = require('../meta');
|
|
const helpers = require('./helpers');
|
|
const user = require('../user');
|
|
|
|
let cache;
|
|
|
|
exports.clearCache = function () {
|
|
if (cache) {
|
|
cache.clear();
|
|
}
|
|
};
|
|
|
|
exports.ratelimit = helpers.try(async (req, res, next) => {
|
|
const { uid } = req;
|
|
if (!meta.config.uploadRateLimitThreshold || (uid && await user.isAdminOrGlobalMod(uid))) {
|
|
return next();
|
|
}
|
|
if (!cache) {
|
|
cache = cacheCreate({
|
|
name: 'upload-rate-limit-cache',
|
|
max: 100,
|
|
ttl: meta.config.uploadRateLimitCooldown * 1000,
|
|
});
|
|
}
|
|
const count = (cache.get(`${req.ip}:uploaded_file_count`) || 0) + req.files.length;
|
|
if (count > meta.config.uploadRateLimitThreshold) {
|
|
return next(new Error(['[[error:upload-ratelimit-reached]]']));
|
|
}
|
|
cache.set(`${req.ip}:uploaded_file_count`, count);
|
|
next();
|
|
});
|
|
|