Skip to content

Commit fabe8ea

Browse files
authored
Fix of download_directory_as_zip function for NC31 (#304)
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]>
1 parent f2522aa commit fabe8ea

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

nc_py_api/_session.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def _response_event(self, response: Response) -> None:
301301

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

426426
async def download2fp(self, url_path: str, fp, dav: bool, params=None, **kwargs):
427427
adapter = self.adapter_dav if dav else self.adapter
428-
async with adapter.stream("GET", url_path, params=params) as response:
428+
async with adapter.stream("GET", url_path, params=params, headers=kwargs.get("headers", None)) as response:
429429
check_error(response)
430430
async for data_chunk in response.aiter_raw(chunk_size=kwargs.get("chunk_size", 5 * 1024 * 1024)):
431431
fp.write(data_chunk)

nc_py_api/files/files.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,14 @@ def download_directory_as_zip(self, path: str | FsNode, local_path: str | Path |
115115
path = path.user_path if isinstance(path, FsNode) else path
116116
result_path = local_path if local_path else os.path.basename(path)
117117
with open(result_path, "wb") as fp:
118-
self._session.download2fp(
119-
"/index.php/apps/files/ajax/download.php", fp, dav=False, params={"dir": path}, **kwargs
120-
)
118+
if self._session.nc_version["major"] >= 31:
119+
full_path = dav_get_obj_path(self._session.user, path)
120+
accept_header = f"application/{kwargs.get('format', 'zip')}"
121+
self._session.download2fp(quote(full_path), fp, dav=True, headers={"Accept": accept_header})
122+
else:
123+
self._session.download2fp(
124+
"/index.php/apps/files/ajax/download.php", fp, dav=False, params={"dir": path}, **kwargs
125+
)
121126
return Path(result_path)
122127

123128
def upload(self, path: str | FsNode, content: bytes | str) -> FsNode:

nc_py_api/files/files_async.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,14 @@ async def download_directory_as_zip(
119119
path = path.user_path if isinstance(path, FsNode) else path
120120
result_path = local_path if local_path else os.path.basename(path)
121121
with open(result_path, "wb") as fp:
122-
await self._session.download2fp(
123-
"/index.php/apps/files/ajax/download.php", fp, dav=False, params={"dir": path}, **kwargs
124-
)
122+
if (await self._session.nc_version)["major"] >= 31:
123+
full_path = dav_get_obj_path(await self._session.user, path)
124+
accept_header = f"application/{kwargs.get('format', 'zip')}"
125+
await self._session.download2fp(quote(full_path), fp, dav=True, headers={"Accept": accept_header})
126+
else:
127+
await self._session.download2fp(
128+
"/index.php/apps/files/ajax/download.php", fp, dav=False, params={"dir": path}, **kwargs
129+
)
125130
return Path(result_path)
126131

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

0 commit comments

Comments
 (0)