Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repair parameters are empty and increase judgment #283

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions owncloud/owncloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -502,6 +523,9 @@ 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
Expand Down Expand Up @@ -571,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,
Expand Down Expand Up @@ -701,6 +728,10 @@ def mkdir(self, path):
"""
if not path.endswith('/'):
path += '/'

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)

def delete(self, path):
Expand All @@ -710,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):
Expand Down Expand Up @@ -874,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)
Expand Down Expand Up @@ -909,7 +944,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)
Expand Down