Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usability improvements to SelectRemotePath #8411

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/tribler/core/restapi/file_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import contextlib
import logging
import os
import platform
from pathlib import Path

from aiohttp import web

from tribler.core.restapi.rest_endpoint import HTTP_NOT_FOUND, RESTEndpoint, RESTResponse
from tribler.core.restapi.rest_endpoint import HTTP_INTERNAL_SERVER_ERROR, HTTP_NOT_FOUND, RESTEndpoint, RESTResponse

if platform.system() == 'Windows':
import win32api
Expand All @@ -25,7 +26,8 @@ def __init__(self) -> None:
super().__init__()
self._logger = logging.getLogger(self.__class__.__name__)
self.app.add_routes([web.get('/browse', self.browse),
web.get('/list', self.list)])
web.get('/list', self.list),
web.get('/create', self.create)])

async def browse(self, request: web.Request) -> RESTResponse:
"""
Expand All @@ -48,7 +50,8 @@ async def browse(self, request: web.Request) -> RESTResponse:
}
)
return RESTResponse({"current": "Root",
"paths": paths})
"paths": paths,
"separator": os.path.sep})

# Move up until we find a directory
parent_path = Path(path).resolve()
Expand All @@ -73,7 +76,8 @@ async def browse(self, request: web.Request) -> RESTResponse:
})

return RESTResponse({"current": str(parent_path.resolve()),
"paths": results})
"paths": results,
"separator": os.path.sep})

async def list(self, request: web.Request) -> RESTResponse:
"""
Expand All @@ -93,3 +97,18 @@ async def list(self, request: web.Request) -> RESTResponse:
for file in path.glob(f"{'**/' if recursively else ''}*") if file.is_file()]

return RESTResponse({"paths": results})

async def create(self, request: web.Request) -> RESTResponse:
"""
Create the specified path.
"""
path = Path(request.query.get("path", ""))
recursively = request.query.get("recursively", "1") != "0"
try:
path.mkdir(parents=recursively, exist_ok=True)
except (OSError, FileNotFoundError) as e:
return RESTResponse({"error": {
"handled": True,
"message": str(e)
}}, status=HTTP_INTERNAL_SERVER_ERROR)
return RESTResponse({"paths": [{"name": path.name, "path": str(path.resolve()), "dir": True}]})
Loading
Loading