Skip to content

Commit fd228e7

Browse files
authored
use existing _session.download2fp method [refactor] (#203)
Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
1 parent 6ef2d85 commit fd228e7

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

nc_py_api/_session.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ def set_user(self, user_id: str) -> None:
255255
def download2stream(self, url_path: str, fp, dav: bool = False, **kwargs):
256256
if isinstance(fp, str | pathlib.Path):
257257
with builtins.open(fp, "wb") as f:
258-
self._download2fp(url_path, f, dav, **kwargs)
258+
self.download2fp(url_path, f, dav, **kwargs)
259259
elif hasattr(fp, "write"):
260-
self._download2fp(url_path, fp, dav, **kwargs)
260+
self.download2fp(url_path, fp, dav, **kwargs)
261261
else:
262262
raise TypeError("`fp` must be a path to file or an object with `write` method.")
263263

@@ -287,9 +287,9 @@ def _response_event(self, response: Response) -> None:
287287
return
288288
self.response_headers = response.headers
289289

290-
def _download2fp(self, url_path: str, fp, dav: bool, **kwargs):
290+
def download2fp(self, url_path: str, fp, dav: bool, params=None, **kwargs):
291291
adapter = self.adapter_dav if dav else self.adapter
292-
with adapter.stream("GET", url_path) as response:
292+
with adapter.stream("GET", url_path, params=params) as response:
293293
check_error(response)
294294
for data_chunk in response.iter_raw(chunk_size=kwargs.get("chunk_size", 5 * 1024 * 1024)):
295295
fp.write(data_chunk)
@@ -375,9 +375,9 @@ def set_user(self, user: str) -> None:
375375
async def download2stream(self, url_path: str, fp, dav: bool = False, **kwargs):
376376
if isinstance(fp, str | pathlib.Path):
377377
with builtins.open(fp, "wb") as f:
378-
await self._download2fp(url_path, f, dav, **kwargs)
378+
await self.download2fp(url_path, f, dav, **kwargs)
379379
elif hasattr(fp, "write"):
380-
await self._download2fp(url_path, fp, dav, **kwargs)
380+
await self.download2fp(url_path, fp, dav, **kwargs)
381381
else:
382382
raise TypeError("`fp` must be a path to file or an object with `write` method.")
383383

@@ -407,9 +407,9 @@ async def _response_event(self, response: Response) -> None:
407407
return
408408
self.response_headers = response.headers
409409

410-
async def _download2fp(self, url_path: str, fp, dav: bool, **kwargs):
410+
async def download2fp(self, url_path: str, fp, dav: bool, params=None, **kwargs):
411411
adapter = self.adapter_dav if dav else self.adapter
412-
async with adapter.stream("GET", url_path) as response:
412+
async with adapter.stream("GET", url_path, params=params) as response:
413413
check_error(response)
414414
async for data_chunk in response.aiter_raw(chunk_size=kwargs.get("chunk_size", 5 * 1024 * 1024)):
415415
fp.write(data_chunk)

nc_py_api/files/files.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,11 @@ def download_directory_as_zip(self, path: str | FsNode, local_path: str | Path |
111111
.. note:: This works only for directories, you should not use this to download a file.
112112
"""
113113
path = path.user_path if isinstance(path, FsNode) else path
114-
with self._session.adapter.stream(
115-
"GET", "/index.php/apps/files/ajax/download.php", params={"dir": path}
116-
) as response:
117-
check_error(response, f"download_directory_as_zip: user={self._session.user}, path={path}")
118-
result_path = local_path if local_path else os.path.basename(path)
119-
with open(
120-
result_path,
121-
"wb",
122-
) as fp:
123-
for data_chunk in response.iter_raw(chunk_size=kwargs.get("chunk_size", 5 * 1024 * 1024)):
124-
fp.write(data_chunk)
114+
result_path = local_path if local_path else os.path.basename(path)
115+
with open(result_path, "wb") as fp:
116+
self._session.download2fp(
117+
"/index.php/apps/files/ajax/download.php", fp, dav=False, params={"dir": path}, **kwargs
118+
)
125119
return Path(result_path)
126120

127121
def upload(self, path: str | FsNode, content: bytes | str) -> FsNode:
@@ -564,17 +558,11 @@ async def download_directory_as_zip(
564558
.. note:: This works only for directories, you should not use this to download a file.
565559
"""
566560
path = path.user_path if isinstance(path, FsNode) else path
567-
async with self._session.adapter.stream(
568-
"GET", "/index.php/apps/files/ajax/download.php", params={"dir": path}
569-
) as response:
570-
check_error(response, f"download_directory_as_zip: user={await self._session.user}, path={path}")
571-
result_path = local_path if local_path else os.path.basename(path)
572-
with open(
573-
result_path,
574-
"wb",
575-
) as fp:
576-
async for data_chunk in response.aiter_raw(chunk_size=kwargs.get("chunk_size", 5 * 1024 * 1024)):
577-
fp.write(data_chunk)
561+
result_path = local_path if local_path else os.path.basename(path)
562+
with open(result_path, "wb") as fp:
563+
await self._session.download2fp(
564+
"/index.php/apps/files/ajax/download.php", fp, dav=False, params={"dir": path}, **kwargs
565+
)
578566
return Path(result_path)
579567

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

0 commit comments

Comments
 (0)