Uncompress non-tar file
This commit is contained in:
parent
baf09cb8a6
commit
1627254cab
@ -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
|
||||
INNER JOIN crypt ON files.crypt_id = crypt.id
|
39
backup.py
39
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user