allow setting file names on upload and download

This commit is contained in:
Christian Kornacker 2013-05-06 13:01:19 +02:00
parent 24f44137a1
commit 5df8b71b71

View file

@ -394,22 +394,22 @@ class Mega(object):
########################################################################## ##########################################################################
# DOWNLOAD # DOWNLOAD
def download(self, file, dest_path=None): def download(self, file, dest_path=None, dest_file=None):
""" """
Download a file by it's file object Download a file by it's file object
""" """
self.download_file(None, None, file=file[1], dest_path=dest_path, is_public=False) self.download_file(None, None, file=file[1], dest_path=dest_path, dest_file=dest_file, is_public=False)
def download_url(self, url, dest_path=None): def download_url(self, url, dest_path=None, dest_file=None):
""" """
Download a file by it's public url Download a file by it's public url
""" """
path = self.parse_url(url).split('!') path = self.parse_url(url).split('!')
file_id = path[0] file_id = path[0]
file_key = path[1] file_key = path[1]
self.download_file(file_id, file_key, dest_path, is_public=True) self.download_file(file_id, file_key, dest_path, dest_file, is_public=True)
def download_file(self, file_handle, file_key, dest_path=None, is_public=False, file=None): def download_file(self, file_handle, file_key, dest_path=None, dest_file=None, is_public=False, file=None):
if file is None : if file is None :
if is_public: if is_public:
file_key = base64_to_a32(file_key) file_key = base64_to_a32(file_key)
@ -436,7 +436,11 @@ class Mega(object):
file_size = file_data['s'] file_size = file_data['s']
attribs = base64_url_decode(file_data['at']) attribs = base64_url_decode(file_data['at'])
attribs = decrypt_attr(attribs, k) attribs = decrypt_attr(attribs, k)
file_name = attribs['n']
if dest_file is not None:
file_name = dest_file
else:
file_name = attribs['n']
input_file = requests.get(file_url, stream=True).raw input_file = requests.get(file_url, stream=True).raw
@ -492,7 +496,7 @@ class Mega(object):
########################################################################## ##########################################################################
# UPLOAD # UPLOAD
def upload(self, filename, dest=None): def upload(self, filename, dest=None, remote=None):
#determine storage node #determine storage node
if dest is None: if dest is None:
#if none set, upload to cloud drive node #if none set, upload to cloud drive node
@ -548,7 +552,11 @@ class Mega(object):
#determine meta mac #determine meta mac
meta_mac = (file_mac[0] ^ file_mac[1], file_mac[2] ^ file_mac[3]) meta_mac = (file_mac[0] ^ file_mac[1], file_mac[2] ^ file_mac[3])
attribs = {'n': os.path.basename(filename)} if remote is not None:
attribs = {'n': remote}
else:
attribs = {'n': os.path.basename(filename)}
encrypt_attribs = base64_url_encode(encrypt_attr(attribs, ul_key[:4])) encrypt_attribs = base64_url_encode(encrypt_attr(attribs, ul_key[:4]))
key = [ul_key[0] ^ ul_key[4], ul_key[1] ^ ul_key[5], key = [ul_key[0] ^ ul_key[4], ul_key[1] ^ ul_key[5],
ul_key[2] ^ meta_mac[0], ul_key[3] ^ meta_mac[1], ul_key[2] ^ meta_mac[0], ul_key[3] ^ meta_mac[1],