]> git.ekhem.eu.org Git - gdrive_knife.git/commitdiff
Display progress for download, encrypt and decrypt.
authorJakub Czajka <jakub@ekhem.eu.org>
Sun, 19 Nov 2023 13:59:45 +0000 (14:59 +0100)
committerJakub Czajka <jakub@ekhem.eu.org>
Sun, 19 Nov 2023 15:43:41 +0000 (16:43 +0100)
gdrive_knife.py

index b4b99129200d5cc29ffa94ddb4ed3a459511c42c..55939e933cb4eef40270b53fe3075091b771501e 100644 (file)
@@ -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.')