def handle(self, *args, **kwargs): localpath = settings.DATABASES['default']['NAME'] dtstr = datetime.now().strftime(r'%Y-%m-%d') ext = os.path.basename(localpath).split('.')[-1] remotepath = settings.WEBDAV_BACKUP_DIR remotepath += f'/{settings.SITE_NAME}-{settings.HOSTNAME}-{dtstr}.{ext}'.lower() log.info('Backing up database localpath: %s; remotepath: %s', localpath, remotepath) client = Client(settings.WEBDAV_OPTIONS) client.upload_sync(local_path=localpath, remote_path=remotepath)
def put_file(filename_local, filename_remote): from webdav3.client import Client options = { 'webdav_hostname': WEBDAV_HOSTNAME, 'webdav_login': WEBDAV_LOGIN, 'webdav_password': WEBDAV_PASSWORD } print(options) client = Client(options) client.verify = False # To not check SSL certificates (Default = True) client.upload_sync(remote_path="/Documents/esp_frame/%s" % filename_remote, local_path=filename_local)
def upload_webdav(fn, url, login, pwd): logger.debug("Uploading file to webdav") #logger.debug("URL: "+ url) #logger.debug("Filename: "+ fn) options = { 'webdav_hostname': url, 'webdav_login': login, 'webdav_password': pwd } client = Client(options) client.verify = False client.upload_sync(remote_path=basename(fn), local_path=fn) return True
class Dav: """A class to handle webdav request for uploading the mail attachments """ def __init__(self, host, user, password): """initializes a webdav connection Args: host (string): host url user (string): dav user name password (string): dav password """ self.options = { 'webdav_hostname': host, 'webdav_login': user, 'webdav_password': password } self.client=Client(self.options) def uploadAll(self, dir): """Uploads all attachments to the cloud Args: dir (string): existing directory to save the files to Returns: False: if something went wrong """ files = [f for f in listdir("attachments") if isfile(join("attachments", f))] year = datetime.datetime.now().strftime("%Y") date = datetime.datetime.now().strftime("%m-%d") if not year in self.client.list(dir): try: self.client.mkdir(dir+"/"+year) except Exception as e: print(e) return False if not date in self.client.list(dir+"/"+year) and len(files) > 0: try: self.client.mkdir(dir+"/"+year+"/"+date) except Exception as e: print(e) return False for f in files: try: self.client.upload_sync(remote_path=dir+"/"+year+"/"+date+"/"+str(random.randint(10000,100000))+"_"+f, local_path="attachments/"+f) os.remove("attachments/"+f) except Exception as e: print(e)
def salvar(caminho_arquivo, nome_arquivo='UFs.xlsx'): cfg = configparser.ConfigParser(interpolation=None) with io.open(str(config.arquivo_config_webdav), mode='r', encoding='utf-8') as fp: cfg.read_file(fp) options = { 'webdav_hostname': cfg['webdav']['hostname'], 'webdav_login': cfg['webdav']['login'], 'webdav_password': cfg['webdav']['password'] } pasta_virtual = cfg['webdav']['pasta_virtual'] cliente = Client(options) cliente.verify = False cliente.upload_sync(remote_path=pasta_virtual + '/' + nome_arquivo, local_path=caminho_arquivo) print('Upload concluído. Listando conteúdo do diretório remoto...') print(cliente.list(pasta_virtual))
def on_event(self, event, payload): if event == "plugin_backup_backup_created": # Helper function for human readable sizes def _convert_size(size_bytes): if size_bytes == 0: return "0B" size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") i = int(math.floor(math.log(size_bytes, 1024))) p = math.pow(1024, i) s = round(size_bytes / p, 2) return "%s %s" % (s, size_name[i]) now = datetime.now() davoptions = { 'webdav_hostname': self._settings.get(["server"]), 'webdav_login': self._settings.get(["username"]), 'webdav_password': self._settings.get(["password"]), 'webdav_timeout': self._settings.get(["timeout"]), } backup_path = payload["path"] backup_name = payload["name"] self._logger.info("Backup " + backup_path + " created, will now attempt to upload to " + davoptions["webdav_hostname"]) davclient = Client(davoptions) davclient.verify = self._settings.get(["verify_certificate"]) check_space = self._settings.get(["check_space"]) upload_path = now.strftime(self._settings.get(["upload_path"])) upload_path = ospath.join("/", upload_path) if self._settings.get(["upload_name"]): upload_name = now.strftime(self._settings.get( ["upload_name"])) + ospath.splitext(backup_path)[1] else: upload_name = backup_name self._logger.debug("Filename for upload: " + upload_name) upload_file = ospath.join("/", upload_path, upload_name) upload_temp = ospath.join("/", upload_path, upload_name + ".tmp") self._logger.debug("Upload location: " + upload_file) # Check actual connection to the WebDAV server as the check command will not do this. if check_space: self._logger.debug("Attempting to check free space.") try: # If the resource was not found dav_free = davclient.free() if dav_free < 0: # If we get a negative free size, this server is not returning correct value. check_space = False self._logger.warning( "Free space on server: " + _convert_size(dav_free) + ", it appears your server does not support reporting size correctly but it's still a proper way to check connectivity." ) else: self._logger.info("Free space on server: " + _convert_size(dav_free)) except RemoteResourceNotFound as exception: self._logger.error( "Resource was not found, something is probably wrong with your settings." ) return except ResponseErrorCode as exception: # Write error and exit function status = HTTPStatus(exception.code) error_switcher = { 400: "Bad request", 401: "Unauthorized", 403: "Forbidden", 404: "Not found", 405: "Method not allowed", 408: "Request timeout", 500: "Internal error", 501: "Not implemented", 502: "Bad gateway", 503: "Service unavailable", 504: "Gateway timeout", 508: "Loop detected", } if (exception.code == 401): http_error = "HTTP error 401 encountered, your credentials are most likely wrong." else: http_error = "HTTP error encountered: " + str( status.value) + " " + error_switcher.get( exception.code, status.phrase) self._logger.error(http_error) return except WebDavException as exception: self._logger.error( "An unexpected WebDAV error was encountered: " + exception.args) raise else: self._logger.debug( "Not checking free space, just try to check the WebDAV root." ) # Not as proper of a check as retrieving size, but it's something. if davclient.check("/"): self._logger.debug("Server returned WebDAV root.") else: self._logger.error( "Server did not return WebDAV root, something is probably wronkg with your settings." ) return backup_size = ospath.getsize(backup_path) self._logger.info("Backup file size: " + _convert_size(backup_size)) if check_space and (backup_size > dav_free): self._logger.error("Unable to upload, size is" + _convert_size(backup_size) + ", free space is " + _convert_size(dav_free)) return else: # Helper function to recursively create paths def _recursive_create_path(path): # Append leading / for preventing abspath issues path = ospath.join("/", path) if davclient.check(path): self._logger.debug("Directory " + path + " was found.") return True else: if path != "/": self._logger.debug( "Directory " + path + " was not found, checking parent.") if _recursive_create_path( ospath.abspath(ospath.join(path, ".."))): davclient.mkdir(path) self._logger.debug("Directory " + path + " has been created.") return True else: self._logger.error( "Could not find WebDAV root, something is probably wrong with your settings." ) return False if _recursive_create_path(upload_path): self._logger.debug("Uploading " + backup_path + " to " + upload_temp) davclient.upload_sync(remote_path=upload_temp, local_path=backup_path) self._logger.debug("Moving " + upload_temp + " to " + upload_file) davclient.move(remote_path_from=upload_temp, remote_path_to=upload_file) self._logger.info( "Backup has been uploaded successfully to " + davoptions["webdav_hostname"] + " as " + upload_file) else: self._logger.error( "Something went wrong trying to check/create the upload path." )
def test_webdav(self): server_2_ip = '172.16.3.155' error, server_id = self.mm['webdavalpine'].run("add_server", ip_addr=server_2_ip, fqdn='webdav.test') self.assertTrue(error == None, msg=error) time.sleep(10) webdav_url = "https://{}/files/".format(server_2_ip) full_path = parentdir + "/work/webdavalpine/1/webdav/admin.pass" self.assertTrue(os.path.exists(full_path), msg="{} does not exist".format(full_path)) admin_pass = open(full_path, "r").read().strip() resp = requests.get("https://{}/".format(server_2_ip), verify=False) self.assertTrue(resp.status_code == 200) options = { 'webdav_hostname': webdav_url, 'webdav_login': "******", 'webdav_password': admin_pass } client = Client(options) client.verify = False result = client.check("public") self.assertTrue(result == True) result = client.list() self.assertTrue(len(result) > 0) subprocess.check_output(["touch /tmp/testdata"], shell=True) client.upload_sync("public/testdata", "/tmp/testdata") result = client.check("public/testdata") self.assertTrue(result == True) unauth_client = Client({ 'webdav_hostname': webdav_url }) unauth_client.verify = False public_list = unauth_client.list("public/") self.assertTrue(len(public_list) > 0) try: unauth_client.upload_sync("public/testdata2", "/tmp/testdata") self.fail() except: pass error, _ = self.mm['webdavalpine'].run("stop_server", id=server_id) self.assertTrue(error == None, msg=error) time.sleep(2) error, _ = self.mm['webdavalpine'].run("start_server", id=server_id) self.assertTrue(error == None, msg=error) time.sleep(10) resp = requests.get("https://{}/".format(server_2_ip), verify=False) self.assertTrue(resp.status_code == 200) options = { 'webdav_hostname': webdav_url, 'webdav_login': "******", 'webdav_password': admin_pass } client2 = Client(options) client2.verify = False public_list = client2.list("public/") self.assertTrue(len(public_list) > 0) self.assertTrue(public_list[1] == "testdata") error, _ = self.mm['webdavalpine'].run("remove_server", id=server_id) self.assertTrue(error == None, msg=error)
date.strftime("%Y") + '_год', date.strftime("%m") + '_месяц', date.strftime('%m_%d') ] rem_path = '' for folder in rem_folders: rem_path += folder + '/' checkpath(rem_path) video_path = r'./' files = os.listdir(video_path) free_space() for file in files: if 'Video' in file: print('File {} is uploading now. Please, wait!!!'.format(file)) client.upload_sync(remote_path=rem_path, local_path=video_path) print('Uploading of {} is finished.'.format(file)) print('Uploading of all files is finished.') free_space() opts = Options() opts.add_argument('--headless') opts.add_argument('--ignore-certificate-errors') print( 'Driver is starting now .........................................................' ) print( "Please wait, don't close windows! .............................................." )
class Webdav_sync(Sync): def __init__(self, user, password, url, db_file): super().__init__() log.info("We are in Webdav_sync.__init__") if user == '' or password == '' or url == '': log.error("Webdav config incomplete. Check config.yaml") raise SystemExit else: self.user = user self.password = password self.url = url self.discobase = db_file #self.backuppath = '/discodos/{}'.format(db_file) options = { 'webdav_hostname': self.url, 'webdav_login': self.user, 'webdav_password': self.password } self.client = Client(options) #print(dir(self.client)) #print('') #print(self.client.is_dir('discodos')) #print(self.client.check(self.discobase)) def _webdav_mtime( self, filename): # we currently don't need this, put to func anyway mod_server_dt = parse(self.client.info(filename)['modified']) mod_server_str = mod_server_dt.strftime('%Y-%m-%d_%H%M%S') #if mod_local_str != mod_server_str: # print('Local and server discobase.db modification time diverge.') # print(mod_local_str) # print(mod_server_str) return mod_server_str def backup(self): # check file stats on local machine bak_file_name = self._get_fileobj_mtime(self.discobase) print("Uploading as {} to {}".format(bak_file_name, self.url)) existing = False try: if self.client.check(bak_file_name): existing = True else: existing = False except WebDavException as exception: log.error('Webserver returned: {}'.format(exception)) raise SystemExit if existing: log.warning('Backup existing. Won\'t overwrite "{}" '.format( bak_file_name)) else: print('Backup not existing yet, uploading ...') self.client.upload_sync(remote_path='{}'.format(bak_file_name), local_path='{}'.format(self.discobase)) # in any case, show list of existing backups self.show_backups() return True def show_backups(self, restore=False): if not restore: print('\nExisting backups:') #relevant_files = self.client.list()[1:] # leave out first item, it's the containing folder all_files = self.client.list() all_files.sort() # sorts by name relevant_files = [] for i, resource in enumerate(all_files): if re.search('_(\d+)-(\d+)-(\d+)_(\d+)$', resource): relevant_files.append(resource) else: log.debug('Sync: Skipping resource: {}'.format(all_files[i])) for j, file in enumerate(relevant_files): file = '({}) - {}'.format(j, file) print(file) if restore: restore_id = ask_user('Restore backup #: ') try: restore_file = relevant_files[int(restore_id)] except ValueError: log.warning('Nothing to restore!') raise SystemExit except IndexError: log.warning('Non-existent ID. Nothing to restore!') raise SystemExit print('Restoring backup {}...'.format(restore_file)) return restore_file print() def restore(self): print('\nWhich backup would you like to restore?') restore_filename = self.show_backups(restore=True) overwrite = ask_user( "Download backup and overwrite local file {} (n)? ".format( self.discobase)) if overwrite.lower() == 'y': self.client.download_sync( remote_path='{}'.format(restore_filename), local_path='{}'.format(self.discobase)) self._touch_to_backupdate(restore_filename)
event = ics.Event() event.name = task['name'] event.begin = task['due'] if 'note' in task and task['note'] is not None: event.description = xmltodict.unparse(task['note'], full_document=False) if 'estimated-minutes' in task and task['estimated-minutes'] is not None: event.duration = datetime.timedelta(minutes = abs(int(task['estimated-minutes']))) event.url = 'omnifocus:///task/{}'.format(task['@id']) event.alarms = get_alarms(task) calendar.events.add(event) if should_log: pprint(event) if should_log: pprint(calendar) # write calendar log('writing calendar to file') with open(CAL_FILE, 'w') as fd: fd.writelines(calendar) # upload files log('uploading calendar to webdav') if client is not None: client.upload_sync(local_path=CAL_FILE, remote_path=add_basepath(CAL_FILE)) # cleanup log('cleaning up') os.remove(CONTENTS_FILE) os.remove('encrypted') os.remove(dec_contents_zip) os.remove(contents_zip) # os.remove('CAL_FILE')
class WebWav: #------------------------------------------------------- # 定数 TEMPWAV = 'TempWav/' # LOCAL端末FTPアップロード対象音声フォルダ TEMPTXT = 'TempTxt/' # LOCAL端末FTPダウンロードフォルダ UPPATH = 'uploads/' # WebWav音声アップロードフォルダ DOWNPATH = 'auto_uploads/' # WebWavテキストダウンロードフォルダ BACKUPPATH = 'BackUp/' # バックアップフォルダ #------------------------------------------------------- # コンストラクタ def __init__(self): #------------------------------------------------------- # WebWavの接続情報 options = { 'webdav_hostname': "https://seto.teracloud.jp/dav/", 'webdav_login': "******", 'webdav_password': "******" } #------------------------------------------------------- # WebWavクライアント self.client = Client(options) self.client.verify = True # To not check SSL certificates (Default = True) #------------------------------------------------------- # 1.AmiVoiceクラウド環境で過去にアップロードされた音声ファイルの削除する。 #------------------------------------------------------- def delWavFile(self): print('---START:delWavFile---') # 削除前にファイル出力 print('AMIVOICE OLD FILE DEL BEFORE') delFilelist = self.client.list(self.UPPATH) print(delFilelist) # uploadフォルダ内、ファイルの削除 for file in delFilelist: if file == self.UPPATH: # ディレクトリ名と一致する場合、次へ (listでディレクトリ名も取得されるので回避) continue delFile = self.UPPATH + file print('DELFILE:' + delFile) self.client.clean(delFile) print('AMIVOICE OLD FILE DEL CHECK List:') print(self.client.list(self.UPPATH)) print('---END:delWavFile---') #------------------------------------------------------- # 2.ローカル端末内の転送用フォルダ(TempWav)内の音声ファイルをWebWavでアップロードする。 #------------------------------------------------------- def uploadWavFile(self): print('---START:uploadWavFile---') # WebWavアップロード対象のファイルを取得 upFileList = os.listdir(self.TEMPWAV) print('LOCAL TempWav List:') print(upFileList) for file in upFileList: print('AMIVOICE UPLOAD FILE:%s' % file) upFile = self.UPPATH + file localUpFile = self.TEMPWAV + file self.client.upload_sync(remote_path=upFile, local_path=localUpFile) print('---END:uploadWavFile---') #------------------------------------------------------- # 3.アップロード後の音声ファイルを転送用フォルダ(TempWav)から削除する。 #------------------------------------------------------- def delLocalWavFile(self): print('---START:delLocalWavFile---') # アップロード終了後、実施 localFileList = os.listdir(self.TEMPWAV) for file in localFileList: print('LOCAL DEL FILE:%s' % file) delfile = self.TEMPWAV + file os.remove(delfile) print('LOCAL TempWav CHECK List:') print(os.listdir(self.TEMPWAV)) print('---END:delLocalWavFile---') #------------------------------------------------------- # 4.音声ファイルから変換されたテキストデータをWebWavで転送用フォルダ(TempTxt)にダウンロードする。 #------------------------------------------------------- def downloadTxtFile(self): print('---START:downloadTxtFile---') downFileList = self.client.list(self.DOWNPATH) print('AMIVOICE DOWNLOAD ') print(downFileList) for file in downFileList: if file == self.DOWNPATH: # ディレクトリ名と一致する場合、次へ (listでディレクトリ名も取得されるので回避) continue downFile = self.DOWNPATH + file localDownFile = self.TEMPTXT + file self.client.download_sync(remote_path=downFile, local_path=localDownFile) print('LOCAL TempTxt CHECK List:') print(os.listdir(self.TEMPTXT)) print('---END:downloadTxtFile---') #------------------------------------------------------- # 5.音声ファイルから変換されたテキストデータを日付単位フォルダにバックアップする。 #------------------------------------------------------- def BackUpTxtFile(self): print('---START:BackUpTxtFile---') # システム日付より、今日日付を取得today = datetime.date.today() BackUpFolder = self.BACKUPPATH + datetime.date.today().strftime( "%Y-%m-%d") print('BACKUP FOLDEPATH:' + BackUpFolder) # 今日日付のディレクトリない場合 if not os.path.exists(BackUpFolder): # フォルダを作成 os.mkdir(BackUpFolder) # 取得したデータのコピーし、バックアップを作成する。 # バックアップ for file in os.listdir(self.TEMPTXT): shutil.copy2(self.TEMPTXT + file, BackUpFolder + '/' + file) print('LOCAL BACKUP FOLDER List:') print(os.listdir(BackUpFolder)) print('---END:BackUpTxtFile---')
class WebDAV: """Commerce Cloud WebDAV session. Args: client (CommerceCloudClientSession): Active client session with Commerce Cloud for a bearer token. instance (str, optional): Optional commerce cloud instance, useful for opening clients to multiple instances using the same bearer token. Defaults to None. cert (str, optional): Path to TLS client certificate. Defaults to None. key ([type], optional): Export key for the TLS certificate. Defaults to None. verify (bool, optional): Verify TLS certificates, set to false for self signed. Defaults to True. """ def __init__(self, client, instance=None, cert=None, key=None, verify=True): self.client = client self._instance = instance or self.client.instance self.options = {"webdav_hostname": self._instance.rstrip("/")} self.verify = verify self.token = self.client.Token self.options.update({"webdav_token": self.token["access_token"]}) self.webdav_client = Client(self.options) self.webdav_client.verify = self.verify if cert and key: self.cert = str(Path(cert).resolve()) self.key = str(Path(key).resolve()) self.options.update({"cert_path": self.cert, "key_path": self.key}) def reauth(self): """Checks token expiry and re-initialises the Client if a new token is needed. """ if self.token["expires_at"] < int(time.time()): self.client.getToken() self.options.update({"webdav_token": self.token["access_token"]}) self.webdav_client = Client(self.options) def reconnect(self): """Re-initalise the Client session. """ self.webdav_client = Client(self.options) @property def hostname(self): """Return the hostname the WebDAV client connection is connected to. Returns: str: Hostname including prefix eg https:// """ return self.options["webdav_hostname"] @property def netloc(self): """Return a urlparse netloc string of the connected hostname. Returns: str: netloc of hostname. """ url = urlparse(self.options["webdav_hostname"]) return url.netloc @retry( retry_on_exceptions=(RetryException), max_calls_total=3, retry_window_after_first_call_in_seconds=10, ) def GetInfo(self, remote_filepath: str, headers: dict = None) -> list: """Get properties for entity [extended_summary] Args: remote_filepath (str): Path to remote resource. headers (dict, optional): Additional headers to apply to request. Defaults to None. Raises: RetryException: Adds to retries counter on failure. Returns: list: WebDAV attribute information. """ try: return self.webdav_client.info(remote_filepath) except (NoConnection, ConnectionException, WebDavException): self.reauth() raise RetryException @retry( retry_on_exceptions=(RetryException), max_calls_total=3, retry_window_after_first_call_in_seconds=10, ) def GetDirectoryList(self, filepath: str, get_info: bool = False, headers: dict = None) -> list: """Get list of files and folders in a path from WebDAV endpoint. [extended_summary] Args: filepath (str): Path to get directory listing for. get_info (bool): returns dictionary of attributes instead of file list. headers (dict, optional): Additional headers to apply to request. Defaults to None. Returns: list: Directory listing. """ try: return self.webdav_client.list(filepath, get_info=get_info) except (NoConnection, ConnectionException, WebDavException): self.reauth() raise RetryException @retry( retry_on_exceptions=(RetryException), max_calls_total=3, retry_window_after_first_call_in_seconds=10, ) def Upload(self, local_filepath: str, remote_filepath: str): """Upload file or directory recursively to WebDAV endpoint. [extended_summary] Args: local_filepath (str): Local path to file or directory to upload. remote_filepath (str): Remote path to upload to. """ local_filepath = str(Path(local_filepath).resolve()) try: self.webdav_client.upload_sync(remote_filepath, local_filepath) except (NoConnection, ConnectionException, WebDavException): self.reauth() raise RetryException @retry( retry_on_exceptions=(RetryException), max_calls_total=3, retry_window_after_first_call_in_seconds=10, ) def StreamUpload(self, payload, remote_path: str, file_name: str): """Upload FileIO, StringIO, BytesIO or string to WebDAV [extended_summary] Args: payload: Stream payload remote_path (str): Remote path relative to host. file_name (str): Name for the file uploaded. """ try: self.webdav_client.upload_to(payload, f"{remote_path}/{file_name}") except (NoConnection, ConnectionException, WebDavException): self.reauth() raise RetryException @retry( retry_on_exceptions=(RetryException), max_calls_total=3, retry_window_after_first_call_in_seconds=10, ) def MakeDir(self, remote_path: str): """Make new directory at path specified. Args: remote_path (str): Path of proposed new directory. """ try: self.webdav_client.mkdir(remote_path) except (NoConnection, ConnectionException, WebDavException): self.reauth() raise RetryException @retry( retry_on_exceptions=(RetryException), max_calls_total=3, retry_window_after_first_call_in_seconds=10, ) def Move(self, remote_path_source: str, remote_path_dest: str, overwrite: bool = False): """Make new directory at path specified. Args: remote_path_source (str): Path of source resource. remote_path_dest (str): Path of destination resource. overwrite (bool): Overwrite destination resource. Defaults to False. """ try: self.webdav_client.move(remote_path_source, remote_path_dest, overwrite) except (NoConnection, ConnectionException, WebDavException): self.reauth() raise RetryException @retry( retry_on_exceptions=(RetryException), max_calls_total=3, retry_window_after_first_call_in_seconds=10, ) def Delete(self, remote_filepath: str): """Delete file on remote WebDAV endpoint. Args: remote_filepath (str): Location of resource to delete. """ try: self.webdav_client.clean(remote_filepath) except (NoConnection, ConnectionException, WebDavException): self.reauth() raise RetryException @retry( retry_on_exceptions=(RetryException), max_calls_total=3, retry_window_after_first_call_in_seconds=10, ) def Download(self, local_filepath: str, remote_filepath: str): """Download file/folder from WebDAV endpoint. This is a synchronous operation, and the file is downloaded in full to the local_filepath. Args: local_filepath (str): Local path to download to, including filename of file saved. remote_filepath (str): Remote path to file to download. """ local_filepath = str(Path(local_filepath).resolve()) try: self.webdav_client.download_sync(remote_filepath, local_filepath) except (NoConnection, ConnectionException, WebDavException): self.reauth() raise RetryException @retry( retry_on_exceptions=(RetryException), max_calls_total=3, retry_window_after_first_call_in_seconds=10, ) def Pull(self, local_filepath: str, remote_filepath: str): """Sync file/folder from WebDAV endpoint to local storage. This downloads missing or nwer modified files from the remote to local storage. You can use it to do "resumeable" transfers, but the checks are slow for deeply nested files. Args: local_filepath (str): Local path to download to, including filename of file saved. remote_filepath (str): Remote path to file to download. """ local_filepath = str(Path(local_filepath).resolve()) try: self.webdav_client.pull(remote_filepath, local_filepath) return True except (NoConnection, ConnectionException, WebDavException): self.reauth() raise RetryException return False @retry( retry_on_exceptions=(RetryException), max_calls_total=3, retry_window_after_first_call_in_seconds=10, ) def Push(self, local_filepath: str, remote_filepath: str): """Sync file/folder from local storage to WebDAV endpoint. This uploads missing or nwer modified files from the local to remote storage. You can use it to do "resumeable" transfers, but the checks are slow for deeply nested files. Args: local_filepath (str): Local path to download to, including filename of file saved. remote_filepath (str): Remote path to file to download. """ local_filepath = str(Path(local_filepath).resolve()) try: self.webdav_client.push(local_filepath, remote_filepath) return True except (NoConnection, ConnectionException, WebDavException): self.reauth() raise RetryException return False @retry( retry_on_exceptions=(RetryException), max_calls_total=10, retry_window_after_first_call_in_seconds=15, ) def StreamDownload(self, remote_filepath: str, buffer=None, decode: bool = False): """Download a file in chunks to a local file buffer. You must provide a BytesIO object or one will be created for you. Args: remote_filepath (str): Path to remote resource to download. buffer ([type], optional): Buffer write streamed content to. decode (bool, optional): Optionally try to decode downloaded file into a string. Defaults to False. Raises: RetryException: Adds to retries counter on failure. Returns: Bytes: Returns a BytesIO object for further use. """ self.reauth() if buffer is None: buffer = BytesIO() try: self.webdav_client.download_from(buff=buffer, remote_path=remote_filepath) if decode is True: return buffer.getvalue().decode("utf-8") else: buffer.seek(0) return buffer except (NoConnection, ConnectionException, WebDavException): raise RetryException @retry( retry_on_exceptions=(RetryException), max_calls_total=10, retry_window_after_first_call_in_seconds=60, ) def HashObject(self, remote_filepath: str) -> str: """Generate a MD5 hashsum for a remote resource. This is streamed into memory, hashed and discarded. Optimised for low memory but high bandwidth environments. Args: remote_filepath (str): Path to remote resource. Raises: RetryException: Adds to retries counter on failure. Returns: str: MDSSUM of the file requested. """ self.reauth() try: sum = md5(self.StreamDownload(remote_filepath).getbuffer()) return { "filepath": remote_filepath, "hashtype": "MD5", "hashsum": sum.hexdigest(), } except (NoConnection, ConnectionException, WebDavException): self.reconnect() raise RetryException def RecursiveFileListing(self, remote_filepath: str) -> str: """Recursive filetree walker, returns paths found. Args: remote_filepath (str): [description] Raises: RetryException: Adds to retries counter on failure. Yields: Iterator[str]: Yields resource paths for any files found. """ @retry( retry_on_exceptions=(RetryException), max_calls_total=10, retry_window_after_first_call_in_seconds=60, ) def get_list(self, path): self.reauth() try: return self.webdav_client.list(path, get_info=True) except (NoConnection, ConnectionException, WebDavException): self.reconnect() raise RetryException def get_files(self, path): return [x for x in get_list(self, path) if x["isdir"] is False] def get_dirs(self, path): return [ x["path"] for x in get_list(self, path) if x["isdir"] is True ] yield from get_files(self, remote_filepath) for subdir in get_dirs(self, remote_filepath): yield from self.RecursiveFileListing(subdir) def RecursiveFolderListing(self, remote_filepath: str) -> str: """Recursive filetree walker, returns paths found. Args: remote_filepath (str): [description] Raises: RetryException: Adds to retries counter on failure. Yields: Iterator[str]: Yields resource paths for any files found. """ @retry( retry_on_exceptions=(RetryException), max_calls_total=10, retry_window_after_first_call_in_seconds=60, ) def get_list(self, path): self.reauth() try: return self.webdav_client.list(path, get_info=True) except (NoConnection, ConnectionException, WebDavException): self.reconnect() raise RetryException def get_dirs(self, path): return [ x["path"] for x in get_list(self, path) if x["isdir"] is True ] dirlist = get_dirs(self, remote_filepath) yield from dirlist for subdir in get_dirs(self, remote_filepath): yield from self.RecursiveFolderListing(subdir)
def upload_dav(): try: options = { 'webdav_hostname': "https://192.168.1.6/remote.php/dav/files/clouduser/", 'webdav_login': "******", 'webdav_password': "******", 'verbose':True } conn=False client = Client(options) client.verify = False try: if(client.list()): print('\n' * 4) print("Connection Successfull") conn=True print('\n' * 3) except: print('\n' * 4) print("Connection error. Check credentials") if conn: print("##############################################################") opt=int(input("Choose action to be performed:\n1:Show all files\n2:Make Directory\n3:Delete\n4:Download file\n5:Upload File\n")) if opt==1: files=client.list() print(files) elif opt==2: d=input("Enter directory name\n") if client.mkdir(d): print("Created Successfully") elif opt==3: d=input("Enter directory or file name\n") client.clean(d) print("Deleted") elif opt==4: file=input("Enter file to download with public key and signature\n") path_1=file+'.encrypted' path_2=file+'.sign' path_3='public_key.pem' s_time=time.time() client.download_sync(path_1, path_1) client.download_sync(path_2, path_2) client.download_sync(path_3, path_3) e_time=time.time() t=e_time-s_time print("Downloaded, Time taken is",e_time-s_time) elif opt==5: file=input("Enter file to upload with public key and signature\n") path_1=file+'.encrypted' path_2=file+'.sign' path_3='public_key.pem' s_time=time.time() client.upload_sync(path_1, path_1) client.upload_sync(path_2, path_2) client.upload_sync(path_3, path_3) e_time=time.time() print("Uplaoded, Time taken is",e_time-s_time) else: None except WebDavException as exception: print("\n\n",exception,"\n\n")