Skip to content

Commit

Permalink
Fix of download_directory_as_zip function for NC31 (#304)
Browse files Browse the repository at this point in the history
In 31 Nextcloud, the old method of downloading folders has simply been
removed.

The changes are very useful, downloading a folder through webdav has
become more pleasant, and now we can download it in `tar` format,
something like this:

```python
   nc.files.download_directory_as_zip("test_empty_dir_in_dir", "2.tar", format="x-tar")
```

Default usage doesn't changed, so **no something breaking** for
nc_py_api usage.

Signed-off-by: Alexander Piskun <[email protected]>
  • Loading branch information
bigcat88 authored Sep 29, 2024
1 parent f2522aa commit fabe8ea
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions nc_py_api/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def _response_event(self, response: Response) -> None:

def download2fp(self, url_path: str, fp, dav: bool, params=None, **kwargs):
adapter = self.adapter_dav if dav else self.adapter
with adapter.stream("GET", url_path, params=params) as response:
with adapter.stream("GET", url_path, params=params, headers=kwargs.get("headers", None)) as response:
check_error(response)
for data_chunk in response.iter_raw(chunk_size=kwargs.get("chunk_size", 5 * 1024 * 1024)):
fp.write(data_chunk)
Expand Down Expand Up @@ -425,7 +425,7 @@ async def _response_event(self, response: Response) -> None:

async def download2fp(self, url_path: str, fp, dav: bool, params=None, **kwargs):
adapter = self.adapter_dav if dav else self.adapter
async with adapter.stream("GET", url_path, params=params) as response:
async with adapter.stream("GET", url_path, params=params, headers=kwargs.get("headers", None)) as response:
check_error(response)
async for data_chunk in response.aiter_raw(chunk_size=kwargs.get("chunk_size", 5 * 1024 * 1024)):
fp.write(data_chunk)
Expand Down
11 changes: 8 additions & 3 deletions nc_py_api/files/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,14 @@ def download_directory_as_zip(self, path: str | FsNode, local_path: str | Path |
path = path.user_path if isinstance(path, FsNode) else path
result_path = local_path if local_path else os.path.basename(path)
with open(result_path, "wb") as fp:
self._session.download2fp(
"/index.php/apps/files/ajax/download.php", fp, dav=False, params={"dir": path}, **kwargs
)
if self._session.nc_version["major"] >= 31:
full_path = dav_get_obj_path(self._session.user, path)
accept_header = f"application/{kwargs.get('format', 'zip')}"
self._session.download2fp(quote(full_path), fp, dav=True, headers={"Accept": accept_header})
else:
self._session.download2fp(
"/index.php/apps/files/ajax/download.php", fp, dav=False, params={"dir": path}, **kwargs
)
return Path(result_path)

def upload(self, path: str | FsNode, content: bytes | str) -> FsNode:
Expand Down
11 changes: 8 additions & 3 deletions nc_py_api/files/files_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,14 @@ async def download_directory_as_zip(
path = path.user_path if isinstance(path, FsNode) else path
result_path = local_path if local_path else os.path.basename(path)
with open(result_path, "wb") as fp:
await self._session.download2fp(
"/index.php/apps/files/ajax/download.php", fp, dav=False, params={"dir": path}, **kwargs
)
if (await self._session.nc_version)["major"] >= 31:
full_path = dav_get_obj_path(await self._session.user, path)
accept_header = f"application/{kwargs.get('format', 'zip')}"
await self._session.download2fp(quote(full_path), fp, dav=True, headers={"Accept": accept_header})
else:
await self._session.download2fp(
"/index.php/apps/files/ajax/download.php", fp, dav=False, params={"dir": path}, **kwargs
)
return Path(result_path)

async def upload(self, path: str | FsNode, content: bytes | str) -> FsNode:
Expand Down

0 comments on commit fabe8ea

Please sign in to comment.