mirror of
				https://github.com/SillyTavern/SillyTavern.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	#362 Fix card chat name sanitation
This commit is contained in:
		| @@ -833,6 +833,13 @@ async function getCharacters() { | |||||||
|             characters[i] = []; |             characters[i] = []; | ||||||
|             characters[i] = getData[i]; |             characters[i] = getData[i]; | ||||||
|             characters[i]['name'] = DOMPurify.sanitize(characters[i]['name']); |             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") { |         if (this_chid != undefined && this_chid != "invalid-safety-id") { | ||||||
|             $("#avatar_url_pole").val(characters[this_chid].avatar); |             $("#avatar_url_pole").val(characters[this_chid].avatar); | ||||||
|   | |||||||
							
								
								
									
										78
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										78
									
								
								server.js
									
									
									
									
									
								
							| @@ -515,65 +515,51 @@ app.post("/generate_textgenerationwebui", jsonParser, async function (request, r | |||||||
|  |  | ||||||
|  |  | ||||||
| app.post("/savechat", jsonParser, function (request, response) { | app.post("/savechat", jsonParser, function (request, response) { | ||||||
|  |     try { | ||||||
|         var dir_name = String(request.body.avatar_url).replace('.png', ''); |         var dir_name = String(request.body.avatar_url).replace('.png', ''); | ||||||
|         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'); | ||||||
|     fs.writeFile(`${chatsPath + dir_name}/${sanitize(request.body.file_name)}.jsonl`, jsonlData, 'utf8', function (err) { |         fs.writeFileSync(`${chatsPath + dir_name}/${sanitize(String(request.body.file_name))}.jsonl`, jsonlData, 'utf8'); | ||||||
|         if (err) { |         return response.send({ result: "ok" }); | ||||||
|             response.send(err); |     } catch (error) { | ||||||
|             return console.log(err); |         response.send(error); | ||||||
|         } else { |         return console.log(error); | ||||||
|             response.send({ result: "ok" }); |  | ||||||
|     } |     } | ||||||
|     }); |  | ||||||
|  |  | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.post("/getchat", jsonParser, function (request, response) { | 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 no chat dir for the character is found, make one with the character name | ||||||
|  |         if (!chatDirExists) { | ||||||
|         if (stat === undefined) {		//if no chat dir for the character is found, make one with the character name |             fs.mkdirSync(chatsPath + dirName); | ||||||
|  |             return response.send({}); | ||||||
|             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); |  | ||||||
|  |  | ||||||
|  |         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'); |         const lines = data.split('\n'); | ||||||
|  |  | ||||||
|         // Iterate through the array of strings and parse each line as JSON |         // Iterate through the array of strings and parse each line as JSON | ||||||
|         const jsonData = lines.map(tryParse).filter(x => x); |         const jsonData = lines.map(tryParse).filter(x => x); | ||||||
|                                 response.send(jsonData); |         return response.send(jsonData); | ||||||
|                                 //console.log('read the requested file') |     } catch (error) { | ||||||
|  |         console.error(error); | ||||||
|                             }); |         return response.send({}); | ||||||
|     } |     } | ||||||
|                     } else { |  | ||||||
|                         response.send({}); |  | ||||||
|                         //return console.log(err); |  | ||||||
|                         return; |  | ||||||
|                     } |  | ||||||
|                 }); |  | ||||||
|             } else { |  | ||||||
|                 console.error(err); |  | ||||||
|                 response.send({}); |  | ||||||
|                 return; |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
| }); | }); | ||||||
|  |  | ||||||
| app.post("/getstatus", jsonParser, async function (request, response_getstatus = response) { | app.post("/getstatus", jsonParser, async function (request, response_getstatus = response) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user