Skip to content

Commit

Permalink
use existing _session.download2fp method [refactor] (#203)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Piskun <[email protected]>
  • Loading branch information
bigcat88 authored Jan 12, 2024
1 parent 6ef2d85 commit fd228e7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
16 changes: 8 additions & 8 deletions nc_py_api/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.")

Expand Down Expand Up @@ -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)
Expand Down
32 changes: 10 additions & 22 deletions nc_py_api/files/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit fd228e7

Please sign in to comment.