Add maxTotalChatBackups config.yaml value

3849908fe1322436d6fa437eb0c48957d8772cf2

Cohee <18619528+Cohee1207@users.noreply.github.com>

3 files changed, +20 -3Ignore whitespace
default/config.yaml+2 -0
@@ -98,6 +98,8 @@ skipContentCheck: false
98disableChatBackup: false98disableChatBackup: false
99# Number of backups to keep for each chat and settings file99# Number of backups to keep for each chat and settings file
100numberOfBackups: 50100numberOfBackups: 50
101# Maximum number of chat backups to keep per user (starting from the most recent). Set to -1 to keep all backups.
102maxTotalChatBackups: 500
101# Interval in milliseconds to throttle chat backups per user103# Interval in milliseconds to throttle chat backups per user
102chatBackupThrottleInterval: 10000104chatBackupThrottleInterval: 10000
103# Allowed hosts for card downloads105# Allowed hosts for card downloads
src/endpoints/chats.js+7 -0
@@ -32,6 +32,13 @@ function backupChat(directory, name, chat) {
32 writeFileAtomicSync(backupFile, chat, 'utf-8');32 writeFileAtomicSync(backupFile, chat, 'utf-8');
3333
34 removeOldBackups(directory, `chat_${name}_`);34 removeOldBackups(directory, `chat_${name}_`);
35
36 const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', 500));
37 if (isNaN(maxTotalChatBackups) || maxTotalChatBackups < 0) {
38 return;
39 }
40
41 removeOldBackups(directory, 'chat_', maxTotalChatBackups);
35 } catch (err) {42 } catch (err) {
36 console.log(`Could not backup chat for ${name}`, err);43 console.log(`Could not backup chat for ${name}`, err);
37 }44 }
src/util.js+11 -3
@@ -376,16 +376,24 @@ export function generateTimestamp() {
376 * Remove old backups with the given prefix from a specified directory.376 * Remove old backups with the given prefix from a specified directory.
377 * @param {string} directory The root directory to remove backups from.377 * @param {string} directory The root directory to remove backups from.
378 * @param {string} prefix File prefix to filter backups by.378 * @param {string} prefix File prefix to filter backups by.
379 * @param {number?} limit Maximum number of backups to keep. If null, the limit is determined by the `numberOfBackups` config value.
379 */380 */
380export function removeOldBackups(directory, prefix) {381export function removeOldBackups(directory, prefix, limit = null) {
381 const MAX_BACKUPS = Number(getConfigValue('numberOfBackups', 50));382 const MAX_BACKUPS = limit ?? Number(getConfigValue('numberOfBackups', 50));
382383
383 let files = fs.readdirSync(directory).filter(f => f.startsWith(prefix));384 let files = fs.readdirSync(directory).filter(f => f.startsWith(prefix));
384 if (files.length > MAX_BACKUPS) {385 if (files.length > MAX_BACKUPS) {
385 files = files.map(f => path.join(directory, f));386 files = files.map(f => path.join(directory, f));
386 files.sort((a, b) => fs.statSync(a).mtimeMs - fs.statSync(b).mtimeMs);387 files.sort((a, b) => fs.statSync(a).mtimeMs - fs.statSync(b).mtimeMs);
387388
388 fs.rmSync(files[0]);389 while (files.length > MAX_BACKUPS) {
390 const oldest = files.shift();
391 if (!oldest) {
392 break;
393 }
394
395 fs.rmSync(oldest);
396 }
389 }397 }
390}398}
391399