mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add maxTotalChatBackups config.yaml value
This commit is contained in:
14
src/util.js
14
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user