Allow filenames without dots in them when sending files (#1228)

* Fix issue 1227 by allowing filenames without dots in them

* Touch new test data file

* Satisfy flake8
This commit is contained in:
Jasmin Bom 2018-09-30 12:07:17 +02:00 committed by GitHub
parent c714a177d1
commit 4c66ba3a8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 1 deletions

View file

@ -70,7 +70,7 @@ class InputFile(object):
self.filename)[0] or DEFAULT_MIME_TYPE
else:
self.mimetype = DEFAULT_MIME_TYPE
if not self.filename or '.' not in self.filename:
if not self.filename:
self.filename = self.mimetype.replace('/', '.')
@property

0
tests/data/telegram Normal file
View file

View file

@ -63,3 +63,34 @@ class TestInputFile(object):
assert (InputFile(BytesIO(b'blah'), filename='tg.notaproperext').mimetype ==
'application/octet-stream')
assert InputFile(BytesIO(b'blah')).mimetype == 'application/octet-stream'
def test_filenames(self):
assert InputFile(open('tests/data/telegram.jpg', 'rb')).filename == 'telegram.jpg'
assert InputFile(open('tests/data/telegram.jpg', 'rb'),
filename='blah').filename == 'blah'
assert InputFile(open('tests/data/telegram.jpg', 'rb'),
filename='blah.jpg').filename == 'blah.jpg'
assert InputFile(open('tests/data/telegram', 'rb')).filename == 'telegram'
assert InputFile(open('tests/data/telegram', 'rb'),
filename='blah').filename == 'blah'
assert InputFile(open('tests/data/telegram', 'rb'),
filename='blah.jpg').filename == 'blah.jpg'
class MockedFileobject(object):
# A open(?, 'rb') without a .name
def __init__(self, f):
self.f = open(f, 'rb')
def read(self):
return self.f.read()
assert InputFile(MockedFileobject('tests/data/telegram.jpg')).filename == 'image.jpeg'
assert InputFile(MockedFileobject('tests/data/telegram.jpg'),
filename='blah').filename == 'blah'
assert InputFile(MockedFileobject('tests/data/telegram.jpg'),
filename='blah.jpg').filename == 'blah.jpg'
assert InputFile(MockedFileobject('tests/data/telegram')).filename == 'application.octet-stream' # flake8: noqa
assert InputFile(MockedFileobject('tests/data/telegram'),
filename='blah').filename == 'blah'
assert InputFile(MockedFileobject('tests/data/telegram'),
filename='blah.jpg').filename == 'blah.jpg'