Make file write operations sync

This commit is contained in:
Cohee
2023-08-18 12:11:18 +03:00
parent 86a486be8f
commit 52431e246a

View File

@@ -69,7 +69,6 @@ app.use(responseTime());
const fs = require('fs'); const fs = require('fs');
const writeFileAtomicSync = require('write-file-atomic').sync; const writeFileAtomicSync = require('write-file-atomic').sync;
const writeFileAtomic = require('write-file-atomic');
const readline = require('readline'); const readline = require('readline');
const open = require('open'); const open = require('open');
@@ -1430,18 +1429,16 @@ app.post('/deleteuseravatar', jsonParser, function (request, response) {
}); });
app.post("/setbackground", jsonParser, function (request, response) { app.post("/setbackground", jsonParser, function (request, response) {
var bg = "#bg1 {background-image: url('../backgrounds/" + request.body.bg + "');}"; try {
writeFileAtomic('public/css/bg_load.css', bg, 'utf8', function (err) { const bg = `#bg1 {background-image: url('../backgrounds/${request.body.bg}');}`;
if (err) { writeFileAtomicSync('public/css/bg_load.css', bg, 'utf8');
response.send(err); response.send({ result: 'ok' });
return console.log(err); } catch (err) {
} else { console.log(err);
//response.redirect("/"); response.send(err);
response.send({ result: 'ok' }); }
}
});
}); });
app.post("/delbackground", jsonParser, function (request, response) { app.post("/delbackground", jsonParser, function (request, response) {
if (!request.body) return response.sendStatus(400); if (!request.body) return response.sendStatus(400);
@@ -1532,14 +1529,13 @@ app.post("/downloadbackground", urlencodedParser, function (request, response) {
}); });
app.post("/savesettings", jsonParser, function (request, response) { app.post("/savesettings", jsonParser, function (request, response) {
writeFileAtomic('public/settings.json', JSON.stringify(request.body, null, 4), 'utf8', function (err) { try {
if (err) { writeFileAtomicSync('public/settings.json', JSON.stringify(request.body, null, 4), 'utf8');
response.send(err); response.send({ result: "ok" });
console.log(err); } catch (err) {
} else { console.log(err);
response.send({ result: "ok" }); response.send(err);
} }
});
}); });
function getCharaCardV2(jsonObject) { function getCharaCardV2(jsonObject) {
@@ -2403,13 +2399,17 @@ app.post("/importchat", urlencodedParser, function (request, response) {
}); });
const errors = []; const errors = [];
newChats.forEach(chat => writeFileAtomic(
`${chatsPath + avatar_url}/${ch_name} - ${humanizedISO8601DateTime()} imported.jsonl`, for (const chat of newChats) {
chat.map(tryParse).filter(x => x).join('\n'), const filePath = `${chatsPath + avatar_url}/${ch_name} - ${humanizedISO8601DateTime()} imported.jsonl`;
'utf8', const fileContent = chat.map(tryParse).filter(x => x).join('\n');
(err) => err ?? errors.push(err)
) try {
); writeFileAtomicSync(filePath, fileContent, 'utf8');
} catch (err) {
errors.push(err);
}
}
if (0 < errors.length) { if (0 < errors.length) {
response.send('Errors occurred while writing character files. Errors: ' + JSON.stringify(errors)); response.send('Errors occurred while writing character files. Errors: ' + JSON.stringify(errors));