Fixed archiving (linereader was broken)

Co-Authored-By: Camdan M <camthehelpdesk@gmail.com>
This commit is contained in:
Isaac (eartharoid) 2020-11-13 19:50:11 +00:00
parent f8dc04d991
commit c3f1532bba
3 changed files with 42 additions and 27 deletions

5
package-lock.json generated
View File

@ -1642,6 +1642,11 @@
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"n-readlines": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/n-readlines/-/n-readlines-1.0.1.tgz",
"integrity": "sha512-z4SyAIVgMy7CkgsoNw7YVz40v0g4+WWvvqy8+ZdHrCtgevcEO758WQyrYcw3XPxcLxF+//RszTz/rO48nzD0wQ=="
},
"natural-compare": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",

View File

@ -11,6 +11,7 @@
"dotenv": "^8.2.0",
"leekslazylogger": "^2.0.7",
"line-reader": "^0.4.0",
"n-readlines": "^1.0.1",
"node-fetch": "^2.6.1",
"sequelize": "^6.3.5",
"terminal-link": "^2.1.1"

View File

@ -9,7 +9,7 @@
const Logger = require('leekslazylogger');
const log = new Logger();
const lineReader = require('line-reader');
const Readlines = require('n-readlines');
const fs = require('fs');
const { join } = require('path');
const dtf = require('@eartharoid/dtf');
@ -117,41 +117,50 @@ module.exports.export = (Ticket, channel) => new Promise((resolve, reject) => {
};
data.messages = [];
let line,
lineNumber = 0;
lineReader.eachLine(raw, line => {
let message = JSON.parse(line);
const lineByLine = new Readlines(join(__dirname, raw));
// eslint-disable-next-line no-cond-assign
while (line = lineByLine.next()) {
let message = JSON.parse(line.toString('ascii'));
let index = data.messages.findIndex(m => m.id === message.id);
if (index === -1) data.messages.push(message);
else data.messages[index] = message;
}, () => {
let endpoint = config.transcripts.web.server;
lineNumber++;
}
if (endpoint[endpoint.length - 1] === '/') endpoint = endpoint.slice(0, -1);
data.ticket.messages = lineNumber;
endpoint += `/${data.ticket.creator}/${data.ticket.channel}/?key=${process.env.ARCHIVES_KEY}`;
let endpoint = config.transcripts.web.server;
fetch(encodeURI(endpoint), {
method: 'post',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' },
})
.then(res => res.json())
.then(res => {
if (res.status !== 200) {
log.warn(res);
return resolve(new Error(`${res.status} (${res.message})`));
}
if (endpoint[endpoint.length - 1] === '/') endpoint = endpoint.slice(0, -1);
log.success(`Uploaded ticket #${ticket.id} archive to server`);
endpoint += `/${data.ticket.creator}/${data.ticket.channel}/?key=${process.env.ARCHIVES_KEY}`;
fs.unlinkSync(join(__dirname, raw));
fs.unlinkSync(join(__dirname, json));
fetch(encodeURI(endpoint), {
method: 'post',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' },
})
.then(res => res.json())
.then(res => {
if (res.status !== 200) {
log.warn(res);
return resolve(new Error(`${res.status} (${res.message})`));
}
resolve(res.url);
}).catch(e => {
log.warn(e);
return resolve(e);
});
});
log.success(`Uploaded ticket #${ticket.id} archive to server`);
fs.unlinkSync(join(__dirname, raw));
fs.unlinkSync(join(__dirname, json));
resolve(res.url);
}).catch(e => {
log.warn(e);
return resolve(e);
});
})();
});