Add maxTotalChatBackups config.yaml value

This commit is contained in:
Cohee
2024-12-07 15:37:57 +02:00
parent 54ca9477dd
commit 3849908fe1
3 changed files with 20 additions and 3 deletions

View File

@@ -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);
}
}
}