Skip to content

Commit

Permalink
Update file endpoints to use query parameters instead of path paramet…
Browse files Browse the repository at this point in the history
…ers for proxy compatibility

Signed-off-by: bigcat88 <[email protected]>
  • Loading branch information
bigcat88 committed Jan 7, 2025
1 parent 5cbf797 commit 7e1ae9b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
35 changes: 16 additions & 19 deletions app/user_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,29 +197,30 @@ def process_full_path(full_path: str) -> FileInfo | str | list[str]:

return web.json_response(results)

def get_user_data_path(request, check_exists = False, param = "file"):
file = request.match_info.get(param, None)
def get_user_data_path(request, check_exists=False, param="file"):
"""Reads a file-like parameter from the query string."""
file = request.query.get(param)
if not file:
return web.Response(status=400)
return web.Response(status=400, text=f"Missing query param '{param}'.")

path = self.get_request_user_filepath(request, file)
if not path:
return web.Response(status=403)
return web.Response(status=403, text="Forbidden path.")

if check_exists and not os.path.exists(path):
return web.Response(status=404)
return web.Response(status=404, text="File not found.")

return path

@routes.get("/userdata/{file}")
@routes.get("/userdata/file")
async def getuserdata(request):
path = get_user_data_path(request, check_exists=True)
if not isinstance(path, str):
return path

return web.FileResponse(path)

@routes.post("/userdata/{file}")
@routes.post("/userdata/file")
async def post_userdata(request):
"""
Upload or update a user data file.
Expand All @@ -228,13 +229,11 @@ async def post_userdata(request):
controlling overwrite behavior and response format.
Query Parameters:
- file: The target file path (URL encoded if necessary).
- overwrite (optional): If "false", prevents overwriting existing files. Defaults to "true".
- full_info (optional): If "true", returns detailed file information (path, size, modified time).
If "false", returns only the relative file path.
Path Parameters:
- file: The target file path (URL encoded if necessary).
Returns:
- 400: If 'file' parameter is missing.
- 403: If the requested path is not allowed.
Expand All @@ -250,7 +249,7 @@ async def post_userdata(request):
return path

overwrite = request.query.get("overwrite", 'true') != "false"
full_info = request.query.get('full_info', 'false').lower() == "true"
full_info = request.query.get("full_info", 'false').lower() == "true"

if not overwrite and os.path.exists(path):
return web.Response(status=409, text="File already exists")
Expand All @@ -268,7 +267,7 @@ async def post_userdata(request):

return web.json_response(resp)

@routes.delete("/userdata/{file}")
@routes.delete("/userdata/file")
async def delete_userdata(request):
path = get_user_data_path(request, check_exists=True)
if not isinstance(path, str):
Expand All @@ -278,19 +277,17 @@ async def delete_userdata(request):

return web.Response(status=204)

@routes.post("/userdata/{file}/move/{dest}")
@routes.post("/userdata/file/move")
async def move_userdata(request):
"""
Move or rename a user data file.
This endpoint handles moving or renaming files within a user's data directory, with options for
controlling overwrite behavior and response format.
Path Parameters:
- file: The source file path (URL encoded if necessary)
- dest: The destination file path (URL encoded if necessary)
Query Parameters:
- source: The source file path (URL encoded if necessary)
- dest: The destination file path (URL encoded if necessary)
- overwrite (optional): If "false", prevents overwriting existing files. Defaults to "true".
- full_info (optional): If "true", returns detailed file information (path, size, modified time).
If "false", returns only the relative file path.
Expand All @@ -304,12 +301,12 @@ async def move_userdata(request):
- Full file information (if full_info=true)
- Relative file path (if full_info=false)
"""
source = get_user_data_path(request, check_exists=True)
source = get_user_data_path(request, check_exists=True, param="source")
if not isinstance(source, str):
return source

dest = get_user_data_path(request, check_exists=False, param="dest")
if not isinstance(source, str):
if not isinstance(dest, str):
return dest

overwrite = request.query.get("overwrite", 'true') != "false"
Expand Down
14 changes: 7 additions & 7 deletions tests-unit/prompt_server_test/user_manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async def test_listuserdata_normalized_separator(aiohttp_client, app, tmp_path):
async def test_post_userdata_new_file(aiohttp_client, app, tmp_path):
client = await aiohttp_client(app)
content = b"test content"
resp = await client.post("/userdata/test.txt", data=content)
resp = await client.post("/userdata/file?file=test.txt", data=content)

assert resp.status == 200
assert await resp.text() == '"test.txt"'
Expand All @@ -138,7 +138,7 @@ async def test_post_userdata_overwrite_existing(aiohttp_client, app, tmp_path):

client = await aiohttp_client(app)
new_content = b"updated content"
resp = await client.post("/userdata/test.txt", data=new_content)
resp = await client.post("/userdata/file?file=test.txt", data=new_content)

assert resp.status == 200
assert await resp.text() == '"test.txt"'
Expand All @@ -154,7 +154,7 @@ async def test_post_userdata_no_overwrite(aiohttp_client, app, tmp_path):
f.write("initial content")

client = await aiohttp_client(app)
resp = await client.post("/userdata/test.txt?overwrite=false", data=b"new content")
resp = await client.post("/userdata/file?file=test.txt&overwrite=false", data=b"new content")

assert resp.status == 409

Expand All @@ -166,7 +166,7 @@ async def test_post_userdata_no_overwrite(aiohttp_client, app, tmp_path):
async def test_post_userdata_full_info(aiohttp_client, app, tmp_path):
client = await aiohttp_client(app)
content = b"test content"
resp = await client.post("/userdata/test.txt?full_info=true", data=content)
resp = await client.post("/userdata/file?file=test.txt&full_info=true", data=content)

assert resp.status == 200
result = await resp.json()
Expand All @@ -181,7 +181,7 @@ async def test_move_userdata(aiohttp_client, app, tmp_path):
f.write("test content")

client = await aiohttp_client(app)
resp = await client.post("/userdata/source.txt/move/dest.txt")
resp = await client.post("/userdata/file/move?source=source.txt&dest=dest.txt")

assert resp.status == 200
assert await resp.text() == '"dest.txt"'
Expand All @@ -200,7 +200,7 @@ async def test_move_userdata_no_overwrite(aiohttp_client, app, tmp_path):
f.write("destination content")

client = await aiohttp_client(app)
resp = await client.post("/userdata/source.txt/move/dest.txt?overwrite=false")
resp = await client.post("/userdata/file/move?source=source.txt&dest=dest.txt&overwrite=false")

assert resp.status == 409

Expand All @@ -217,7 +217,7 @@ async def test_move_userdata_full_info(aiohttp_client, app, tmp_path):
f.write("test content")

client = await aiohttp_client(app)
resp = await client.post("/userdata/source.txt/move/dest.txt?full_info=true")
resp = await client.post("/userdata/file/move?source=source.txt&dest=dest.txt&full_info=true")

assert resp.status == 200
result = await resp.json()
Expand Down

0 comments on commit 7e1ae9b

Please sign in to comment.