From c4741b9ba63f6810ea698949b3d532a6f16db204 Mon Sep 17 00:00:00 2001 From: gabriel Date: Thu, 31 Oct 2019 15:58:00 -0300 Subject: [PATCH 1/6] fix download without path arguments --- telegram/files/file.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/telegram/files/file.py b/telegram/files/file.py index 33fa3cb6174..0fbd998abcf 100644 --- a/telegram/files/file.py +++ b/telegram/files/file.py @@ -19,6 +19,7 @@ """This module contains an object that represents a Telegram File.""" from base64 import b64decode from os.path import basename +from os import getcwd from future.backports.urllib import parse as urllib_parse @@ -116,8 +117,10 @@ def download(self, custom_path=None, out=None, timeout=None): else: if custom_path: filename = custom_path - else: + elif file_path: filename = basename(self.file_path) + else: + filename = getcwd() buf = self.bot.request.retrieve(url, timeout=timeout) if self._credentials: From a623c51bc663213793798ca5a8090915d93c38d1 Mon Sep 17 00:00:00 2001 From: gabriel Date: Thu, 31 Oct 2019 16:08:22 -0300 Subject: [PATCH 2/6] fix download without path arguments --- telegram/files/file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/files/file.py b/telegram/files/file.py index 0fbd998abcf..20b6a75a26e 100644 --- a/telegram/files/file.py +++ b/telegram/files/file.py @@ -117,7 +117,7 @@ def download(self, custom_path=None, out=None, timeout=None): else: if custom_path: filename = custom_path - elif file_path: + elif self.file_path: filename = basename(self.file_path) else: filename = getcwd() From edba857736ebd7c2abff9d9431fdda4c095145b6 Mon Sep 17 00:00:00 2001 From: gabriel Date: Thu, 31 Oct 2019 16:33:31 -0300 Subject: [PATCH 3/6] solved downloading a file without file_path or custom_path --- telegram/files/file.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telegram/files/file.py b/telegram/files/file.py index 20b6a75a26e..9c59fa1423f 100644 --- a/telegram/files/file.py +++ b/telegram/files/file.py @@ -19,7 +19,7 @@ """This module contains an object that represents a Telegram File.""" from base64 import b64decode from os.path import basename -from os import getcwd +import os from future.backports.urllib import parse as urllib_parse @@ -120,7 +120,7 @@ def download(self, custom_path=None, out=None, timeout=None): elif self.file_path: filename = basename(self.file_path) else: - filename = getcwd() + filename = os.path.join(os.getcwd(), self.file_path) buf = self.bot.request.retrieve(url, timeout=timeout) if self._credentials: From d634d023c1f45048c179594676b2ead855726d17 Mon Sep 17 00:00:00 2001 From: gabriel Date: Sun, 3 Nov 2019 20:36:59 -0300 Subject: [PATCH 4/6] if no file_path, download as file_id --- telegram/files/file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/files/file.py b/telegram/files/file.py index 9c59fa1423f..1276bcf3e82 100644 --- a/telegram/files/file.py +++ b/telegram/files/file.py @@ -120,7 +120,7 @@ def download(self, custom_path=None, out=None, timeout=None): elif self.file_path: filename = basename(self.file_path) else: - filename = os.path.join(os.getcwd(), self.file_path) + filename = os.path.join(os.getcwd(), self.file_id) buf = self.bot.request.retrieve(url, timeout=timeout) if self._credentials: From 963030016171806ef1e8ab746351b3ab123e3299 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler Date: Fri, 29 Nov 2019 12:05:25 +0000 Subject: [PATCH 5/6] Add test case --- tests/test_file.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_file.py b/tests/test_file.py index 6028139b8f4..7f2c3756ecc 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -96,6 +96,22 @@ def test(*args, **kwargs): 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 From 3a7e4edbc95a9bc27dd43eb3180a7edb562c05fa Mon Sep 17 00:00:00 2001 From: Hinrich Mahler Date: Fri, 29 Nov 2019 12:05:40 +0000 Subject: [PATCH 6/6] Elaborate doc string --- telegram/files/file.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/telegram/files/file.py b/telegram/files/file.py index 1276bcf3e82..8046b390933 100644 --- a/telegram/files/file.py +++ b/telegram/files/file.py @@ -77,9 +77,10 @@ def de_json(cls, data, bot): 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.