From 39d771cc4a17c989ad81cd5debb0296cf11d125d Mon Sep 17 00:00:00 2001 From: valadaptive Date: Tue, 5 Dec 2023 17:55:52 -0500 Subject: [PATCH] Replace path "\" with "/" server-side --- public/scripts/chats.js | 2 +- public/scripts/utils.js | 2 +- server.js | 3 ++- src/endpoints/assets.js | 4 ++-- src/endpoints/files.js | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/public/scripts/chats.js b/public/scripts/chats.js index a53b8c712..85c9e75c1 100644 --- a/public/scripts/chats.js +++ b/public/scripts/chats.js @@ -167,7 +167,7 @@ export async function uploadFileAttachment(fileName, base64Data) { } const responseData = await result.json(); - return responseData.path.replace(/\\/g, '/'); + return responseData.path; } catch (error) { toastr.error(String(error), 'Could not upload file'); console.error('Could not upload file', error); diff --git a/public/scripts/utils.js b/public/scripts/utils.js index 371f1aa57..7699263ba 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -950,7 +950,7 @@ export async function saveBase64AsFile(base64Data, characterName, filename = '', // If the response is successful, get the saved image path from the server's response if (response.ok) { const responseData = await response.json(); - return responseData.path.replace(/\\/g, '/'); // Replace backslashes with forward slashes + return responseData.path; } else { const errorData = await response.json(); throw new Error(errorData.error || 'Failed to upload the image to the server'); diff --git a/server.js b/server.js index 17ed81b7f..0103be08f 100644 --- a/server.js +++ b/server.js @@ -1588,7 +1588,8 @@ app.post('/uploadimage', jsonParser, async (request, response) => { const imageBuffer = Buffer.from(base64Data, 'base64'); await fs.promises.writeFile(pathToNewFile, imageBuffer); // send the path to the image, relative to the client folder, which means removing the first folder from the path which is 'public' - pathToNewFile = pathToNewFile.split(path.sep).slice(1).join(path.sep); + // Use forward slashes, even on Windows + pathToNewFile = pathToNewFile.split(path.sep).slice(1).join('/'); response.send({ path: pathToNewFile }); } catch (error) { console.log(error); diff --git a/src/endpoints/assets.js b/src/endpoints/assets.js index 17f277fad..64a75011f 100644 --- a/src/endpoints/assets.js +++ b/src/endpoints/assets.js @@ -102,7 +102,7 @@ router.post('/get', jsonParser, async (_, response) => { file = path.normalize(file.replace('public' + path.sep, '')); if (file.includes('model') && file.endsWith('.json')) { //console.debug("Asset live2d model found:",file) - output[folder].push(path.normalize(path.join(file))); + output[folder].push(path.normalize(file).replace(/\\/g, '/')); } } continue; @@ -115,7 +115,7 @@ router.post('/get', jsonParser, async (_, response) => { }); output[folder] = []; for (const file of files) { - output[folder].push(path.join('assets', folder, file)); + output[folder].push(path.join('assets', folder, file).replace(/\\/g, '/')); } } } diff --git a/src/endpoints/files.js b/src/endpoints/files.js index d5381bad4..4e0b106f1 100644 --- a/src/endpoints/files.js +++ b/src/endpoints/files.js @@ -23,7 +23,7 @@ router.post('/upload', jsonParser, async (request, response) => { const pathToUpload = path.join(DIRECTORIES.files, request.body.name); writeFileSyncAtomic(pathToUpload, request.body.data, 'base64'); - const url = path.normalize(pathToUpload.replace('public' + path.sep, '')); + const url = path.normalize(pathToUpload.replace('public' + path.sep, '').replace(/\\/g, '/')); return response.send({ path: url }); } catch (error) { console.log(error);