mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-02-22 06:57:41 +01:00
Add chat file backups
This commit is contained in:
parent
6369ca6483
commit
5dbe2ebf29
@ -10,6 +10,7 @@ const listen = true; // If true, Can be access from other device or PC. otherwis
|
|||||||
const allowKeysExposure = false; // If true, private API keys could be fetched to the frontend.
|
const allowKeysExposure = false; // If true, private API keys could be fetched to the frontend.
|
||||||
const skipContentCheck = false; // If true, no new default content will be delivered to you.
|
const skipContentCheck = false; // If true, no new default content will be delivered to you.
|
||||||
const thumbnailsQuality = 95; // Quality of thumbnails. 0-100
|
const thumbnailsQuality = 95; // Quality of thumbnails. 0-100
|
||||||
|
const disableChatBackup = false; // Disables the backup of chat logs to the /backups folder
|
||||||
|
|
||||||
// If true, Allows insecure settings for listen, whitelist, and authentication.
|
// If true, Allows insecure settings for listen, whitelist, and authentication.
|
||||||
// Change this setting only on "trusted networks". Do not change this value unless you are aware of the issues that can arise from changing this setting and configuring a insecure setting.
|
// Change this setting only on "trusted networks". Do not change this value unless you are aware of the issues that can arise from changing this setting and configuring a insecure setting.
|
||||||
@ -51,4 +52,5 @@ module.exports = {
|
|||||||
requestOverrides,
|
requestOverrides,
|
||||||
thumbnailsQuality,
|
thumbnailsQuality,
|
||||||
extras,
|
extras,
|
||||||
|
disableChatBackup,
|
||||||
};
|
};
|
||||||
|
54
server.js
54
server.js
@ -692,6 +692,7 @@ app.post("/savechat", jsonParser, function (request, response) {
|
|||||||
let chat_data = request.body.chat;
|
let chat_data = request.body.chat;
|
||||||
let jsonlData = chat_data.map(JSON.stringify).join('\n');
|
let jsonlData = chat_data.map(JSON.stringify).join('\n');
|
||||||
writeFileAtomicSync(`${chatsPath + sanitize(dir_name)}/${sanitize(String(request.body.file_name))}.jsonl`, jsonlData, 'utf8');
|
writeFileAtomicSync(`${chatsPath + sanitize(dir_name)}/${sanitize(String(request.body.file_name))}.jsonl`, jsonlData, 'utf8');
|
||||||
|
backupChat(dir_name, jsonlData)
|
||||||
return response.send({ result: "ok" });
|
return response.send({ result: "ok" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
response.send(error);
|
response.send(error);
|
||||||
@ -2652,6 +2653,7 @@ app.post('/savegroupchat', jsonParser, (request, response) => {
|
|||||||
let chat_data = request.body.chat;
|
let chat_data = request.body.chat;
|
||||||
let jsonlData = chat_data.map(JSON.stringify).join('\n');
|
let jsonlData = chat_data.map(JSON.stringify).join('\n');
|
||||||
writeFileAtomicSync(pathToFile, jsonlData, 'utf8');
|
writeFileAtomicSync(pathToFile, jsonlData, 'utf8');
|
||||||
|
backupChat(String(id), jsonlData);
|
||||||
return response.send({ ok: true });
|
return response.send({ ok: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -3541,10 +3543,7 @@ if (true === cliArguments.ssl) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function backupSettings() {
|
function generateTimestamp() {
|
||||||
const MAX_BACKUPS = 25;
|
|
||||||
|
|
||||||
function generateTimestamp() {
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const year = now.getFullYear();
|
const year = now.getFullYear();
|
||||||
const month = String(now.getMonth() + 1).padStart(2, '0');
|
const month = String(now.getMonth() + 1).padStart(2, '0');
|
||||||
@ -3554,8 +3553,38 @@ function backupSettings() {
|
|||||||
const seconds = String(now.getSeconds()).padStart(2, '0');
|
const seconds = String(now.getSeconds()).padStart(2, '0');
|
||||||
|
|
||||||
return `${year}${month}${day}-${hours}${minutes}${seconds}`;
|
return `${year}${month}${day}-${hours}${minutes}${seconds}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} name
|
||||||
|
* @param {string} chat
|
||||||
|
*/
|
||||||
|
function backupChat(name, chat) {
|
||||||
|
try {
|
||||||
|
const isBackupDisabled = config.disableChatBackup;
|
||||||
|
|
||||||
|
if (isBackupDisabled) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!fs.existsSync(DIRECTORIES.backups)) {
|
||||||
|
fs.mkdirSync(DIRECTORIES.backups);
|
||||||
|
}
|
||||||
|
|
||||||
|
// replace non-alphanumeric characters with underscores
|
||||||
|
name = sanitize(name).replace(/[^a-z0-9]/gi, '_').toLowerCase();
|
||||||
|
|
||||||
|
const backupFile = path.join(DIRECTORIES.backups, `chat_${name}_${generateTimestamp()}.json`);
|
||||||
|
writeFileAtomicSync(backupFile, chat, 'utf-8');
|
||||||
|
|
||||||
|
removeOldBackups(`chat_${name}_`);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(`Could not backup chat for ${name}`, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function backupSettings() {
|
||||||
try {
|
try {
|
||||||
if (!fs.existsSync(DIRECTORIES.backups)) {
|
if (!fs.existsSync(DIRECTORIES.backups)) {
|
||||||
fs.mkdirSync(DIRECTORIES.backups);
|
fs.mkdirSync(DIRECTORIES.backups);
|
||||||
@ -3564,16 +3593,25 @@ function backupSettings() {
|
|||||||
const backupFile = path.join(DIRECTORIES.backups, `settings_${generateTimestamp()}.json`);
|
const backupFile = path.join(DIRECTORIES.backups, `settings_${generateTimestamp()}.json`);
|
||||||
fs.copyFileSync(SETTINGS_FILE, backupFile);
|
fs.copyFileSync(SETTINGS_FILE, backupFile);
|
||||||
|
|
||||||
let files = fs.readdirSync(DIRECTORIES.backups).filter(f => f.startsWith('settings_'));
|
removeOldBackups('settings_');
|
||||||
|
} catch (err) {
|
||||||
|
console.log('Could not backup settings file', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} prefix
|
||||||
|
*/
|
||||||
|
function removeOldBackups(prefix) {
|
||||||
|
const MAX_BACKUPS = 25;
|
||||||
|
|
||||||
|
let files = fs.readdirSync(DIRECTORIES.backups).filter(f => f.startsWith(prefix));
|
||||||
if (files.length > MAX_BACKUPS) {
|
if (files.length > MAX_BACKUPS) {
|
||||||
files = files.map(f => path.join(DIRECTORIES.backups, f));
|
files = files.map(f => path.join(DIRECTORIES.backups, f));
|
||||||
files.sort((a, b) => fs.statSync(a).mtimeMs - fs.statSync(b).mtimeMs);
|
files.sort((a, b) => fs.statSync(a).mtimeMs - fs.statSync(b).mtimeMs);
|
||||||
|
|
||||||
fs.rmSync(files[0]);
|
fs.rmSync(files[0]);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
|
||||||
console.log('Could not backup settings file', err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function ensurePublicDirectoriesExist() {
|
function ensurePublicDirectoriesExist() {
|
||||||
|
@ -77,7 +77,7 @@ function registerEndpoints(app, jsonParser) {
|
|||||||
for (let file of fs.readdirSync(live2d_model_path)) {
|
for (let file of fs.readdirSync(live2d_model_path)) {
|
||||||
if (file.includes("model")) {
|
if (file.includes("model")) {
|
||||||
//console.debug("Asset live2d model found:",file)
|
//console.debug("Asset live2d model found:",file)
|
||||||
output[folder].push([`${model_folder}`,path.join("assets", folder, model_folder, file)]);
|
output[folder].push([`${model_folder}`, path.join("assets", folder, model_folder, file)]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -257,7 +257,7 @@ function registerEndpoints(app, jsonParser) {
|
|||||||
for (let file of fs.readdirSync(live2dModelPath)) {
|
for (let file of fs.readdirSync(live2dModelPath)) {
|
||||||
//console.debug("Character live2d model found:", file)
|
//console.debug("Character live2d model found:", file)
|
||||||
if (file.includes("model"))
|
if (file.includes("model"))
|
||||||
output.push([`${modelFolder}`,path.join("characters", name, category, modelFolder, file) ]);
|
output.push([`${modelFolder}`, path.join("characters", name, category, modelFolder, file)]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user