From: Jakub Czajka Date: Sun, 19 Nov 2023 13:59:45 +0000 (+0100) Subject: Display progress for download, encrypt and decrypt. X-Git-Url: https://git.ekhem.eu.org/?a=commitdiff_plain;h=6e5454c97ebcaf170f2865ed97264a9ffdae529a;p=gdrive_knife.git Display progress for download, encrypt and decrypt. --- diff --git a/gdrive_knife.py b/gdrive_knife.py index b4b9912..55939e9 100644 --- a/gdrive_knife.py +++ b/gdrive_knife.py @@ -62,6 +62,11 @@ def get_file_id(drive, file_path): # encrypts/decrypts them separately. See https://stackoverflow.com/a/71068357. def encrypt_chunks_in_place(encryption_key, path): block = 1 << 16 + + file_size = os.path.getsize(path) + processed_bytes = 0 + percentage = 0 + tmp_path = path + str(uuid.uuid4()) with open(path, 'rb') as input_file, open(tmp_path, 'wb') as output_file: while True: @@ -72,11 +77,22 @@ def encrypt_chunks_in_place(encryption_key, path): bytes_as_int = len(encrypted_bytes).to_bytes(4, 'big') output_file.write(bytes_as_int) output_file.write(encrypted_bytes) + + processed_bytes = processed_bytes + len(unencrypted_bytes) + new_percentage = int(processed_bytes / file_size * 100) + if new_percentage > percentage + 7: + percentage = new_percentage + print(f'Encrypt: {percentage}%') + if len(unencrypted_bytes) < block: break os.rename(tmp_path, path) def decrypt_chunks_in_place(encryption_key, path): + file_size = os.path.getsize(path) + processed_bytes = 0 + percentage = 0 + tmp_path = path + str(uuid.uuid4()) with open(path, 'rb') as input_file, open(tmp_path, 'wb') as output_file: while True: @@ -87,6 +103,13 @@ def decrypt_chunks_in_place(encryption_key, path): chunk = input_file.read(bytes_as_int) decrypted_bytes = encryption_key.decrypt(chunk) output_file.write(decrypted_bytes) + + processed_bytes = processed_bytes + 4 + len(chunk) + new_percentage = int(processed_bytes / file_size * 100) + if new_percentage > percentage + 7: + percentage = new_percentage + print(f'Decrypt: {percentage}%') + os.rename(tmp_path, path) class auth_server(): @@ -164,7 +187,7 @@ def download(args): done = False while done is False: status, done = downloader.next_chunk() - print(f'Download {int(status.progress() * 100)}.') + print(f'Download: {int(status.progress() * 100)}%.') decrypt_chunks_in_place(args.key, path_in_tmp) print(f'{args.path} decrypted.')