From 1627254cab029a126eed8ba8b3617a3959950489 Mon Sep 17 00:00:00 2001 From: LPOCHOLLE <> Date: Tue, 26 Oct 2021 10:10:01 +0200 Subject: [PATCH] Uncompress non-tar file --- SELECT.sql | 2 +- backup.py | 39 +++++++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/SELECT.sql b/SELECT.sql index 2ea38ed..1dfbb86 100644 --- a/SELECT.sql +++ b/SELECT.sql @@ -1,3 +1,3 @@ SELECT name, path, size, m_date, c_date, crypt.id, compress_mode FROM files -inner join CRYPT on files.crypt_id = crypt.id \ No newline at end of file +INNER JOIN crypt ON files.crypt_id = crypt.id \ No newline at end of file diff --git a/backup.py b/backup.py index ce08708..32c0328 100644 --- a/backup.py +++ b/backup.py @@ -66,7 +66,7 @@ class Backup: print("Proceed", uri) enc = crypt(compress(uri), self.key) print(" Size : ", get_size(enc)) - save_crypted_file(enc, self.save_mode, file_name=os.path.join(self.save_location, crypt_name), bucket=self.buk) + upload_file(enc, self.save_mode, file_name=os.path.join(self.save_location, crypt_name), bucket=self.buk) else: files.append({'name': f, 'path': pathlib.Path(uri).as_posix(), @@ -82,27 +82,27 @@ class Backup: tarball = tar([file['path'] for file in files]) enc = crypt(compress(tarball), self.key) print(" Size : ", get_size(enc)) - save_crypted_file(enc, self.save_mode, file_name=os.path.join(self.save_location, crypt_name), bucket=self.buk) + upload_file(enc, self.save_mode, file_name=os.path.join(self.save_location, crypt_name), bucket=self.buk) def recover_file(self, paths, parents=False, save_path=os.getcwd()): files = self.bdd.get_crypt_file(paths) for file in files: if file['crypt'] is not None: - crypted_file = get_crypted_file(self.save_mode, os.path.join(self.save_location, file['crypt']), bucket=self.buk) + encrypted_file = download_file(self.save_mode, os.path.join(self.save_location, file['crypt']), bucket=self.buk) if parents: save_path = os.path.join(save_path, file['path']) - untar(uncompress(uncrypt(crypted_file, self.key)), file['name'], save_path) + uncompress(uncrypt(encrypted_file, self.key), file['name'], save_path, file['compress_mode']) -def save_crypted_file(file, mode, file_name, bucket=None): +def upload_file(file, mode, file_name, bucket=None): if mode == BACKBLAZE: bucket.upload_bytes(file.read(), file_name) elif mode == LOCAL: save(file, file_name) -def get_crypted_file(mode, file, bucket=None): +def download_file(mode, file, bucket=None): dl = tempfile.SpooledTemporaryFile() if mode == BACKBLAZE: bucket.download_file_by_name(file).save(dl) @@ -156,12 +156,21 @@ def compress(file): return compressed_file -def uncompress(file): - if type(file) is tempfile.SpooledTemporaryFile: - file.seek(0) +def uncompress(data, file_name, save_path, compress_mode): + modes = compress_mode.split('.') + if 'gz' in modes: + data = ungz(data) + if 'tar' in modes: + untar(data, file_name, save_path) + else: + save(data, os.path.join(save_path, file_name)) + +def ungz(data): + if type(data) is tempfile.SpooledTemporaryFile: + data.seek(0) decompressed_file = tempfile.SpooledTemporaryFile() - with gzip.open(file, 'rb') as zipfile: + with gzip.open(data, 'rb') as zipfile: decompressed_file.write(zipfile.read()) return decompressed_file @@ -241,9 +250,15 @@ class DataBase: # for path in [file['path'] for file in list_file]: for path in list_file: path = pathlib.Path(path).as_posix() - cursor.execute("""SELECT crypt_id FROM files WHERE path=?""", (path,)) + cursor.execute("""SELECT crypt_id, compress_mode FROM files + INNER JOIN crypt ON files.crypt_id = crypt.id + WHERE path=?""", (path,)) + retval = cursor.fetchone() try: - crypt_list.append({'name': os.path.basename(path),'path': path, 'crypt': str(cursor.fetchone()['crypt_id']).zfill(ZFILL)}) + crypt_list.append({'name': os.path.basename(path), + 'path': path, + 'crypt': str(retval['crypt_id']).zfill(ZFILL), + 'compress_mode': retval['compress_mode']}) except TypeError: crypt_list.append({'path': path, 'crypt': None}) return crypt_list