瀏覽代碼

Fix URL encoding in content accessor by replacing quote_plus with quote for file URLs (#2542)

* Fix URL encoding in content accessor by replacing quote_plus with quote for file URLs

* Add test for file download handling with spaces in file path

This commit introduces a new test case to verify the functionality of file downloads when the file path contains spaces. The test creates a temporary file, sets the content variable in the GUI, and checks the generated markdown string against expected output, ensuring proper URL encoding for spaces.

* Update expected output for file download test to reflect correct file path with spaces

This commit modifies the expected output in the test for file downloads to ensure the correct URL is generated when the file path contains spaces. The change updates the defaultContent attribute to include the proper path format.
PRANSHU GUPTA 1 月之前
父節點
當前提交
0336c92137
共有 2 個文件被更改,包括 21 次插入1 次删除
  1. 1 1
      taipy/gui/data/content_accessor.py
  2. 20 0
      tests/gui/control/test_file_download.py

+ 1 - 1
taipy/gui/data/content_accessor.py

@@ -113,7 +113,7 @@ class _ContentAccessor:
             self.__content_paths[url_path] = dir_path
             self.__content_paths[url_path] = dir_path
             file_url = f"{url_path}/{path.name}"
             file_url = f"{url_path}/{path.name}"
             self.__url_is_image[file_url] = image
             self.__url_is_image[file_url] = image
-            return (urllib.parse.quote_plus(file_url, safe="/"),)
+            return (urllib.parse.quote(file_url, safe="/"),)
         elif _has_magic_module:
         elif _has_magic_module:
             try:
             try:
                 mime = magic.from_buffer(value, mime=True)
                 mime = magic.from_buffer(value, mime=True)

+ 20 - 0
tests/gui/control/test_file_download.py

@@ -50,6 +50,26 @@ def test_file_download_path_md(gui: Gui, test_client, helpers):
     helpers.test_control_md(gui, md_string, expected_list)
     helpers.test_control_md(gui, md_string, expected_list)
 
 
 
 
+def test_file_download_with_spaces_path_md(gui: Gui, test_client, helpers):
+    resources_dir = pathlib.Path(__file__).parent.parent / "resources"
+    test_file_path = resources_dir / "test file with spaces.txt"
+
+    try:
+        with open(test_file_path, "w") as f:
+            f.write("Test content")
+
+        gui._bind_var_val("content", str(test_file_path.resolve()))
+        md_string = "<|{content}|file_download|>"
+        expected_list = [
+            "<FileDownload",
+            'defaultContent="/taipy-content/taipyStatic0/test%20file%20with%20spaces.txt"',
+        ]
+        helpers.test_control_md(gui, md_string, expected_list)
+    finally:
+        if test_file_path.exists():
+            test_file_path.unlink()
+
+
 def test_file_download_any_file_md(gui: Gui, test_client, helpers):
 def test_file_download_any_file_md(gui: Gui, test_client, helpers):
     with open(os.path.abspath(__file__), "rb") as content:
     with open(os.path.abspath(__file__), "rb") as content:
         gui._bind_var_val("content", content.read())
         gui._bind_var_val("content", content.read())