1717# You should have received a copy of the GNU Lesser Public License
1818# along with this program. If not, see [http://www.gnu.org/licenses/].
1919"""This module contains an object that represents a Telegram File."""
20-
2120from os .path import basename
2221
22+ from future .backports .urllib import parse as urllib_parse
23+
2324from telegram import TelegramObject
2425
2526
@@ -46,8 +47,7 @@ def __init__(self, file_id, bot, file_size=None, file_path=None, **kwargs):
4647
4748 # Optionals
4849 self .file_size = file_size
49- if file_path :
50- self .file_path = str (file_path )
50+ self .file_path = file_path
5151
5252 self .bot = bot
5353
@@ -91,7 +91,10 @@ def download(self, custom_path=None, out=None, timeout=None):
9191 if custom_path is not None and out is not None :
9292 raise ValueError ('custom_path and out are mutually exclusive' )
9393
94- url = self .file_path
94+ # Convert any UTF-8 char into a url encoded ASCII string.
95+ sres = urllib_parse .urlsplit (self .file_path )
96+ url = urllib_parse .urlunsplit (urllib_parse .SplitResult (
97+ sres .scheme , sres .netloc , urllib_parse .quote (sres .path ), sres .query , sres .fragment ))
9598
9699 if out :
97100 buf = self .bot .request .retrieve (url )
@@ -101,6 +104,6 @@ def download(self, custom_path=None, out=None, timeout=None):
101104 if custom_path :
102105 filename = custom_path
103106 else :
104- filename = basename (url )
107+ filename = basename (self . file_path )
105108
106109 self .bot .request .download (url , filename , timeout = timeout )
0 commit comments