Uncompress non-tar file

This commit is contained in:
LPOCHOLLE 2021-10-26 10:10:01 +02:00
parent baf09cb8a6
commit 1627254cab
2 changed files with 28 additions and 13 deletions

View File

@ -1,3 +1,3 @@
SELECT name, path, size, m_date, c_date, crypt.id, compress_mode SELECT name, path, size, m_date, c_date, crypt.id, compress_mode
FROM files FROM files
inner join CRYPT on files.crypt_id = crypt.id INNER JOIN crypt ON files.crypt_id = crypt.id

View File

@ -66,7 +66,7 @@ class Backup:
print("Proceed", uri) print("Proceed", uri)
enc = crypt(compress(uri), self.key) enc = crypt(compress(uri), self.key)
print(" Size : ", get_size(enc)) 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: else:
files.append({'name': f, files.append({'name': f,
'path': pathlib.Path(uri).as_posix(), 'path': pathlib.Path(uri).as_posix(),
@ -82,27 +82,27 @@ class Backup:
tarball = tar([file['path'] for file in files]) tarball = tar([file['path'] for file in files])
enc = crypt(compress(tarball), self.key) enc = crypt(compress(tarball), self.key)
print(" Size : ", get_size(enc)) 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()): def recover_file(self, paths, parents=False, save_path=os.getcwd()):
files = self.bdd.get_crypt_file(paths) files = self.bdd.get_crypt_file(paths)
for file in files: for file in files:
if file['crypt'] is not None: 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: if parents:
save_path = os.path.join(save_path, file['path']) 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: if mode == BACKBLAZE:
bucket.upload_bytes(file.read(), file_name) bucket.upload_bytes(file.read(), file_name)
elif mode == LOCAL: elif mode == LOCAL:
save(file, file_name) save(file, file_name)
def get_crypted_file(mode, file, bucket=None): def download_file(mode, file, bucket=None):
dl = tempfile.SpooledTemporaryFile() dl = tempfile.SpooledTemporaryFile()
if mode == BACKBLAZE: if mode == BACKBLAZE:
bucket.download_file_by_name(file).save(dl) bucket.download_file_by_name(file).save(dl)
@ -156,12 +156,21 @@ def compress(file):
return compressed_file return compressed_file
def uncompress(file): def uncompress(data, file_name, save_path, compress_mode):
if type(file) is tempfile.SpooledTemporaryFile: modes = compress_mode.split('.')
file.seek(0) 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() decompressed_file = tempfile.SpooledTemporaryFile()
with gzip.open(file, 'rb') as zipfile: with gzip.open(data, 'rb') as zipfile:
decompressed_file.write(zipfile.read()) decompressed_file.write(zipfile.read())
return decompressed_file return decompressed_file
@ -241,9 +250,15 @@ class DataBase:
# for path in [file['path'] for file in list_file]: # for path in [file['path'] for file in list_file]:
for path in list_file: for path in list_file:
path = pathlib.Path(path).as_posix() 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: 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: except TypeError:
crypt_list.append({'path': path, 'crypt': None}) crypt_list.append({'path': path, 'crypt': None})
return crypt_list return crypt_list