Fix uploading files with unicode filenames (#1214)

* Patch urllib3RequestField to make it *not* support RFC2231

* Add new required test data file

* Fix on py2

* Remove weird legacy code from inputfile

Not needed anymore, and also makes it not work... so that was not great
This commit is contained in:
Jasmin Bom 2018-09-10 21:08:05 +02:00 committed by GitHub
parent b8c288ff4a
commit e247fa7c2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 6 deletions

View file

@ -22,7 +22,6 @@
import imghdr
import mimetypes
import os
import sys
from uuid import uuid4
from telegram import TelegramError
@ -74,10 +73,6 @@ class InputFile(object):
if not self.filename or '.' not in self.filename:
self.filename = self.mimetype.replace('/', '.')
if sys.version_info < (3,):
if isinstance(self.filename, unicode): # flake8: noqa pylint: disable=E0602
self.filename = self.filename.encode('utf-8', 'replace')
@property
def field_tuple(self):
return self.filename, self.input_file_content, self.mimetype

View file

@ -36,6 +36,7 @@ try:
import telegram.vendor.ptb_urllib3.urllib3.contrib.appengine as appengine
from telegram.vendor.ptb_urllib3.urllib3.connection import HTTPConnection
from telegram.vendor.ptb_urllib3.urllib3.util.timeout import Timeout
from telegram.vendor.ptb_urllib3.urllib3.fields import RequestField
except ImportError: # pragma: no cover
warnings.warn("python-telegram-bot wasn't properly installed. Please refer to README.rst on "
"how to properly install.")
@ -45,6 +46,20 @@ from telegram import (InputFile, TelegramError, InputMedia)
from telegram.error import (Unauthorized, NetworkError, TimedOut, BadRequest, ChatMigrated,
RetryAfter, InvalidToken)
def _render_part(self, name, value):
"""
Monkey patch urllib3.urllib3.fields.RequestField to make it *not* support RFC2231 compliant
Content-Disposition headers since telegram servers don't understand it. Instead just escape
\ and " and replace any \n and \r with a space.
"""
value = value.replace(u'\\', u'\\\\').replace(u'"', u'\\"')
value = value.replace(u'\r', u' ').replace(u'\n', u' ')
return u'%s="%s"' % (name, value)
RequestField._render_part = _render_part
logging.getLogger('urllib3').setLevel(logging.WARNING)
USER_AGENT = 'Python Telegram Bot (https://github.com/python-telegram-bot/python-telegram-bot)'

BIN
tests/data/测试.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
#
# -*- coding: utf-8 -*-
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2018
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
@ -195,6 +195,21 @@ class TestPhoto(object):
assert isinstance(photo.file_id, str)
assert photo.file_id != ''
@flaky(3, 1)
@pytest.mark.timeout(10)
def test_send_file_unicode_filename(self, bot, chat_id):
"""
Regression test for https://github.com/python-telegram-bot/python-telegram-bot/issues/1202
"""
with open(u'tests/data/测试.png', 'rb') as f:
message = bot.send_photo(photo=f, chat_id=chat_id)
photo = message.photo[-1]
assert isinstance(photo, PhotoSize)
assert isinstance(photo.file_id, str)
assert photo.file_id != ''
@flaky(3, 1)
@pytest.mark.timeout(10)
def test_send_bytesio_jpg_file(self, bot, chat_id):