diff --git a/nc_py_api/_session.py b/nc_py_api/_session.py index 27bd93e6..66326c7b 100644 --- a/nc_py_api/_session.py +++ b/nc_py_api/_session.py @@ -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) @@ -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) diff --git a/nc_py_api/files/files.py b/nc_py_api/files/files.py index 3fd4e804..8b356c2a 100644 --- a/nc_py_api/files/files.py +++ b/nc_py_api/files/files.py @@ -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: diff --git a/nc_py_api/files/files_async.py b/nc_py_api/files/files_async.py index a31a8c25..54c8b6fb 100644 --- a/nc_py_api/files/files_async.py +++ b/nc_py_api/files/files_async.py @@ -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: