Add try-catch to rename chat endpoint

363b07ab0bcae1e5a5d62979fa6a4659f72c4b2d

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +26 -21Ignore whitespace
src/endpoints/chats.js+26 -21
@@ -69,7 +69,7 @@ function getBackupFunction(handle) {
6969 if (!backupFunctions.has(handle)) {
7070 backupFunctions.set(handle, _.throttle(backupChat, throttleInterval, { leading: true, trailing: true }));
7171 }
7272 return backupFunctions.get(handle) || (() => { });
7373}
7474
7575/**
@@ -487,28 +487,33 @@ router.post('/get', validateAvatarUrlMiddleware, function (request, response) {
487487});
488488
489489router.post('/rename', validateAvatarUrlMiddleware, async function (request, response) {
490- if (!request.body || !request.body.original_file || !request.body.renamed_file) {
490+ try {
491- return response.sendStatus(400);
491+ if (!request.body || !request.body.original_file || !request.body.renamed_file) {
492- }
492+ return response.sendStatus(400);
493+ }
493494
494495 const pathToFolder = request.body.is_group
495496 ? request.user.directories.groupChats
496497 : path.join(request.user.directories.chats, String(request.body.avatar_url).replace('.png', ''));
497498 const pathToOriginalFile = path.join(pathToFolder, sanitize(request.body.original_file));
498499 const pathToRenamedFile = path.join(pathToFolder, sanitize(request.body.renamed_file));
499500 const sanitizedFileName = path.parse(pathToRenamedFile).name;
500501 console.infodebug('Old chat name', pathToOriginalFile);
501502 console.infodebug('New chat name', pathToRenamedFile);
503+
504+ if (!fs.existsSync(pathToOriginalFile) || fs.existsSync(pathToRenamedFile)) {
505+ console.error('Either Source or Destination files are not available');
506+ return response.status(400).send({ error: true });
507+ }
502508
503- if (!fs.existsSync(pathToOriginalFile) || fs.existsSync(pathToRenamedFile)) {
509+ fs.copyFileSync(pathToOriginalFile, pathToRenamedFile);
504- console.error('Either Source or Destination files are not available');
510+ fs.unlinkSync(pathToOriginalFile);
505- return response.status(400).send({ error: true });
511+ console.info('Successfully renamed chat file.');
512+ return response.send({ ok: true, sanitizedFileName });
513+ } catch (error) {
514+ console.error('Error renaming chat file:', error);
515+ return response.status(500).send({ error: true });
506516 }
507-
508- fs.copyFileSync(pathToOriginalFile, pathToRenamedFile);
509- fs.unlinkSync(pathToOriginalFile);
510- console.info('Successfully renamed.');
511- return response.send({ ok: true, sanitizedFileName });
512517});
513518
514519router.post('/delete', validateAvatarUrlMiddleware, function (request, response) {
@@ -859,7 +864,7 @@ router.post('/search', validateAvatarUrlMiddleware, function (request, response)
859864 // Search through title and messages of the chat
860865 const fragments = query.trim().toLowerCase().split(/\s+/).filter(x => x);
861866 const text = [path.parse(chatFile.path).name,
862867 ...messages.map(message => message?.mes)].join('\n').toLowerCase();
863868 const hasMatch = fragments.every(fragment => text.includes(fragment));
864869
865870 if (hasMatch) {