fixes #2 – client method upload_file won't work
This commit is contained in:
parent
ffe7d0178d
commit
b749855a80
4 changed files with 51 additions and 25 deletions
|
|
@ -371,7 +371,7 @@ class Client(object):
|
|||
if buff.tell() == 0:
|
||||
data = buff.read()
|
||||
else:
|
||||
data = buff
|
||||
data = buff
|
||||
response = requests.request(
|
||||
'PUT',
|
||||
options["URL"],
|
||||
|
|
@ -388,7 +388,7 @@ class Client(object):
|
|||
if os.path.isdir(local_path):
|
||||
self.upload_directory(local_path=local_path, remote_path=remote_path, progress=progress)
|
||||
else:
|
||||
self.upload_file(local_path=local_path, remote_path=remote_path, progress=progress)
|
||||
self.upload_file(local_path=local_path, remote_path=remote_path)
|
||||
|
||||
def upload_directory(self, remote_path, local_path, progress=None):
|
||||
|
||||
|
|
@ -414,7 +414,7 @@ class Client(object):
|
|||
self.upload(local_path=_local_path, remote_path=_remote_path, progress=progress)
|
||||
|
||||
@wrap_connection_error
|
||||
def upload_file(self, remote_path, local_path, progress=None):
|
||||
def upload_file(self, remote_path, local_path):
|
||||
|
||||
if not os.path.exists(local_path):
|
||||
raise LocalResourceNotFound(local_path)
|
||||
|
|
@ -431,37 +431,22 @@ class Client(object):
|
|||
raise RemoteParentNotFound(urn.path())
|
||||
|
||||
with open(local_path, "rb") as local_file:
|
||||
|
||||
url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()}
|
||||
options = {
|
||||
'URL': "{hostname}{root}{path}".format(**url),
|
||||
'HTTPHEADER': self.get_header('upload_file'),
|
||||
'UPLOAD': 1,
|
||||
'READFUNCTION': local_file.read,
|
||||
# 'NOPROGRESS': 0 if progress else 1
|
||||
}
|
||||
|
||||
# if progress:
|
||||
# options["PROGRESSFUNCTION"] = progress
|
||||
|
||||
file_size = os.path.getsize(local_path)
|
||||
if file_size > self.large_size:
|
||||
options['INFILESIZE_LARGE'] = file_size
|
||||
else:
|
||||
options['INFILESIZE'] = file_size
|
||||
raise ResourceTooBig(path=local_path, size=file_size)
|
||||
|
||||
response = requests.request(
|
||||
'PUT',
|
||||
options["URL"],
|
||||
method='PUT',
|
||||
url="{hostname}{root}{path}".format(**url),
|
||||
auth=(self.webdav.login, self.webdav.password),
|
||||
headers=self.get_header('upload_file'),
|
||||
data=buff
|
||||
data=local_file
|
||||
)
|
||||
if request.status_code == 507:
|
||||
if response.status_code == 507:
|
||||
raise NotEnoughSpace()
|
||||
|
||||
# request.close()
|
||||
|
||||
def upload_sync(self, remote_path, local_path, callback=None):
|
||||
|
||||
self.upload(local_path=local_path, remote_path=remote_path)
|
||||
|
|
|
|||
|
|
@ -13,7 +13,8 @@ class OptionNotValid(NotValid):
|
|||
self.ns = ns
|
||||
|
||||
def __str__(self):
|
||||
return "Option ({ns}{name}={value}) have invalid name or value".format(ns=self.ns, name=self.name, value=self.value)
|
||||
return "Option ({ns}{name}={value}) have invalid name or value".format(ns=self.ns, name=self.name,
|
||||
value=self.value)
|
||||
|
||||
|
||||
class CertificateNotValid(NotValid):
|
||||
|
|
@ -48,6 +49,19 @@ class RemoteParentNotFound(NotFound):
|
|||
return "Remote parent for: {path} not found".format(path=self.path)
|
||||
|
||||
|
||||
class ResourceTooBig(WebDavException):
|
||||
def __init__(self, path, size, max_size):
|
||||
self.path = path
|
||||
self.size = size
|
||||
self.max_size = max_size
|
||||
|
||||
def __str__(self):
|
||||
return "Resource {path} is too big, it should be less then {max_size} but actually: {size}".format(
|
||||
path=self.path,
|
||||
max_size=self.max_size,
|
||||
size=self.size)
|
||||
|
||||
|
||||
class MethodNotSupported(WebDavException):
|
||||
def __init__(self, name, server):
|
||||
self.name = name
|
||||
|
|
@ -70,4 +84,4 @@ class NotEnoughSpace(WebDavException):
|
|||
pass
|
||||
|
||||
def __str__(self):
|
||||
return "Not enough space on the server"
|
||||
return "Not enough space on the server"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue