From 3822ae9356dd3dcf80eb8f9c41ed061cd3fb53dc Mon Sep 17 00:00:00 2001 From: Isaac McFadyen Date: Thu, 18 Apr 2024 15:50:27 -0400 Subject: [PATCH 1/2] Switched fs.renameSync to fs.copyFileSync --- post-install.js | 3 ++- src/endpoints/assets.js | 3 ++- src/endpoints/backgrounds.js | 6 ++++-- src/endpoints/characters.js | 3 ++- src/endpoints/chats.js | 3 ++- src/users.js | 6 ++++-- 6 files changed, 16 insertions(+), 8 deletions(-) diff --git a/post-install.js b/post-install.js index 645085a8a..f787eeba3 100644 --- a/post-install.js +++ b/post-install.js @@ -60,7 +60,8 @@ function convertConfig() { try { console.log(color.blue('Converting config.conf to config.yaml. Your old config.conf will be renamed to config.conf.bak')); const config = require(path.join(process.cwd(), './config.conf')); - fs.renameSync('./config.conf', './config.conf.bak'); + fs.copyFileSync('./config.conf', './config.conf.bak'); + fs.rmSync('./config.conf'); fs.writeFileSync('./config.yaml', yaml.stringify(config)); console.log(color.green('Conversion successful. Please check your config.yaml and fix it if necessary.')); } catch (error) { diff --git a/src/endpoints/assets.js b/src/endpoints/assets.js index a9dc317d6..78f5270a7 100644 --- a/src/endpoints/assets.js +++ b/src/endpoints/assets.js @@ -227,7 +227,8 @@ router.post('/download', jsonParser, async (request, response) => { // Move into asset place console.debug('Download finished, moving file from', temp_path, 'to', file_path); - fs.renameSync(temp_path, file_path); + fs.copyFileSync(temp_path, file_path); + fs.rmSync(temp_path); response.sendStatus(200); } catch (error) { diff --git a/src/endpoints/backgrounds.js b/src/endpoints/backgrounds.js index 33419ef4f..b8965ab5f 100644 --- a/src/endpoints/backgrounds.js +++ b/src/endpoints/backgrounds.js @@ -51,7 +51,8 @@ router.post('/rename', jsonParser, function (request, response) { return response.sendStatus(400); } - fs.renameSync(oldFileName, newFileName); + fs.copyFileSync(oldFileName, newFileName); + fs.rmSync(oldFileName); invalidateThumbnail(request.user.directories, 'bg', request.body.old_bg); return response.send('ok'); }); @@ -63,7 +64,8 @@ router.post('/upload', urlencodedParser, function (request, response) { const filename = request.file.originalname; try { - fs.renameSync(img_path, path.join(request.user.directories.backgrounds, filename)); + fs.copyFileSync(img_path, path.join(request.user.directories.backgrounds, filename)); + fs.rmSync(img_path); invalidateThumbnail(request.user.directories, 'bg', filename); response.send(filename); } catch (err) { diff --git a/src/endpoints/characters.js b/src/endpoints/characters.js index f9ff18688..3fa022a8e 100644 --- a/src/endpoints/characters.js +++ b/src/endpoints/characters.js @@ -680,7 +680,8 @@ router.post('/rename', jsonParser, async function (request, response) { // Rename chats folder if (fs.existsSync(oldChatsPath) && !fs.existsSync(newChatsPath)) { - fs.renameSync(oldChatsPath, newChatsPath); + fs.cpSync(oldChatsPath, newChatsPath); + fs.rmSync(oldChatsPath, { recursive: true, force: true }); } // Remove the old character file diff --git a/src/endpoints/chats.js b/src/endpoints/chats.js index 49cf98e01..ff55d3ff0 100644 --- a/src/endpoints/chats.js +++ b/src/endpoints/chats.js @@ -213,8 +213,9 @@ router.post('/rename', jsonParser, async function (request, response) { return response.status(400).send({ error: true }); } + fs.copyFileSync(pathToOriginalFile, pathToRenamedFile); + fs.rmSync(pathToOriginalFile); console.log('Successfully renamed.'); - fs.renameSync(pathToOriginalFile, pathToRenamedFile); return response.send({ ok: true }); }); diff --git a/src/users.js b/src/users.js index 8781334e3..8e2394fc5 100644 --- a/src/users.js +++ b/src/users.js @@ -286,12 +286,14 @@ async function migrateUserData() { // Copy the file to the new location fs.cpSync(migration.old, migration.new, { force: true }); // Move the file to the backup location - fs.renameSync(migration.old, path.join(backupDirectory, path.basename(migration.old))); + fs.cpSync(migration.old, path.join(backupDirectory, path.basename(migration.old))); + fs.rmSync(migration.old, { recursive: true, force: true }); } else { // Copy the directory to the new location fs.cpSync(migration.old, migration.new, { recursive: true, force: true }); // Move the directory to the backup location - fs.renameSync(migration.old, path.join(backupDirectory, path.basename(migration.old))); + fs.cpSync(migration.old, path.join(backupDirectory, path.basename(migration.old))); + fs.rmSync(migration.old, { recursive: true, force: true }); } } catch (error) { console.error(color.red(`Error migrating ${migration.old} to ${migration.new}:`), error.message); From 15a8adb0b926fba8b40072ec25f01b3b87a65b96 Mon Sep 17 00:00:00 2001 From: Isaac McFadyen Date: Thu, 18 Apr 2024 16:04:04 -0400 Subject: [PATCH 2/2] Changed fs.cpSync to use recursive copying --- src/endpoints/characters.js | 2 +- src/users.js | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/endpoints/characters.js b/src/endpoints/characters.js index 3fa022a8e..556a8fecc 100644 --- a/src/endpoints/characters.js +++ b/src/endpoints/characters.js @@ -680,7 +680,7 @@ router.post('/rename', jsonParser, async function (request, response) { // Rename chats folder if (fs.existsSync(oldChatsPath) && !fs.existsSync(newChatsPath)) { - fs.cpSync(oldChatsPath, newChatsPath); + fs.cpSync(oldChatsPath, newChatsPath, { recursive: true }); fs.rmSync(oldChatsPath, { recursive: true, force: true }); } diff --git a/src/users.js b/src/users.js index 8e2394fc5..831ff09d5 100644 --- a/src/users.js +++ b/src/users.js @@ -286,13 +286,21 @@ async function migrateUserData() { // Copy the file to the new location fs.cpSync(migration.old, migration.new, { force: true }); // Move the file to the backup location - fs.cpSync(migration.old, path.join(backupDirectory, path.basename(migration.old))); + fs.cpSync( + migration.old, + path.join(backupDirectory, path.basename(migration.old)), + { recursive: true, force: true } + ); fs.rmSync(migration.old, { recursive: true, force: true }); } else { // Copy the directory to the new location fs.cpSync(migration.old, migration.new, { recursive: true, force: true }); // Move the directory to the backup location - fs.cpSync(migration.old, path.join(backupDirectory, path.basename(migration.old))); + fs.cpSync( + migration.old, + path.join(backupDirectory, path.basename(migration.old)), + { recursive: true, force: true } + ); fs.rmSync(migration.old, { recursive: true, force: true }); } } catch (error) {