diff --git a/lib/channel/channel.js b/lib/channel/channel.js index 13985793..2ecfde1a 100644 --- a/lib/channel/channel.js +++ b/lib/channel/channel.js @@ -9,6 +9,8 @@ var path = require("path"); var sio = require("socket.io"); var db = require("../database"); +const SIZE_LIMIT = 1048576; + /** * Previously, async channel functions were riddled with race conditions due to * an event causing the channel to be unloaded while a pending callback still @@ -130,6 +132,23 @@ Channel.prototype.initModules = function () { self.logger.log("[init] Loaded modules: " + inited.join(", ")); }; +Channel.prototype.getDiskSize = function (cb) { + if (this._getDiskSizeTimeout > Date.now()) { + return cb(null, this._cachedDiskSize); + } + + var self = this; + var file = path.join(__dirname, "..", "..", "chandump", self.uniqueName); + fs.stat(file, function (err, stats) { + if (err) { + return cb(err); + } + + self._cachedDiskSize = stats.size; + cb(null, self._cachedDiskSize); + }); +}; + Channel.prototype.loadState = function () { var self = this; var file = path.join(__dirname, "..", "..", "chandump", self.uniqueName); @@ -158,7 +177,7 @@ Channel.prototype.loadState = function () { if (!err) { var mb = stats.size / 1048576; mb = Math.floor(mb * 100) / 100; - if (mb > 1) { + if (mb > SIZE_LIMIT / 1048576) { Logger.errlog.log("Large chandump detected: " + self.uniqueName + " (" + mb + " MiB)"); var msg = "This channel's state size has exceeded the memory limit " + @@ -233,6 +252,20 @@ Channel.prototype.saveState = function () { * channels. */ var err = fs.writeFileSync(file, json); + + // Check for large chandump and warn moderators/admins + self.getDiskSize(function (err, size) { + if (!err && size > SIZE_LIMIT && self.users) { + self.users.forEach(function (u) { + if (u.account.effectiveRank >= 2) { + u.socket.emit("warnLargeChandump", { + limit: SIZE_LIMIT, + actual: size + }); + } + }); + } + }); }; Channel.prototype.checkModules = function (fn, args, cb) { diff --git a/www/js/callbacks.js b/www/js/callbacks.js index 0e8f563b..a258458e 100644 --- a/www/js/callbacks.js +++ b/www/js/callbacks.js @@ -1072,6 +1072,28 @@ Callbacks = { row.hide("fade", row.remove.bind(row)); CHANNEL.emotes.splice(i, 1); } + }, + + warnLargeChandump: function (data) { + function toHumanReadable(size) { + if (size > 1048576) { + return Math.floor((size / 1048576) * 100) / 100 + "MiB"; + } else if (size > 1024) { + return Math.floor((size / 1024) * 100) / 100 + "KiB"; + } else { + return size + "B"; + } + } + + if ($("#chandumptoobig").length > 0) { + $("#chandumptoobig").remove(); + } + + errDialog("This channel currently exceeds the maximum size of " + + toHumanReadable(data.limit) + " (channel size is " + + toHumanReadable(data.actual) + "). Please reduce the size by removing " + + "unneeded playlist items, filters, and/or emotes or else the channel will " + + "be unable to load the next time it is reloaded").attr("id", "chandumptoobig"); } }