From a51cd5cb7d8a1573db577f39be51837da7ec6915 Mon Sep 17 00:00:00 2001
From: VIAUD Nathan <nathan.viaud@inria.fr>
Date: Thu, 7 Dec 2023 10:05:07 +0000
Subject: [PATCH] fix: normalize assets between posix & windows

---
 electron/components/file.js                     |  5 ++---
 electron/components/main.js                     | 17 ++++++++++++++---
 .../forms/components/inputs/FileInput.vue       |  6 +++++-
 3 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/electron/components/file.js b/electron/components/file.js
index b1bb2397..f6bdb049 100644
--- a/electron/components/file.js
+++ b/electron/components/file.js
@@ -261,7 +261,7 @@ const copyFileToWorkdir = async function (workdir, filepath, isIcon) {
     const copyPath = path.join(assetsPath, path.basename(filepath).replace(/[^a-z0-9.]/gi, '_'));
     if (!fs.existsSync(assetsPath)) fs.mkdirSync(assetsPath);
     fs.copyFileSync(filepath, copyPath);
-    return path.relative(workdir, copyPath);
+    return path.relative(workdir, copyPath).replaceAll('\\', '/');
 };
 
 /**
@@ -345,7 +345,7 @@ const getUsedAssets = function (workdir) {
 
     if(!matches) return [];
     return matches.map(match => {
-        return match.replace(`assets${path.sep}`, '')
+        return match.replace('assets/', '')
             .replaceAll('"', '')
             // To only keep the slash after icons
             .replace(/\/+/g, '/')
@@ -363,7 +363,6 @@ const getUnusedAssets = function (workdir) {
             unusedAssets.push(asset);
         }
     }
-    console.assert(unusedAssets.length === 0, 'parasite asset detected', unusedAssets);
 
     return unusedAssets;
 };
diff --git a/electron/components/main.js b/electron/components/main.js
index 0f2deb1b..6599fb94 100644
--- a/electron/components/main.js
+++ b/electron/components/main.js
@@ -60,15 +60,26 @@ module.exports.setupWindow = function (window) {
         console.error('Failed to register protocol:', error);
     }
 
+    const windowsUrl= [
+        `file:///${encodeURI(path.join(__dirname, '../../dist/assets/').replaceAll('\\', '/'))}*`,
+        `file:///${encodeURI(path.join(__dirname, '../../dist/images/').replaceAll('\\', '/'))}*`,
+        `file:///${encodeURI(path.join(__dirname, '../../dist/videos/').replaceAll('\\', '/'))}*`,
+    ];
+
+    const posixUrl = [
+        `file://${encodeURI(path.join(__dirname, '../../dist/assets/'))}*`,
+        `file://${encodeURI(path.join(__dirname, '../../dist/images/'))}*`,
+        `file://${encodeURI(path.join(__dirname, '../../dist/videos/'))}*`,
+    ];
+
     // Intercept all url starting with assets/ and redirect it to custom protocol (wysiwyg/quill)
     const filter = {
         urls: [
             'http://localhost:8000/assets/*',
             'http://localhost:8000/images/*',
             'http://localhost:8000/videos/*',
-            `file://${encodeURI(path.join(__dirname, '../../dist/assets/'))}*`,
-            `file://${encodeURI(path.join(__dirname, '../../dist/images/'))}*`,
-            `file://${encodeURI(path.join(__dirname, '../../dist/videos/'))}*`
+
+            ...(process.platform === 'win32' ? windowsUrl : posixUrl)
         ]
     };
     
diff --git a/src/features/forms/components/inputs/FileInput.vue b/src/features/forms/components/inputs/FileInput.vue
index cc06259d..c20a68b1 100644
--- a/src/features/forms/components/inputs/FileInput.vue
+++ b/src/features/forms/components/inputs/FileInput.vue
@@ -26,7 +26,6 @@ const filetype = computed(() => {
     return null;
 });
 
-//! This function have to fetch the image from the back
 async function changeImage(e: Event) {
 
     savedState = getCurrentState(true);
@@ -56,6 +55,11 @@ function openFile() {
 
 onMounted(() => {
     url.value = props.inputValue;
+    if(url.value.includes('\\')) {
+        //? Backwards support if files contains backslashes (not possible anymore)
+        // TODO: Remove this in the future
+        emit('input', url.value.replaceAll('\\', '/'));
+    }
 });
 
 watch(
-- 
GitLab