Skip to content

Commit 384519c

Browse files
author
Richard O'Dwyer
committed
Merge pull request #63 from plutec/master
Upload supports empty files
2 parents 9263d0c + 3bd15a9 commit 384519c

File tree

1 file changed

+60
-28
lines changed

1 file changed

+60
-28
lines changed

mega/mega.py

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,34 @@ def _init_shared_keys(self, files, shared_keys):
199199

200200
##########################################################################
201201
# GET
202+
203+
def find_path_descriptor(self, path):
204+
"""
205+
Find descriptor of folder inside a path. i.e.: folder1/folder2/folder3
206+
Params:
207+
path, string like folder1/folder2/folder3
208+
Return:
209+
Descriptor (str) of folder3 if exists, None otherwise
210+
"""
211+
paths = path.split('/')
212+
213+
files = self.get_files()
214+
parent_desc = self.root_id
215+
found = False
216+
for foldername in paths:
217+
if foldername != '':
218+
for file in files.iteritems():
219+
if file[1]['a'] and file[1]['t'] and \
220+
file[1]['a']['n'] == foldername:
221+
if parent_desc == file[1]['p']:
222+
parent_desc = file[0]
223+
found = True
224+
if found:
225+
found = False
226+
else:
227+
return None
228+
return parent_desc
229+
202230
def find(self, filename):
203231
"""
204232
Return file object from given filename
@@ -531,37 +559,41 @@ def upload(self, filename, dest=None, dest_filename=None):
531559
mac_str = '\0' * 16
532560
mac_encryptor = AES.new(k_str, AES.MODE_CBC, mac_str)
533561
iv_str = a32_to_str([ul_key[4], ul_key[5], ul_key[4], ul_key[5]])
562+
if file_size > 0:
563+
for chunk_start, chunk_size in get_chunks(file_size):
564+
chunk = input_file.read(chunk_size)
565+
upload_progress += len(chunk)
566+
567+
encryptor = AES.new(k_str, AES.MODE_CBC, iv_str)
568+
for i in range(0, len(chunk)-16, 16):
569+
block = chunk[i:i + 16]
570+
encryptor.encrypt(block)
571+
572+
#fix for files under 16 bytes failing
573+
if file_size > 16:
574+
i += 16
575+
else:
576+
i = 0
534577

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):
541578
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)
579+
if len(block) % 16:
580+
block += '\0' * (16 - len(block) % 16)
581+
mac_str = mac_encryptor.encrypt(encryptor.encrypt(block))
582+
583+
#encrypt file and upload
584+
chunk = aes.encrypt(chunk)
585+
output_file = requests.post(ul_url + "/" + str(chunk_start),
586+
data=chunk, timeout=self.timeout)
587+
completion_file_handle = output_file.text
588+
589+
if self.options.get('verbose') is True:
590+
# upload progress
591+
print('{0} of {1} uploaded'.format(upload_progress, file_size))
592+
else:
593+
output_file = requests.post(ul_url + "/0",
594+
data='', timeout=self.timeout)
559595
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-
596+
565597
file_mac = str_to_a32(mac_str)
566598

567599
#determine meta mac

0 commit comments

Comments
 (0)