@@ -98,14 +98,8 @@ def download2stream(self, path: str | FsNode, fp, **kwargs) -> None:
9898 The object must implement the ``file.write`` method and be able to write binary data.
9999 :param kwargs: **chunk_size** an int value specifying chunk size to write. Default = **5Mb**
100100 """
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 )
109103
110104 def download_directory_as_zip (self , path : str | FsNode , local_path : str | Path | None = None , ** kwargs ) -> Path :
111105 """Downloads a remote directory as zip archive.
@@ -439,12 +433,6 @@ def _listdir(
439433 self ._session .cfg .dav_url_suffix , webdav_response , user , path , properties , exclude_self , prop_type
440434 )
441435
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-
448436 def __upload_stream (self , path : str , fp , chunk_size : int ) -> FsNode :
449437 _tmp_path = "nc-py-api-" + random_string (56 )
450438 _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:
561549 The object must implement the ``file.write`` method and be able to write binary data.
562550 :param kwargs: **chunk_size** an int value specifying chunk size to write. Default = **5Mb**
563551 """
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 )
572554
573555 async def download_directory_as_zip (
574556 self , path : str | FsNode , local_path : str | Path | None = None , ** kwargs
@@ -909,14 +891,6 @@ async def _listdir(
909891 self ._session .cfg .dav_url_suffix , webdav_response , user , path , properties , exclude_self , prop_type
910892 )
911893
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-
920894 async def __upload_stream (self , path : str , fp , chunk_size : int ) -> FsNode :
921895 _tmp_path = "nc-py-api-" + random_string (56 )
922896 _dav_path = quote (dav_get_obj_path (await self ._session .user , _tmp_path , root_path = "/uploads" ))
0 commit comments