Merge pull request #641 from azogue/fixes

fix setting default mimetype of inputfile
This commit is contained in:
Noam Meltzer 2017-06-03 00:22:32 +03:00 committed by GitHub
commit 2680740316
4 changed files with 34 additions and 1 deletions

View file

@ -24,6 +24,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `Eli Gao <https://github.com/eligao>`_
- `ErgoZ Riftbit Vaper <https://github.com/ergoz>`_
- `Eugene Lisitsky <https://github.com/lisitsky>`_
- `Eugenio Panadero <https://github.com/azogue>`_
- `evgfilim1 <https://github.com/evgfilim1>`_
- `franciscod <https://github.com/franciscod>`_
- `Hugo Damer <https://github.com/HakimusGIT>`_

View file

@ -69,7 +69,11 @@ class InputFile(object):
if not self.filename or '.' not in self.filename:
self.filename = self.mimetype.replace('/', '.')
except TelegramError:
self.mimetype = mimetypes.guess_type(self.filename)[0] or DEFAULT_MIME_TYPE
if self.filename:
self.mimetype = mimetypes.guess_type(
self.filename)[0] or DEFAULT_MIME_TYPE
else:
self.mimetype = DEFAULT_MIME_TYPE
@property
def headers(self):

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

View file

@ -18,6 +18,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents Tests for Telegram Photo"""
from io import BytesIO
import sys
import unittest
import os
@ -37,6 +38,7 @@ class PhotoTest(BaseTest, unittest.TestCase):
self.photo_file = open('tests/data/telegram.jpg', 'rb')
self.photo_file_id = 'AgADAQADgEsyGx8j9QfmDMmwkPBrFcKRzy8ABHW8ul9nW7FoNHYBAAEC'
self.photo_file_url = 'https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/tests/data/telegram.jpg'
self.photo_bytes_jpg_no_standard = 'tests/data/telegram_no_standard_header.jpg'
self.width = 300
self.height = 300
self.thumb = {
@ -122,6 +124,32 @@ class PhotoTest(BaseTest, unittest.TestCase):
self.assertEqual(photo.height, self.height)
self.assertEqual(photo.file_size, self.file_size)
@flaky(3, 1)
@timeout(10)
def test_send_photo_bytesio_jpg_file(self):
from telegram.inputfile import InputFile
# raw image bytes
raw_bytes = BytesIO(open(self.photo_bytes_jpg_no_standard, 'rb').read())
inputfile = InputFile({"photo": raw_bytes})
self.assertEqual(inputfile.mimetype, 'application/octet-stream')
# raw image bytes with name info
raw_bytes = BytesIO(open(self.photo_bytes_jpg_no_standard, 'rb').read())
raw_bytes.name = self.photo_bytes_jpg_no_standard
inputfile = InputFile({"photo": raw_bytes})
self.assertEqual(inputfile.mimetype, 'image/jpeg')
# send raw photo
raw_bytes = BytesIO(open(self.photo_bytes_jpg_no_standard, 'rb').read())
message = self._bot.sendPhoto(self._chat_id, photo=raw_bytes)
photo = message.photo[-1]
self.assertTrue(isinstance(photo.file_id, str))
self.assertNotEqual(photo.file_id, '')
self.assertTrue(isinstance(photo, telegram.PhotoSize))
self.assertEqual(photo.width, 1920)
self.assertEqual(photo.height, 1080)
self.assertEqual(photo.file_size, 30907)
@flaky(3, 1)
@timeout(10)
def test_send_photo_resend(self):