Skip to content

Commit 0336c92

Browse files
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.
1 parent b9edfd9 commit 0336c92

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

taipy/gui/data/content_accessor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def get_info(self, var_name: str, value: t.Any, image: bool) -> t.Union[str, t.T
113113
self.__content_paths[url_path] = dir_path
114114
file_url = f"{url_path}/{path.name}"
115115
self.__url_is_image[file_url] = image
116-
return (urllib.parse.quote_plus(file_url, safe="/"),)
116+
return (urllib.parse.quote(file_url, safe="/"),)
117117
elif _has_magic_module:
118118
try:
119119
mime = magic.from_buffer(value, mime=True)

tests/gui/control/test_file_download.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ def test_file_download_path_md(gui: Gui, test_client, helpers):
5050
helpers.test_control_md(gui, md_string, expected_list)
5151

5252

53+
def test_file_download_with_spaces_path_md(gui: Gui, test_client, helpers):
54+
resources_dir = pathlib.Path(__file__).parent.parent / "resources"
55+
test_file_path = resources_dir / "test file with spaces.txt"
56+
57+
try:
58+
with open(test_file_path, "w") as f:
59+
f.write("Test content")
60+
61+
gui._bind_var_val("content", str(test_file_path.resolve()))
62+
md_string = "<|{content}|file_download|>"
63+
expected_list = [
64+
"<FileDownload",
65+
'defaultContent="/taipy-content/taipyStatic0/test%20file%20with%20spaces.txt"',
66+
]
67+
helpers.test_control_md(gui, md_string, expected_list)
68+
finally:
69+
if test_file_path.exists():
70+
test_file_path.unlink()
71+
72+
5373
def test_file_download_any_file_md(gui: Gui, test_client, helpers):
5474
with open(os.path.abspath(__file__), "rb") as content:
5575
gui._bind_var_val("content", content.read())

0 commit comments

Comments
 (0)