Files
NodeBB/src/middleware/uploads.js
Barış Soner Uşaklı 4430de8c4a fix: don't error if file required too early
Exception during run: TypeError: ttl must be positive integer or Infinity if set
    at new TTLCache (/home/runner/work/NodeBB/NodeBB/node_modules/@isaacs/ttlcache/index.js:35:13)
2024-06-11 12:09:24 -04:00

34 lines
824 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({
ttl: meta.config.uploadRateLimitCooldown * 1000,
});
}
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();
});