mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-29 15:49:02 +01:00
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:
parent
d8dcdeea75
commit
bacabbe767
2 changed files with 24 additions and 4 deletions
|
@ -19,6 +19,7 @@
|
|||
"""This module contains an object that represents a Telegram File."""
|
||||
from base64 import b64decode
|
||||
from os.path import basename
|
||||
import os
|
||||
|
||||
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):
|
||||
"""
|
||||
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
|
||||
saved to that path instead. If :attr:`out` is defined, the file contents will be saved to
|
||||
that object using the ``out.write`` method.
|
||||
original filename as reported by Telegram. If the file has no filename, it the file ID will
|
||||
be used as filename. If a :attr:`custom_path` is supplied, it will be saved to that path
|
||||
instead. If :attr:`out` is defined, the file contents will be saved to that object using
|
||||
the ``out.write`` method.
|
||||
|
||||
Note:
|
||||
:attr:`custom_path` and :attr:`out` are mutually exclusive.
|
||||
|
@ -116,8 +118,10 @@ class File(TelegramObject):
|
|||
else:
|
||||
if custom_path:
|
||||
filename = custom_path
|
||||
else:
|
||||
elif 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)
|
||||
if self._credentials:
|
||||
|
|
|
@ -96,6 +96,22 @@ class TestFile(object):
|
|||
os.close(file_handle)
|
||||
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(*args, **kwargs):
|
||||
return self.file_content
|
||||
|
|
Loading…
Reference in a new issue