fix download without path arguments (#1591)

* fix download without path arguments

* fix download without path arguments

* solved downloading a file without file_path or custom_path

* if no file_path, download as file_id

* Add test case

* Elaborate doc string

Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
This commit is contained in:
Gabriel Simonetto 2020-02-06 07:21:21 -03:00 committed by GitHub
parent d8dcdeea75
commit bacabbe767
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View file

@ -19,6 +19,7 @@
"""This module contains an object that represents a Telegram File.""" """This module contains an object that represents a Telegram File."""
from base64 import b64decode from base64 import b64decode
from os.path import basename from os.path import basename
import os
from future.backports.urllib import parse as urllib_parse from future.backports.urllib import parse as urllib_parse
@ -76,9 +77,10 @@ class File(TelegramObject):
def download(self, custom_path=None, out=None, timeout=None): def download(self, custom_path=None, out=None, timeout=None):
""" """
Download this file. By default, the file is saved in the current working directory with its Download this file. By default, the file is saved in the current working directory with its
original filename as reported by Telegram. If a :attr:`custom_path` is supplied, it will be original filename as reported by Telegram. If the file has no filename, it the file ID will
saved to that path instead. If :attr:`out` is defined, the file contents will be saved to be used as filename. If a :attr:`custom_path` is supplied, it will be saved to that path
that object using the ``out.write`` method. instead. If :attr:`out` is defined, the file contents will be saved to that object using
the ``out.write`` method.
Note: Note:
:attr:`custom_path` and :attr:`out` are mutually exclusive. :attr:`custom_path` and :attr:`out` are mutually exclusive.
@ -116,8 +118,10 @@ class File(TelegramObject):
else: else:
if custom_path: if custom_path:
filename = custom_path filename = custom_path
else: elif self.file_path:
filename = basename(self.file_path) filename = basename(self.file_path)
else:
filename = os.path.join(os.getcwd(), self.file_id)
buf = self.bot.request.retrieve(url, timeout=timeout) buf = self.bot.request.retrieve(url, timeout=timeout)
if self._credentials: if self._credentials:

View file

@ -96,6 +96,22 @@ class TestFile(object):
os.close(file_handle) os.close(file_handle)
os.unlink(custom_path) os.unlink(custom_path)
def test_download_no_filename(self, monkeypatch, file):
def test(*args, **kwargs):
return self.file_content
file.file_path = None
monkeypatch.setattr('telegram.utils.request.Request.retrieve', test)
out_file = file.download()
assert out_file[-len(file.file_id):] == file.file_id
try:
with open(out_file, 'rb') as fobj:
assert fobj.read() == self.file_content
finally:
os.unlink(out_file)
def test_download_file_obj(self, monkeypatch, file): def test_download_file_obj(self, monkeypatch, file):
def test(*args, **kwargs): def test(*args, **kwargs):
return self.file_content return self.file_content