Skip to content
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
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