From 218cc5ad4c7d9c2da0d47310fdc981e63d23dfab Mon Sep 17 00:00:00 2001 From: Pink Rabbit Date: Fri, 11 Aug 2023 11:50:55 +0800 Subject: [PATCH 1/4] fix: when sharing a link, name may be empty --- owncloud/owncloud.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/owncloud/owncloud.py b/owncloud/owncloud.py index 2933503..ad02034 100644 --- a/owncloud/owncloud.py +++ b/owncloud/owncloud.py @@ -909,7 +909,7 @@ def share_file_with_link(self, path, **kwargs): 'path': path, 'url': data_el.find('url').text, 'token': data_el.find('token').text, - 'name': data_el.find('name').text + 'name': name.text if (name := data_el.find('name')) else None } ) raise HTTPResponseError(res) From ecbb692a6bc2f7b6785303478a8a19ed42a339bc Mon Sep 17 00:00:00 2001 From: Pink Rabbit Date: Fri, 11 Aug 2023 11:57:57 +0800 Subject: [PATCH 2/4] perf: increase the judgment when obtaining files --- owncloud/owncloud.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/owncloud/owncloud.py b/owncloud/owncloud.py index ad02034..9969d2f 100644 --- a/owncloud/owncloud.py +++ b/owncloud/owncloud.py @@ -506,6 +506,23 @@ def get_file(self, remote_path, local_file=None): self._webdav_url + parse.quote(self._encode_string(remote_path)), stream=True ) + + path_split = list(filter(None, remote_path.split("/"))) + try: + if len(path_split) > 1: + file_list = [x.get_name() for x in self.list(self._normalize_path("/".join(path_split[0:-1])))] + if path_split[-1] not in file_list: + raise FileNotFoundError(f"Remote file {path_split[-1]} not exist, please check!") + else: + file_list = [x.get_name() for x in self.list(path="./")] + if path_split[0] not in file_list: + raise FileNotFoundError(f"Remote file {path_split[-1]} not exist, please check!") + except HTTPResponseError as e: + if e.status_code == 404: + raise FileNotFoundError(f"Remote file {path_split[-1]} not exist, please check!") + else: + raise e + if res.status_code == 200: if local_file is None: # use downloaded file name from Content-Disposition From 7306a031d2da449d6f6f66e84bd3b194f0f7dfc8 Mon Sep 17 00:00:00 2001 From: Pink Rabbit Date: Fri, 11 Aug 2023 12:01:15 +0800 Subject: [PATCH 3/4] perf: increase the judgment on creating folders --- owncloud/owncloud.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/owncloud/owncloud.py b/owncloud/owncloud.py index 9969d2f..8fc62b7 100644 --- a/owncloud/owncloud.py +++ b/owncloud/owncloud.py @@ -718,6 +718,17 @@ def mkdir(self, path): """ if not path.endswith('/'): path += '/' + + path_split = list(filter(None, path.split("/"))) + if len(path_split) > 1: + file_list = [x.get_name() for x in self.list(self._normalize_path("/".join(path_split[0:-1])))] + if path_split[-1] in file_list: + raise FileExistsError(f"Remote folder {path_split[-1]} exist, please check!") + else: + file_list = [x.get_name() for x in self.list(path="./")] + if path_split[0] in file_list: + raise FileExistsError(f"Remote folder {path_split[-1]} exist, please check!") + return self._make_dav_request('MKCOL', path) def delete(self, path): From f41cc824181abfeda11b9071944145571c5b16e6 Mon Sep 17 00:00:00 2001 From: Pink Rabbit Date: Tue, 15 Aug 2023 10:42:08 +0800 Subject: [PATCH 4/4] perf: encapsulation check path method --- owncloud/owncloud.py | 61 ++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/owncloud/owncloud.py b/owncloud/owncloud.py index 8fc62b7..aeb0b69 100644 --- a/owncloud/owncloud.py +++ b/owncloud/owncloud.py @@ -392,7 +392,28 @@ def anon_login(self, folder_token, folder_password=''): url_components = parse.urlparse(self.url) self._davpath = url_components.path + 'public.php/webdav' self._webdav_url = self.url + 'public.php/webdav' - + + def check_remote_path(self, path: str): + path_split = list(filter(None, path.split("/"))) + try: + if len(path_split) > 1: + file_list = [x.get_name() for x in self.list(self._normalize_path("/".join(path_split[0:-1])))] + if path_split[-1] not in file_list: + return False + else: + return True + else: + file_list = [x.get_name() for x in self.list(path="./")] + if path_split[0] not in file_list: + return False + else: + return True + except HTTPResponseError as e: + if e.status_code == 404: + raise ValueError(f"Remote path {path_split[-1]} there is an error, please check!") + else: + raise e + @classmethod def from_public_link(cls, public_link, folder_password='', **kwargs): public_link_components = parse.urlparse(public_link) @@ -502,27 +523,13 @@ def get_file(self, remote_path, local_file=None): :raises: HTTPResponseError in case an HTTP error status was returned """ remote_path = self._normalize_path(remote_path) + if self.check_remote_path(path=remote_path): + raise FileNotFoundError(f"get_file function error: Remote file {remote_path} not exist, please check!") + res = self._session.get( self._webdav_url + parse.quote(self._encode_string(remote_path)), stream=True ) - - path_split = list(filter(None, remote_path.split("/"))) - try: - if len(path_split) > 1: - file_list = [x.get_name() for x in self.list(self._normalize_path("/".join(path_split[0:-1])))] - if path_split[-1] not in file_list: - raise FileNotFoundError(f"Remote file {path_split[-1]} not exist, please check!") - else: - file_list = [x.get_name() for x in self.list(path="./")] - if path_split[0] not in file_list: - raise FileNotFoundError(f"Remote file {path_split[-1]} not exist, please check!") - except HTTPResponseError as e: - if e.status_code == 404: - raise FileNotFoundError(f"Remote file {path_split[-1]} not exist, please check!") - else: - raise e - if res.status_code == 200: if local_file is None: # use downloaded file name from Content-Disposition @@ -588,6 +595,9 @@ def put_file(self, remote_path, local_source_file, **kwargs): :returns: True if the operation succeeded, False otherwise :raises: HTTPResponseError in case an HTTP error status was returned """ + if self.check_remote_path(path=remote_path): + raise FileExistsError(f"put_file function error: Remote file {remote_path} exist, please check!") + if kwargs.get('chunked', True): return self._put_file_chunked( remote_path, @@ -719,15 +729,8 @@ def mkdir(self, path): if not path.endswith('/'): path += '/' - path_split = list(filter(None, path.split("/"))) - if len(path_split) > 1: - file_list = [x.get_name() for x in self.list(self._normalize_path("/".join(path_split[0:-1])))] - if path_split[-1] in file_list: - raise FileExistsError(f"Remote folder {path_split[-1]} exist, please check!") - else: - file_list = [x.get_name() for x in self.list(path="./")] - if path_split[0] in file_list: - raise FileExistsError(f"Remote folder {path_split[-1]} exist, please check!") + if self.check_remote_path(path=path): + raise FileExistsError(f"mkdir function error: Remote file {path} exist, please check!") return self._make_dav_request('MKCOL', path) @@ -738,6 +741,8 @@ def delete(self, path): :returns: True if the operation succeeded, False otherwise :raises: HTTPResponseError in case an HTTP error status was returned """ + if not self.check_remote_path(path=path): + raise FileNotFoundError(f"delete function error: Remote file {path} not exist, please check!") return self._make_dav_request('DELETE', path) def list_open_remote_share(self): @@ -902,6 +907,8 @@ def share_file_with_link(self, path, **kwargs): or False if the operation failed :raises: HTTPResponseError in case an HTTP error status was returned """ + if not self.check_remote_path(path=path): + raise FileNotFoundError(f"share_file_with_link function error: Remote file {path} not exist, please check!") perms = kwargs.get('perms', None) public_upload = kwargs.get('public_upload', 'false') password = kwargs.get('password', None)