def upload_from(self, buff, remote_path): urn = Urn(remote_path) if urn.is_dir(): raise OptionNotValid(name="remote_path", value=remote_path) if not self.check(urn.parent()): raise RemoteParentNotFound(urn.path()) url = { 'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote() } options = { 'URL': "{hostname}{root}{path}".format(**url), } # import ipdb; ipdb.set_trace(); #FIXME if buff.tell() == 0: data = buff.read() else: data = buff response = requests.request('PUT', options["URL"], auth=(self.webdav.login, self.webdav.password), headers=self.get_header('upload_from'), data=data) if response.status_code == 507: raise NotEnoughSpace() log.debug("Response: %s", response)
def upload_directory(self, remote_path, local_path, progress=None): urn = Urn(remote_path, directory=True) if not urn.is_dir(): raise OptionNotValid(name="remote_path", value=remote_path) if not os.path.isdir(local_path): raise OptionNotValid(name="local_path", value=local_path) if not os.path.exists(local_path): raise LocalResourceNotFound(local_path) if self.check(urn.path()): self.clean(urn.path()) self.mkdir(remote_path) for resource_name in listdir(local_path): _remote_path = "{parent}{name}".format(parent=urn.path(), name=resource_name) _local_path = os.path.join(local_path, resource_name) self.upload(local_path=_local_path, remote_path=_remote_path, progress=progress)
def upload_file(self, remote_path, local_path, progress=None): if not os.path.exists(local_path): raise LocalResourceNotFound(local_path) urn = Urn(remote_path) if urn.is_dir(): raise OptionNotValid(name="remote_path", value=remote_path) if os.path.isdir(local_path): raise OptionNotValid(name="local_path", value=local_path) if not self.check(urn.parent()): 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 response = requests.request('PUT', options["URL"], auth=(self.webdav.login, self.webdav.password), headers=self.get_header('upload_file'), data=buff) if request.status_code == 507: raise NotEnoughSpace() if response.status_code == 409: raise Conflict()