From 402f54f9c6d8b1e13b692ab31254fd7b2596b8ac Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Sun, 10 Dec 2017 14:35:03 -0600 Subject: [PATCH] =?UTF-8?q?Sincrionizacion=20SeaFile=20part=C3=ADcular?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/app/controllers/helper.py | 70 +++++++++++++++++++++++++++++--- source/app/controllers/util.py | 30 +++++++------- 2 files changed, 81 insertions(+), 19 deletions(-) diff --git a/source/app/controllers/helper.py b/source/app/controllers/helper.py index 16875f7..64bda8b 100644 --- a/source/app/controllers/helper.py +++ b/source/app/controllers/helper.py @@ -797,7 +797,7 @@ class SeaFileAPI(object): return True - def upload_file(self, path_file, repo_id, password): + def upload_file(self, path_file, repo_id, relative_path, password=''): if not self._headers: return False @@ -808,7 +808,7 @@ class SeaFileAPI(object): upload_link = self._get_upload_link(repo_id) data = { 'filename': path_file, - 'parent_dir': '/', + 'parent_dir': relative_path, 'relative_path': '', } files = {'file': self._open(path_file)} @@ -826,10 +826,67 @@ class SeaFileAPI(object): path, filename = os.path.split(path) return path, filename - def update_file(self, path_file, repo_id, password, create=True): + def list_directory(self, repo_id): if not self._headers: return False + url = '{}repos/{}/dir/'.format(self._url, repo_id) + params = {'p': '/', 't': 'd', 'recursive': '1'} + try: + resp = requests.get(url, params=params, headers=self._headers) + return resp.json() + except Exception as e: + return False + + def create_dir(self, repo_id, name): + url = '{}repos/{}/dir/'.format(self._url, repo_id) + data = {'operation': 'mkdir'} + params = {'p': name} + resp = requests.post(url, data=data, headers=self._headers, params=params) + if resp.status_code != requests.codes.created: + msg = 'Create Dir Error: {}'.format(resp.status_code) + print (msg) + return False + + return True + + def _validate_directory(self, repo_id, target_file): + if target_file == '/': + return True + + names = target_file.split('/')[:-1] + directories = self.list_directory(repo_id) + + if isinstance(directories, bool): + return False + + exists = False + parent_dir = '/' + name_dir = '' + for name in names: + name_dir += '/' + name + for directory in directories: + if name == directory['name'] and parent_dir == directory['parent_dir']: + exists = True + break + if exists: + exists = False + else: + self.create_dir(repo_id, name_dir) + if parent_dir == '/': + parent_dir += name + else: + parent_dir += '/' + name + + return True + + def update_file(self, path_file, repo_id, target_file, password, create=True): + if not self._headers: + return False + + if not self._validate_directory(repo_id, target_file): + return False + if password: if not self._decrypt(repo_id, password): return False @@ -839,7 +896,7 @@ class SeaFileAPI(object): files = { 'file': (filename, self._open(path_file)), 'filename': (None, filename), - 'target_file': (None, '/{}'.format(filename)) + 'target_file': (None, '{}{}'.format(target_file, filename)) } resp = requests.post(update_link, files=files, headers=self._headers) @@ -847,7 +904,10 @@ class SeaFileAPI(object): msg = 'Update Code: {}\n{}'.format(resp.status_code, resp.text) print (msg) if resp.status_code == self.FILE_DOES_NOT_EXIST and create: - return self.upload_file(path_file, repo_id, password) + relative_path = '/' + if target_file != '/': + relative_path += target_file + return self.upload_file(path_file, repo_id, relative_path, password) else: return False diff --git a/source/app/controllers/util.py b/source/app/controllers/util.py index 9162630..7f5c5c0 100644 --- a/source/app/controllers/util.py +++ b/source/app/controllers/util.py @@ -1401,25 +1401,27 @@ def _to_seafile(path_db, data): msg = '\tSincronizando: {} '.format(filename) log.info(msg) seafile.update_file( - path_db, SEAFILE_SERVER['REPO'], SEAFILE_SERVER['PASS']) + path_db, SEAFILE_SERVER['REPO'], '/', SEAFILE_SERVER['PASS']) msg = '\tRespaldo general de {} sincronizado'.format(filename) log.info(msg) + msg = '\tSin datos para sincronización particular de {}'.format(filename) if len(data) < 2: + log.info(msg) return - if not data[0] or not data[1]: + if not data[0] or not data[1] or not data[2]: + log.info(msg) return - # ~ msg = '\tSincronizando backup particular...' - # ~ log.info(msg) - # ~ seafile = SeaFileAPI(SEAFILE_SERVER['URL'], data[0], data[1]) - # ~ if seafile.is_connect: - # ~ msg = '\tSincronizando: {} '.format(filename) - # ~ log.info(msg) - # ~ seafile.update_file( - # ~ path_db, SEAFILE_SERVER['REPO'], data[1]) - # ~ msg = '\tRespaldo partícular de {} sincronizado'.format(filename) - # ~ log.info(msg) + msg = '\tSincronizando backup particular...' + log.info(msg) + seafile = SeaFileAPI(SEAFILE_SERVER['URL'], data[0], data[1]) + if seafile.is_connect: + msg = '\t\tSincronizando: {} '.format(filename) + log.info(msg) + seafile.update_file(path_db, data[2], 'Base de datos/', data[1]) + msg = '\t\tRespaldo partícular de {} sincronizado'.format(filename) + log.info(msg) return @@ -1429,7 +1431,7 @@ def _backup_and_sync(rfc, data): msg = 'Generando backup de: {}'.format(rfc) log.info(msg) - sql = 'select correo_timbrado, token_timbrado from emisor;' + sql = 'select correo_timbrado, token_timbrado, token_soporte from emisor;' path_bk = _join(PATH_MEDIA, 'tmp', '{}.bk'.format(rfc.lower())) if data['type'] == 'postgres': args = 'pg_dump -U postgres -Fc {} > "{}"'.format( @@ -1440,7 +1442,7 @@ def _backup_and_sync(rfc, data): sql = 'sqlite3 "{}" "{}"'.format(data['name'], sql) try: result = _call(args) - msg = '\tBackup generado...' + msg = '\tBackup generado de {}'.format(rfc) log.info(msg) result = _call(sql).strip().split('|') _to_seafile(path_bk, result)