Skip to content

Commit bacabbe

Browse files
fix download without path arguments (python-telegram-bot#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>
1 parent d8dcdee commit bacabbe

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

telegram/files/file.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"""This module contains an object that represents a Telegram File."""
2020
from base64 import b64decode
2121
from os.path import basename
22+
import os
2223

2324
from future.backports.urllib import parse as urllib_parse
2425

@@ -76,9 +77,10 @@ def de_json(cls, data, bot):
7677
def download(self, custom_path=None, out=None, timeout=None):
7778
"""
7879
Download this file. By default, the file is saved in the current working directory with its
79-
original filename as reported by Telegram. If a :attr:`custom_path` is supplied, it will be
80-
saved to that path instead. If :attr:`out` is defined, the file contents will be saved to
81-
that object using the ``out.write`` method.
80+
original filename as reported by Telegram. If the file has no filename, it the file ID will
81+
be used as filename. If a :attr:`custom_path` is supplied, it will be saved to that path
82+
instead. If :attr:`out` is defined, the file contents will be saved to that object using
83+
the ``out.write`` method.
8284
8385
Note:
8486
:attr:`custom_path` and :attr:`out` are mutually exclusive.
@@ -116,8 +118,10 @@ def download(self, custom_path=None, out=None, timeout=None):
116118
else:
117119
if custom_path:
118120
filename = custom_path
119-
else:
121+
elif self.file_path:
120122
filename = basename(self.file_path)
123+
else:
124+
filename = os.path.join(os.getcwd(), self.file_id)
121125

122126
buf = self.bot.request.retrieve(url, timeout=timeout)
123127
if self._credentials:

tests/test_file.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ def test(*args, **kwargs):
9696
os.close(file_handle)
9797
os.unlink(custom_path)
9898

99+
def test_download_no_filename(self, monkeypatch, file):
100+
def test(*args, **kwargs):
101+
return self.file_content
102+
103+
file.file_path = None
104+
105+
monkeypatch.setattr('telegram.utils.request.Request.retrieve', test)
106+
out_file = file.download()
107+
108+
assert out_file[-len(file.file_id):] == file.file_id
109+
try:
110+
with open(out_file, 'rb') as fobj:
111+
assert fobj.read() == self.file_content
112+
finally:
113+
os.unlink(out_file)
114+
99115
def test_download_file_obj(self, monkeypatch, file):
100116
def test(*args, **kwargs):
101117
return self.file_content

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy