Skip to content

Commit d2247c1

Browse files
authored
Normalize path returned by /userdata to always use / as separator (#4906)
1 parent cb12ad7 commit d2247c1

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

app/user_manager.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ async def post_users(request):
120120
async def listuserdata(request):
121121
directory = request.rel_url.query.get('dir', '')
122122
if not directory:
123-
return web.Response(status=400)
123+
return web.Response(status=400, text="Directory not provided")
124124

125125
path = self.get_request_user_filepath(request, directory)
126126
if not path:
127-
return web.Response(status=403)
127+
return web.Response(status=403, text="Invalid directory")
128128

129129
if not os.path.exists(path):
130-
return web.Response(status=404)
130+
return web.Response(status=404, text="Directory not found")
131131

132132
recurse = request.rel_url.query.get('recurse', '').lower() == "true"
133133
full_info = request.rel_url.query.get('full_info', '').lower() == "true"
@@ -143,17 +143,21 @@ async def listuserdata(request):
143143
if full_info:
144144
results = [
145145
{
146-
'path': os.path.relpath(x, path),
146+
'path': os.path.relpath(x, path).replace(os.sep, '/'),
147147
'size': os.path.getsize(x),
148148
'modified': os.path.getmtime(x)
149149
} for x in results if os.path.isfile(x)
150150
]
151151
else:
152-
results = [os.path.relpath(x, path) for x in results if os.path.isfile(x)]
152+
results = [
153+
os.path.relpath(x, path).replace(os.sep, '/')
154+
for x in results
155+
if os.path.isfile(x)
156+
]
153157

154158
split_path = request.rel_url.query.get('split', '').lower() == "true"
155159
if split_path and not full_info:
156-
results = [[x] + x.split(os.sep) for x in results]
160+
results = [[x] + x.split('/') for x in results]
157161

158162
return web.json_response(results)
159163

tests-unit/prompt_server_test/user_manager_test.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from aiohttp import web
44
from app.user_manager import UserManager
5+
from unittest.mock import patch
56

67
pytestmark = (
78
pytest.mark.asyncio
@@ -53,7 +54,7 @@ async def test_listuserdata_recursive(aiohttp_client, app, tmp_path):
5354
client = await aiohttp_client(app)
5455
resp = await client.get("/userdata?dir=test_dir&recurse=true")
5556
assert resp.status == 200
56-
assert set(await resp.json()) == {"file1.txt", os.path.join("subdir", "file2.txt")}
57+
assert set(await resp.json()) == {"file1.txt", "subdir/file2.txt"}
5758

5859

5960
async def test_listuserdata_full_info(aiohttp_client, app, tmp_path):
@@ -80,11 +81,40 @@ async def test_listuserdata_split_path(aiohttp_client, app, tmp_path):
8081
resp = await client.get("/userdata?dir=test_dir&recurse=true&split=true")
8182
assert resp.status == 200
8283
assert await resp.json() == [
83-
[os.path.join("subdir", "file1.txt"), "subdir", "file1.txt"]
84+
["subdir/file1.txt", "subdir", "file1.txt"]
8485
]
8586

8687

8788
async def test_listuserdata_invalid_directory(aiohttp_client, app):
8889
client = await aiohttp_client(app)
8990
resp = await client.get("/userdata?dir=")
9091
assert resp.status == 400
92+
93+
94+
async def test_listuserdata_normalized_separator(aiohttp_client, app, tmp_path):
95+
os_sep = "\\"
96+
with patch("os.sep", os_sep):
97+
with patch("os.path.sep", os_sep):
98+
os.makedirs(tmp_path / "test_dir" / "subdir")
99+
with open(tmp_path / "test_dir" / "subdir" / "file1.txt", "w") as f:
100+
f.write("test content")
101+
102+
client = await aiohttp_client(app)
103+
resp = await client.get("/userdata?dir=test_dir&recurse=true")
104+
assert resp.status == 200
105+
result = await resp.json()
106+
assert len(result) == 1
107+
assert "/" in result[0] # Ensure forward slash is used
108+
assert "\\" not in result[0] # Ensure backslash is not present
109+
assert result[0] == "subdir/file1.txt"
110+
111+
# Test with full_info
112+
resp = await client.get(
113+
"/userdata?dir=test_dir&recurse=true&full_info=true"
114+
)
115+
assert resp.status == 200
116+
result = await resp.json()
117+
assert len(result) == 1
118+
assert "/" in result[0]["path"] # Ensure forward slash is used
119+
assert "\\" not in result[0]["path"] # Ensure backslash is not present
120+
assert result[0]["path"] == "subdir/file1.txt"

0 commit comments

Comments
 (0)