diff --git a/nc_py_api/_session.py b/nc_py_api/_session.py index d99cf155..2e7a26b7 100644 --- a/nc_py_api/_session.py +++ b/nc_py_api/_session.py @@ -255,9 +255,9 @@ def set_user(self, user_id: str) -> None: def download2stream(self, url_path: str, fp, dav: bool = False, **kwargs): if isinstance(fp, str | pathlib.Path): with builtins.open(fp, "wb") as f: - self._download2fp(url_path, f, dav, **kwargs) + self.download2fp(url_path, f, dav, **kwargs) elif hasattr(fp, "write"): - self._download2fp(url_path, fp, dav, **kwargs) + self.download2fp(url_path, fp, dav, **kwargs) else: raise TypeError("`fp` must be a path to file or an object with `write` method.") @@ -287,9 +287,9 @@ def _response_event(self, response: Response) -> None: return self.response_headers = response.headers - def _download2fp(self, url_path: str, fp, dav: bool, **kwargs): + 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) as response: + with adapter.stream("GET", url_path, params=params) 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) @@ -375,9 +375,9 @@ def set_user(self, user: str) -> None: async def download2stream(self, url_path: str, fp, dav: bool = False, **kwargs): if isinstance(fp, str | pathlib.Path): with builtins.open(fp, "wb") as f: - await self._download2fp(url_path, f, dav, **kwargs) + await self.download2fp(url_path, f, dav, **kwargs) elif hasattr(fp, "write"): - await self._download2fp(url_path, fp, dav, **kwargs) + await self.download2fp(url_path, fp, dav, **kwargs) else: raise TypeError("`fp` must be a path to file or an object with `write` method.") @@ -407,9 +407,9 @@ async def _response_event(self, response: Response) -> None: return self.response_headers = response.headers - async def _download2fp(self, url_path: str, fp, dav: bool, **kwargs): + 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) as response: + async with adapter.stream("GET", url_path, params=params) 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 facd2790..4f24e55c 100644 --- a/nc_py_api/files/files.py +++ b/nc_py_api/files/files.py @@ -111,17 +111,11 @@ def download_directory_as_zip(self, path: str | FsNode, local_path: str | Path | .. note:: This works only for directories, you should not use this to download a file. """ path = path.user_path if isinstance(path, FsNode) else path - with self._session.adapter.stream( - "GET", "/index.php/apps/files/ajax/download.php", params={"dir": path} - ) as response: - check_error(response, f"download_directory_as_zip: user={self._session.user}, path={path}") - result_path = local_path if local_path else os.path.basename(path) - with open( - result_path, - "wb", - ) as fp: - for data_chunk in response.iter_raw(chunk_size=kwargs.get("chunk_size", 5 * 1024 * 1024)): - fp.write(data_chunk) + 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 + ) return Path(result_path) def upload(self, path: str | FsNode, content: bytes | str) -> FsNode: @@ -564,17 +558,11 @@ async def download_directory_as_zip( .. note:: This works only for directories, you should not use this to download a file. """ path = path.user_path if isinstance(path, FsNode) else path - async with self._session.adapter.stream( - "GET", "/index.php/apps/files/ajax/download.php", params={"dir": path} - ) as response: - check_error(response, f"download_directory_as_zip: user={await self._session.user}, path={path}") - result_path = local_path if local_path else os.path.basename(path) - with open( - result_path, - "wb", - ) as fp: - async for data_chunk in response.aiter_raw(chunk_size=kwargs.get("chunk_size", 5 * 1024 * 1024)): - fp.write(data_chunk) + 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 + ) return Path(result_path) async def upload(self, path: str | FsNode, content: bytes | str) -> FsNode: