Quellcode durchsuchen

[frontent] add support for % in file and folder names (#2981)

* [frontent] add support for % in file and folder names

* [frontend] nit: Remove space
Bjorn Alm vor 3 Jahren
Ursprung
Commit
fd1960d4df

+ 3 - 1
apps/filebrowser/src/filebrowser/templates/display.mako

@@ -400,7 +400,9 @@ ${ fb_components.menubar() }
       // split the path on the first occurence and the remaining string will not
       // be part of the path. Question marks must also be encoded or the string after the first
       // question mark will be interpreted as the url querystring.
-      const partiallyEncodedFilePath = correctedFilePath.replaceAll('#', encodeURIComponent('#'))
+      // Percentage character must be double encoded due to the page library that decodes the url twice
+      const doubleUrlEncodedPercentage = '%2525';
+      const partiallyEncodedFilePath = correctedFilePath.replaceAll('%', doubleUrlEncodedPercentage).replaceAll('#', encodeURIComponent('#'))
         .replaceAll('?', encodeURIComponent('?'));
       const baseUrl = "${url('filebrowser:filebrowser_views_download', path='')}";
       const fullUrl = baseUrl+partiallyEncodedFilePath;

+ 21 - 4
apps/filebrowser/src/filebrowser/templates/listdir_components.mako

@@ -1367,6 +1367,8 @@ else:
       }
 
       self.viewFile = function (file, e) {
+        const urlEncodedPercentage = '%25';
+        
         e.stopImmediatePropagation();
         if (file.type == "dir") {
           // Reset page number so that we don't hit a page that doesn't exist
@@ -1374,14 +1376,27 @@ else:
           self.enableFilterAfterSearch = false;
           self.searchQuery("");
           self.targetPath(file.url);
-          updateHash(stripHashes(file.path));
+
+          const path = file.path;
+          const percentageEncodedPath = path.includes('%') && !path.includes(urlEncodedPercentage) ? 
+            path.replaceAll('%', urlEncodedPercentage) : path;
+        
+          updateHash(stripHashes(percentageEncodedPath));
         } else {
           
           // Fix to encode potential hash characters in a file name when viewing a file
-          const url = file.name.indexOf('#') >= 0 && file.url.indexOf('#') >= 0 
+          let url = file.name.indexOf('#') >= 0 && file.url.indexOf('#') >= 0 
             ? file.url.replaceAll('#', encodeURIComponent('#')) : file.url;
 
           %if is_embeddable:
+
+          // Fix. The '%' character needs to be encoded twice due to a bug in the page library
+          // that decodes the url twice
+          
+          if (file.path.includes('%') && !file.path.includes(urlEncodedPercentage)) {            
+            url = url.replaceAll(urlEncodedPercentage, encodeURIComponent(urlEncodedPercentage));
+          }
+
           huePubSub.publish('open.link', url);
           %else:
           window.location.href = url;
@@ -1398,8 +1413,10 @@ else:
       // If the hash characters aren't encoded the page library will
       // split the path on the first occurence and the remaining string will not
       // be part of the path. Question marks must also be encoded or the string after the first
-      // question mark will be interpreted as the url querystring.      
-        const partiallyEncodedFilePath = self.selectedFile().path.replaceAll('#', encodeURIComponent('#'))
+      // question mark will be interpreted as the url querystring.
+      // Percentage character must be double encoded due to the page library that decodes the url twice
+        const doubleUrlEncodedPercentage = '%2525';
+        const partiallyEncodedFilePath = self.selectedFile().path.replaceAll('%', doubleUrlEncodedPercentage).replaceAll('#', encodeURIComponent('#'))
           .replaceAll('?', encodeURIComponent('?'));;
         const fullUrl = baseUrl+partiallyEncodedFilePath;