Skip to content

files: added url quote to mkdir and delete #192

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

Merged
merged 2 commits into from
Dec 26, 2023
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

All notable changes to this project will be documented in this file.

## [0.7.1 - 2022-12-2x]
## [0.7.2 - 2022-12-27]

### Fixed

- files: proper url encoding of special chars in `mkdir` and `delete` methods. #191 Thanks to @tobenary

## [0.7.1 - 2022-12-21]

### Added

Expand Down
2 changes: 1 addition & 1 deletion nc_py_api/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Version of nc_py_api."""

__version__ = "0.7.1"
__version__ = "0.7.2.dev0"
9 changes: 5 additions & 4 deletions nc_py_api/files/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import builtins
import os
from pathlib import Path
from urllib.parse import quote

from httpx import Headers

Expand Down Expand Up @@ -166,7 +167,7 @@ def mkdir(self, path: str | FsNode) -> FsNode:
"""
path = path.user_path if isinstance(path, FsNode) else path
full_path = dav_get_obj_path(self._session.user, path)
response = self._session.adapter_dav.request("MKCOL", full_path)
response = self._session.adapter_dav.request("MKCOL", quote(full_path))
check_error(response)
full_path += "/" if not full_path.endswith("/") else ""
return FsNode(full_path.lstrip("/"), **etag_fileid_from_response(response))
Expand Down Expand Up @@ -201,7 +202,7 @@ def delete(self, path: str | FsNode, not_fail=False) -> None:
:param not_fail: if set to ``True`` and the object is not found, it does not raise an exception.
"""
path = path.user_path if isinstance(path, FsNode) else path
response = self._session.adapter_dav.delete(dav_get_obj_path(self._session.user, path))
response = self._session.adapter_dav.delete(quote(dav_get_obj_path(self._session.user, path)))
if response.status_code == 404 and not_fail:
return
check_error(response)
Expand Down Expand Up @@ -630,7 +631,7 @@ async def mkdir(self, path: str | FsNode) -> FsNode:
"""
path = path.user_path if isinstance(path, FsNode) else path
full_path = dav_get_obj_path(await self._session.user, path)
response = await self._session.adapter_dav.request("MKCOL", full_path)
response = await self._session.adapter_dav.request("MKCOL", quote(full_path))
check_error(response)
full_path += "/" if not full_path.endswith("/") else ""
return FsNode(full_path.lstrip("/"), **etag_fileid_from_response(response))
Expand Down Expand Up @@ -665,7 +666,7 @@ async def delete(self, path: str | FsNode, not_fail=False) -> None:
:param not_fail: if set to ``True`` and the object is not found, it does not raise an exception.
"""
path = path.user_path if isinstance(path, FsNode) else path
response = await self._session.adapter_dav.delete(dav_get_obj_path(await self._session.user, path))
response = await self._session.adapter_dav.delete(quote(dav_get_obj_path(await self._session.user, path)))
if response.status_code == 404 and not_fail:
return
check_error(response)
Expand Down
8 changes: 6 additions & 2 deletions tests/actual_tests/files_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,31 +452,35 @@ async def test_file_upload_chunked_zero_size_async(anc_any, file_name):
await anc_any.files.delete(f"/test_dir_tmp/{file_name}", not_fail=True)


@pytest.mark.parametrize("dir_name", ("1 2", "Яё", "відео та картинки", "复杂 目录 Í", "Björn", "João"))
@pytest.mark.parametrize("dir_name", ("1 2", "Яё", "відео та картинки", "复杂 目录 Í", "Björn", "João", "1##3"))
def test_mkdir(nc_any, dir_name):
nc_any.files.delete(dir_name, not_fail=True)
result = nc_any.files.mkdir(dir_name)
assert result.is_dir
assert not result.has_extra
result_by_id = nc_any.files.by_id(result.file_id)
with pytest.raises(NextcloudException):
nc_any.files.mkdir(dir_name)
nc_any.files.delete(dir_name)
with pytest.raises(NextcloudException):
nc_any.files.delete(dir_name)
assert result_by_id.full_path == result.full_path


@pytest.mark.asyncio(scope="session")
@pytest.mark.parametrize("dir_name", ("1 2", "Яё", "відео та картинки", "复杂 目录 Í", "Björn", "João"))
@pytest.mark.parametrize("dir_name", ("1 2", "Яё", "відео та картинки", "复杂 目录 Í", "Björn", "João", "1##3"))
async def test_mkdir_async(anc_any, dir_name):
await anc_any.files.delete(dir_name, not_fail=True)
result = await anc_any.files.mkdir(dir_name)
assert result.is_dir
assert not result.has_extra
result_by_id = await anc_any.files.by_id(result.file_id)
with pytest.raises(NextcloudException):
await anc_any.files.mkdir(dir_name)
await anc_any.files.delete(dir_name)
with pytest.raises(NextcloudException):
await anc_any.files.delete(dir_name)
assert result_by_id.full_path == result.full_path


def test_mkdir_invalid_args(nc_any):
Expand Down