Merge pull request #38 from ckornacker/feature/filenames

allow setting file names on upload and download
This commit is contained in:
Richard O'Dwyer 2013-05-08 03:36:42 -07:00
commit 18d86578a5

View file

@ -372,22 +372,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)
@ -414,7 +414,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
@ -466,7 +470,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
@ -521,7 +525,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],