Promisify getallchatsofcharacter
This commit is contained in:
parent
15c81749b8
commit
6f0f420063
69
server.js
69
server.js
|
@ -1829,7 +1829,7 @@ app.post("/getallchatsofcharacter", jsonParser, function (request, response) {
|
||||||
if (!request.body) return response.sendStatus(400);
|
if (!request.body) return response.sendStatus(400);
|
||||||
|
|
||||||
var char_dir = (request.body.avatar_url).replace('.png', '')
|
var char_dir = (request.body.avatar_url).replace('.png', '')
|
||||||
fs.readdir(chatsPath + char_dir, (err, files) => {
|
fs.readdir(chatsPath + char_dir, async (err, files) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log('found error in history loading');
|
console.log('found error in history loading');
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
@ -1843,20 +1843,20 @@ app.post("/getallchatsofcharacter", jsonParser, function (request, response) {
|
||||||
// sort the files by name
|
// sort the files by name
|
||||||
//jsonFiles.sort().reverse();
|
//jsonFiles.sort().reverse();
|
||||||
// print the sorted file names
|
// print the sorted file names
|
||||||
var chatData = {};
|
let ii = jsonFiles.length; //this is the number of files belonging to the character
|
||||||
let ii = jsonFiles.length; //this is the number of files belonging to the character
|
if (ii === 0) {
|
||||||
if (ii !== 0) {
|
response.send({ error: true });
|
||||||
//console.log('found '+ii+' chat logs to load');
|
return;
|
||||||
for (let i = jsonFiles.length - 1; i >= 0; i--) {
|
}
|
||||||
const file = jsonFiles[i];
|
|
||||||
|
const jsonFilesPromise = jsonFiles.map((file) => {
|
||||||
|
return new Promise(async (res) => {
|
||||||
const fileStream = fs.createReadStream(chatsPath + char_dir + '/' + file);
|
const fileStream = fs.createReadStream(chatsPath + char_dir + '/' + file);
|
||||||
|
|
||||||
const fullPathAndFile = chatsPath + char_dir + '/' + file
|
const fullPathAndFile = chatsPath + char_dir + '/' + file
|
||||||
const stats = fs.statSync(fullPathAndFile);
|
const stats = fs.statSync(fullPathAndFile);
|
||||||
const fileSizeInKB = (stats.size / 1024).toFixed(2) + "kb";
|
const fileSizeInKB = (stats.size / 1024).toFixed(2) + "kb";
|
||||||
|
|
||||||
//console.log(fileSizeInKB);
|
|
||||||
|
|
||||||
const rl = readline.createInterface({
|
const rl = readline.createInterface({
|
||||||
input: fileStream,
|
input: fileStream,
|
||||||
crlfDelay: Infinity
|
crlfDelay: Infinity
|
||||||
|
@ -1869,33 +1869,42 @@ app.post("/getallchatsofcharacter", jsonParser, function (request, response) {
|
||||||
lastLine = line;
|
lastLine = line;
|
||||||
});
|
});
|
||||||
rl.on('close', () => {
|
rl.on('close', () => {
|
||||||
ii--;
|
rl.close();
|
||||||
if (lastLine) {
|
|
||||||
|
|
||||||
|
if (lastLine) {
|
||||||
let jsonData = tryParse(lastLine);
|
let jsonData = tryParse(lastLine);
|
||||||
if (jsonData && (jsonData.name !== undefined || jsonData.character_name !== undefined)) {
|
if (
|
||||||
chatData[i] = {};
|
jsonData &&
|
||||||
chatData[i]['file_name'] = file;
|
(jsonData.name !== undefined ||
|
||||||
chatData[i]['file_size'] = fileSizeInKB;
|
jsonData.character_name !== undefined)
|
||||||
chatData[i]['chat_items'] = itemCounter - 1;
|
) {
|
||||||
chatData[i]['mes'] = jsonData['mes'] || '[The chat is empty]';
|
const chatData = {};
|
||||||
chatData[i]['last_mes'] = jsonData['send_date'] || Date.now();
|
|
||||||
|
chatData['file_name'] = file;
|
||||||
|
chatData['file_size'] = fileSizeInKB;
|
||||||
|
chatData['chat_items'] = itemCounter - 1;
|
||||||
|
chatData['mes'] =
|
||||||
|
jsonData['mes'] || '[The chat is empty]';
|
||||||
|
chatData['last_mes'] =
|
||||||
|
jsonData['send_date'] || Date.now();
|
||||||
|
|
||||||
|
res(chatData);
|
||||||
} else {
|
} else {
|
||||||
console.log('Found an invalid or corrupted chat file: ' + fullPathAndFile);
|
console.log(
|
||||||
|
'Found an invalid or corrupted chat file: ' +
|
||||||
|
fullPathAndFile
|
||||||
|
);
|
||||||
|
|
||||||
|
res({});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ii === 0) {
|
|
||||||
//console.log('ii count went to zero, responding with chatData');
|
|
||||||
response.send(chatData);
|
|
||||||
}
|
|
||||||
//console.log('successfully closing getallchatsofcharacter');
|
|
||||||
rl.close();
|
|
||||||
});
|
});
|
||||||
};
|
});
|
||||||
} else {
|
});
|
||||||
//console.log('Found No Chats. Exiting Load Routine.');
|
|
||||||
response.send({ error: true });
|
const chatData = await Promise.all(jsonFilesPromise);
|
||||||
};
|
|
||||||
|
response.send(chatData);
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue