@@ -98,14 +98,8 @@ def download2stream(self, path: str | FsNode, fp, **kwargs) -> None:
98
98
The object must implement the ``file.write`` method and be able to write binary data.
99
99
:param kwargs: **chunk_size** an int value specifying chunk size to write. Default = **5Mb**
100
100
"""
101
- path = path .user_path if isinstance (path , FsNode ) else path
102
- if isinstance (fp , str | Path ):
103
- with builtins .open (fp , "wb" ) as f :
104
- self .__download2stream (path , f , ** kwargs )
105
- elif hasattr (fp , "write" ):
106
- self .__download2stream (path , fp , ** kwargs )
107
- else :
108
- raise TypeError ("`fp` must be a path to file or an object with `write` method." )
101
+ path = quote (dav_get_obj_path (self ._session .user , path .user_path if isinstance (path , FsNode ) else path ))
102
+ self ._session .download2stream (path , fp , dav = True , ** kwargs )
109
103
110
104
def download_directory_as_zip (self , path : str | FsNode , local_path : str | Path | None = None , ** kwargs ) -> Path :
111
105
"""Downloads a remote directory as zip archive.
@@ -439,12 +433,6 @@ def _listdir(
439
433
self ._session .cfg .dav_url_suffix , webdav_response , user , path , properties , exclude_self , prop_type
440
434
)
441
435
442
- def __download2stream (self , path : str , fp , ** kwargs ) -> None :
443
- with self ._session .adapter_dav .stream ("GET" , quote (dav_get_obj_path (self ._session .user , path ))) as response :
444
- check_error (response , f"download_stream: user={ self ._session .user } , path={ path } " )
445
- for data_chunk in response .iter_raw (chunk_size = kwargs .get ("chunk_size" , 5 * 1024 * 1024 )):
446
- fp .write (data_chunk )
447
-
448
436
def __upload_stream (self , path : str , fp , chunk_size : int ) -> FsNode :
449
437
_tmp_path = "nc-py-api-" + random_string (56 )
450
438
_dav_path = quote (dav_get_obj_path (self ._session .user , _tmp_path , root_path = "/uploads" ))
@@ -561,14 +549,8 @@ async def download2stream(self, path: str | FsNode, fp, **kwargs) -> None:
561
549
The object must implement the ``file.write`` method and be able to write binary data.
562
550
:param kwargs: **chunk_size** an int value specifying chunk size to write. Default = **5Mb**
563
551
"""
564
- path = path .user_path if isinstance (path , FsNode ) else path
565
- if isinstance (fp , str | Path ):
566
- with builtins .open (fp , "wb" ) as f :
567
- await self .__download2stream (path , f , ** kwargs )
568
- elif hasattr (fp , "write" ):
569
- await self .__download2stream (path , fp , ** kwargs )
570
- else :
571
- raise TypeError ("`fp` must be a path to file or an object with `write` method." )
552
+ path = quote (dav_get_obj_path (await self ._session .user , path .user_path if isinstance (path , FsNode ) else path ))
553
+ await self ._session .download2stream (path , fp , dav = True , ** kwargs )
572
554
573
555
async def download_directory_as_zip (
574
556
self , path : str | FsNode , local_path : str | Path | None = None , ** kwargs
@@ -909,14 +891,6 @@ async def _listdir(
909
891
self ._session .cfg .dav_url_suffix , webdav_response , user , path , properties , exclude_self , prop_type
910
892
)
911
893
912
- async def __download2stream (self , path : str , fp , ** kwargs ) -> None :
913
- async with self ._session .adapter_dav .stream (
914
- "GET" , quote (dav_get_obj_path (await self ._session .user , path ))
915
- ) as response :
916
- check_error (response , f"download_stream: user={ await self ._session .user } , path={ path } " )
917
- async for data_chunk in response .aiter_raw (chunk_size = kwargs .get ("chunk_size" , 5 * 1024 * 1024 )):
918
- fp .write (data_chunk )
919
-
920
894
async def __upload_stream (self , path : str , fp , chunk_size : int ) -> FsNode :
921
895
_tmp_path = "nc-py-api-" + random_string (56 )
922
896
_dav_path = quote (dav_get_obj_path (await self ._session .user , _tmp_path , root_path = "/uploads" ))
0 commit comments