Merge pull request #38 from ckornacker/feature/filenames
allow setting file names on upload and download
This commit is contained in:
commit
18d86578a5
1 changed files with 16 additions and 8 deletions
24
mega/mega.py
24
mega/mega.py
|
@ -372,22 +372,22 @@ class Mega(object):
|
|||
|
||||
##########################################################################
|
||||
# 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
|
||||
"""
|
||||
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
|
||||
"""
|
||||
path = self.parse_url(url).split('!')
|
||||
file_id = path[0]
|
||||
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 is_public:
|
||||
file_key = base64_to_a32(file_key)
|
||||
|
@ -414,7 +414,11 @@ class Mega(object):
|
|||
file_size = file_data['s']
|
||||
attribs = base64_url_decode(file_data['at'])
|
||||
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
|
||||
|
||||
|
@ -466,7 +470,7 @@ class Mega(object):
|
|||
|
||||
##########################################################################
|
||||
# UPLOAD
|
||||
def upload(self, filename, dest=None):
|
||||
def upload(self, filename, dest=None, remote=None):
|
||||
#determine storage node
|
||||
if dest is None:
|
||||
#if none set, upload to cloud drive node
|
||||
|
@ -521,7 +525,11 @@ class Mega(object):
|
|||
#determine meta mac
|
||||
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]))
|
||||
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],
|
||||
|
|
Loading…
Reference in a new issue