Skip to content

Commit b1ada6a

Browse files
committed
Upload supports empty files
1 parent 9263d0c commit b1ada6a

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

mega/mega.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -531,37 +531,41 @@ def upload(self, filename, dest=None, dest_filename=None):
531531
mac_str = '\0' * 16
532532
mac_encryptor = AES.new(k_str, AES.MODE_CBC, mac_str)
533533
iv_str = a32_to_str([ul_key[4], ul_key[5], ul_key[4], ul_key[5]])
534+
if file_size > 0:
535+
for chunk_start, chunk_size in get_chunks(file_size):
536+
chunk = input_file.read(chunk_size)
537+
upload_progress += len(chunk)
538+
539+
encryptor = AES.new(k_str, AES.MODE_CBC, iv_str)
540+
for i in range(0, len(chunk)-16, 16):
541+
block = chunk[i:i + 16]
542+
encryptor.encrypt(block)
543+
544+
#fix for files under 16 bytes failing
545+
if file_size > 16:
546+
i += 16
547+
else:
548+
i = 0
534549

535-
for chunk_start, chunk_size in get_chunks(file_size):
536-
chunk = input_file.read(chunk_size)
537-
upload_progress += len(chunk)
538-
539-
encryptor = AES.new(k_str, AES.MODE_CBC, iv_str)
540-
for i in range(0, len(chunk)-16, 16):
541550
block = chunk[i:i + 16]
542-
encryptor.encrypt(block)
543-
544-
#fix for files under 16 bytes failing
545-
if file_size > 16:
546-
i += 16
547-
else:
548-
i = 0
549-
550-
block = chunk[i:i + 16]
551-
if len(block) % 16:
552-
block += '\0' * (16 - len(block) % 16)
553-
mac_str = mac_encryptor.encrypt(encryptor.encrypt(block))
554-
555-
#encrypt file and upload
556-
chunk = aes.encrypt(chunk)
557-
output_file = requests.post(ul_url + "/" + str(chunk_start),
558-
data=chunk, timeout=self.timeout)
551+
if len(block) % 16:
552+
block += '\0' * (16 - len(block) % 16)
553+
mac_str = mac_encryptor.encrypt(encryptor.encrypt(block))
554+
555+
#encrypt file and upload
556+
chunk = aes.encrypt(chunk)
557+
output_file = requests.post(ul_url + "/" + str(chunk_start),
558+
data=chunk, timeout=self.timeout)
559+
completion_file_handle = output_file.text
560+
561+
if self.options.get('verbose') is True:
562+
# upload progress
563+
print('{0} of {1} uploaded'.format(upload_progress, file_size))
564+
else:
565+
output_file = requests.post(ul_url + "/0",
566+
data='', timeout=self.timeout)
559567
completion_file_handle = output_file.text
560-
561-
if self.options.get('verbose') is True:
562-
# upload progress
563-
print('{0} of {1} uploaded'.format(upload_progress, file_size))
564-
568+
565569
file_mac = str_to_a32(mac_str)
566570

567571
#determine meta mac

0 commit comments

Comments
 (0)