upload/download in class

This commit is contained in:
toto 2021-11-02 14:43:45 +01:00
parent 1cc0e98de3
commit f84e226770

View File

@ -68,7 +68,7 @@ class Backup:
enc = crypt(compress(uri), self.key)
self.bdd.set_crypt_attr(crypt_id, compress_mode="gz", sha1sum=get_hash(enc))
print(" Size : ", get_size(enc))
upload_file(enc, self.save_mode, file_name=os.path.join(self.save_location, str(crypt_id).zfill(ZFILL)), bucket=self.buk)
self.upload_file(enc, file_name=str(crypt_id).zfill(ZFILL))
else:
files.append({'name': f,
'path': pathlib.Path(uri).as_posix(),
@ -85,13 +85,13 @@ class Backup:
enc = crypt(compress(tarball), self.key)
self.bdd.set_crypt_attr(crypt_id, compress_mode="tar.gz", sha1sum=get_hash(enc))
print(" Size : ", get_size(enc))
upload_file(enc, self.save_mode, file_name=os.path.join(self.save_location, str(crypt_id).zfill(ZFILL)), bucket=self.buk)
self.upload_file(enc, file_name=str(crypt_id).zfill(ZFILL))
def recover_file(self, paths, parents=False, save_path=os.getcwd()):
files = self.bdd.get_crypt_name(paths)
for file in files:
if file['crypt_id'] is not None:
encrypted_file = download_file(self.save_mode, os.path.join(self.save_location, str(file['crypt_id']).zfill(ZFILL)), bucket=self.buk)
encrypted_file = self.download_file(str(file['crypt_id']).zfill(ZFILL))
file_hash = get_hash(encrypted_file)
if file['sha1sum'] == file_hash:
if parents:
@ -103,20 +103,18 @@ class Backup:
print("{} {}".format(file_hash, "File"))
print()
def upload_file(self, file, file_name):
if self.save_mode == BACKBLAZE:
self.buk.upload_bytes(file.read(), file_name)
elif self.save_mode == LOCAL:
save(file, os.path.join(self.save_location, file_name))
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 download_file(mode, file, bucket=None):
def download_file(self, file):
dl = tempfile.SpooledTemporaryFile()
if mode == BACKBLAZE:
bucket.download_file_by_name(file).save(dl)
elif mode == LOCAL:
with open(file, 'rb') as infile:
if self.save_mode == BACKBLAZE:
self.buk.download_file_by_name(file).save(dl)
elif self.save_mode == LOCAL:
with open(os.path.join(self.save_location, file), 'rb') as infile:
dl.write(infile.read())
return dl