From a5207b64c2934c2cbabd0c467ae03563ffa4f8ee Mon Sep 17 00:00:00 2001 From: SillyLossy Date: Mon, 22 May 2023 11:16:02 +0300 Subject: [PATCH] #362 Fix card chat name sanitation --- public/script.js | 9 ++++- server.js | 96 +++++++++++++++++++++--------------------------- 2 files changed, 49 insertions(+), 56 deletions(-) diff --git a/public/script.js b/public/script.js index 94ed942ee..7f5eb6294 100644 --- a/public/script.js +++ b/public/script.js @@ -833,6 +833,13 @@ async function getCharacters() { characters[i] = []; characters[i] = getData[i]; characters[i]['name'] = DOMPurify.sanitize(characters[i]['name']); + + // For dropped-in cards + if (!characters[i]['chat']) { + characters[i]['chat'] = `${characters[i]['name']} - ${humanizedDateTime()}`; + } + + characters[i]['chat'] = String(characters[i]['chat']); } if (this_chid != undefined && this_chid != "invalid-safety-id") { $("#avatar_url_pole").val(characters[this_chid].avatar); @@ -1217,7 +1224,7 @@ function addOneMessage(mes, { type = "normal", insertAfter = null, scroll = true $(".mes_prompt").hide(); //console.log(itemizedPrompts); } else { - //console.log('skipping prompt data for User Message'); + //console.log('skipping prompt data for User Message'); } newMessage.find('.avatar img').on('error', function () { diff --git a/server.js b/server.js index 96522bbc4..2a40f7e60 100644 --- a/server.js +++ b/server.js @@ -515,65 +515,51 @@ app.post("/generate_textgenerationwebui", jsonParser, async function (request, r app.post("/savechat", jsonParser, function (request, response) { - var dir_name = String(request.body.avatar_url).replace('.png', ''); - let chat_data = request.body.chat; - let jsonlData = chat_data.map(JSON.stringify).join('\n'); - fs.writeFile(`${chatsPath + dir_name}/${sanitize(request.body.file_name)}.jsonl`, jsonlData, 'utf8', function (err) { - if (err) { - response.send(err); - return console.log(err); - } else { - response.send({ result: "ok" }); - } - }); - + try { + var dir_name = String(request.body.avatar_url).replace('.png', ''); + let chat_data = request.body.chat; + let jsonlData = chat_data.map(JSON.stringify).join('\n'); + fs.writeFileSync(`${chatsPath + dir_name}/${sanitize(String(request.body.file_name))}.jsonl`, jsonlData, 'utf8'); + return response.send({ result: "ok" }); + } catch (error) { + response.send(error); + return console.log(error); + } }); + app.post("/getchat", jsonParser, function (request, response) { - var dir_name = String(request.body.avatar_url).replace('.png', ''); + try { + const dirName = String(request.body.avatar_url).replace('.png', ''); + const chatDirExists = fs.existsSync(chatsPath + dirName); - fs.stat(chatsPath + dir_name, function (err, stat) { - - if (stat === undefined) { //if no chat dir for the character is found, make one with the character name - - fs.mkdirSync(chatsPath + dir_name); - response.send({}); - return; - } else { - - if (err === null) { //if there is a dir, then read the requested file from the JSON call - - fs.stat(`${chatsPath + dir_name}/${sanitize(request.body.file_name)}.jsonl`, function (err, stat) { - if (err === null) { //if no error (the file exists), read the file - if (stat !== undefined) { - fs.readFile(`${chatsPath + dir_name}/${sanitize(request.body.file_name)}.jsonl`, 'utf8', (err, data) => { - if (err) { - console.error(err); - response.send(err); - return; - } - //console.log(data); - const lines = data.split('\n'); - - // Iterate through the array of strings and parse each line as JSON - const jsonData = lines.map(tryParse).filter(x => x); - response.send(jsonData); - //console.log('read the requested file') - - }); - } - } else { - response.send({}); - //return console.log(err); - return; - } - }); - } else { - console.error(err); - response.send({}); - return; - } + //if no chat dir for the character is found, make one with the character name + if (!chatDirExists) { + fs.mkdirSync(chatsPath + dirName); + return response.send({}); } - }); + + + if (!request.body.file_name) { + return response.send({}); + } + + const fileName = `${chatsPath + dirName}/${sanitize(String(request.body.file_name))}.jsonl`; + const chatFileExists = fs.existsSync(fileName); + + if (!chatFileExists) { + return response.send({}); + } + + const data = fs.readFileSync(fileName, 'utf8'); + const lines = data.split('\n'); + + // Iterate through the array of strings and parse each line as JSON + const jsonData = lines.map(tryParse).filter(x => x); + return response.send(jsonData); + } catch (error) { + console.error(error); + return response.send({}); + } }); app.post("/getstatus", jsonParser, async function (request, response_getstatus = response) {