From 3849908fe1322436d6fa437eb0c48957d8772cf2 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sat, 7 Dec 2024 15:37:57 +0200 Subject: [PATCH 1/3] Add maxTotalChatBackups config.yaml value --- default/config.yaml | 2 ++ src/endpoints/chats.js | 7 +++++++ src/util.js | 14 +++++++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/default/config.yaml b/default/config.yaml index 730ffa967..22276d567 100644 --- a/default/config.yaml +++ b/default/config.yaml @@ -98,6 +98,8 @@ skipContentCheck: false disableChatBackup: false # Number of backups to keep for each chat and settings file numberOfBackups: 50 +# Maximum number of chat backups to keep per user (starting from the most recent). Set to -1 to keep all backups. +maxTotalChatBackups: 500 # Interval in milliseconds to throttle chat backups per user chatBackupThrottleInterval: 10000 # Allowed hosts for card downloads diff --git a/src/endpoints/chats.js b/src/endpoints/chats.js index 2eb8c379e..dbd20687b 100644 --- a/src/endpoints/chats.js +++ b/src/endpoints/chats.js @@ -32,6 +32,13 @@ function backupChat(directory, name, chat) { writeFileAtomicSync(backupFile, chat, 'utf-8'); removeOldBackups(directory, `chat_${name}_`); + + const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', 500)); + if (isNaN(maxTotalChatBackups) || maxTotalChatBackups < 0) { + return; + } + + removeOldBackups(directory, 'chat_', maxTotalChatBackups); } catch (err) { console.log(`Could not backup chat for ${name}`, err); } diff --git a/src/util.js b/src/util.js index 7b8445453..b07142e6b 100644 --- a/src/util.js +++ b/src/util.js @@ -376,16 +376,24 @@ export function generateTimestamp() { * Remove old backups with the given prefix from a specified directory. * @param {string} directory The root directory to remove backups from. * @param {string} prefix File prefix to filter backups by. + * @param {number?} limit Maximum number of backups to keep. If null, the limit is determined by the `numberOfBackups` config value. */ -export function removeOldBackups(directory, prefix) { - const MAX_BACKUPS = Number(getConfigValue('numberOfBackups', 50)); +export function removeOldBackups(directory, prefix, limit = null) { + const MAX_BACKUPS = limit ?? Number(getConfigValue('numberOfBackups', 50)); let files = fs.readdirSync(directory).filter(f => f.startsWith(prefix)); if (files.length > MAX_BACKUPS) { files = files.map(f => path.join(directory, f)); files.sort((a, b) => fs.statSync(a).mtimeMs - fs.statSync(b).mtimeMs); - fs.rmSync(files[0]); + while (files.length > MAX_BACKUPS) { + const oldest = files.shift(); + if (!oldest) { + break; + } + + fs.rmSync(oldest); + } } } From 7098e17d2225358e4ede003b12895c8eedbf9441 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 8 Dec 2024 14:04:13 +0200 Subject: [PATCH 2/3] Change default value of maxTotalChatBackups --- default/config.yaml | 2 +- src/endpoints/chats.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/default/config.yaml b/default/config.yaml index 22276d567..a3de53021 100644 --- a/default/config.yaml +++ b/default/config.yaml @@ -99,7 +99,7 @@ disableChatBackup: false # Number of backups to keep for each chat and settings file numberOfBackups: 50 # Maximum number of chat backups to keep per user (starting from the most recent). Set to -1 to keep all backups. -maxTotalChatBackups: 500 +maxTotalChatBackups: -1 # Interval in milliseconds to throttle chat backups per user chatBackupThrottleInterval: 10000 # Allowed hosts for card downloads diff --git a/src/endpoints/chats.js b/src/endpoints/chats.js index dbd20687b..70817e037 100644 --- a/src/endpoints/chats.js +++ b/src/endpoints/chats.js @@ -33,7 +33,7 @@ function backupChat(directory, name, chat) { removeOldBackups(directory, `chat_${name}_`); - const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', 500)); + const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', -1)); if (isNaN(maxTotalChatBackups) || maxTotalChatBackups < 0) { return; } From 210fac321b222420a26b6b37a320c02560e89094 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 8 Dec 2024 14:05:14 +0200 Subject: [PATCH 3/3] Move config reading to top-level --- src/endpoints/chats.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/endpoints/chats.js b/src/endpoints/chats.js index 70817e037..1e7a00d08 100644 --- a/src/endpoints/chats.js +++ b/src/endpoints/chats.js @@ -11,6 +11,10 @@ import _ from 'lodash'; import { jsonParser, urlencodedParser } from '../express-common.js'; import { getConfigValue, humanizedISO8601DateTime, tryParse, generateTimestamp, removeOldBackups } from '../util.js'; +const isBackupDisabled = getConfigValue('disableChatBackup', false); +const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', -1)); +const throttleInterval = getConfigValue('chatBackupThrottleInterval', 10_000); + /** * Saves a chat to the backups directory. * @param {string} directory The user's backups directory. @@ -19,7 +23,6 @@ import { getConfigValue, humanizedISO8601DateTime, tryParse, generateTimestamp, */ function backupChat(directory, name, chat) { try { - const isBackupDisabled = getConfigValue('disableChatBackup', false); if (isBackupDisabled) { return; @@ -33,7 +36,6 @@ function backupChat(directory, name, chat) { removeOldBackups(directory, `chat_${name}_`); - const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', -1)); if (isNaN(maxTotalChatBackups) || maxTotalChatBackups < 0) { return; } @@ -52,7 +54,6 @@ const backupFunctions = new Map(); * @returns {function(string, string, string): void} Backup function */ function getBackupFunction(handle) { - const throttleInterval = getConfigValue('chatBackupThrottleInterval', 10_000); if (!backupFunctions.has(handle)) { backupFunctions.set(handle, _.throttle(backupChat, throttleInterval, { leading: true, trailing: true })); }