#!/usr/bin/env python
# pylint: disable=W0622,E0611
#
# 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>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program.  If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram InputFile."""

import imghdr
import mimetypes
import os
from uuid import uuid4

from telegram import TelegramError

DEFAULT_MIME_TYPE = 'application/octet-stream'


class InputFile(object):
    """This object represents a Telegram InputFile.

    Attributes:
        input_file_content (:obj:`bytes`): The binaray content of the file to send.
        filename (:obj:`str`): Optional, Filename for the file to be sent.
        attach (:obj:`str`): Optional, attach id for sending multiple files.

    Args:
        obj (:obj:`File handler`): An open file descriptor.
        filename (:obj:`str`, optional): Filename for this InputFile.
        attach (:obj:`bool`, optional): Whether this should be send as one file or is part of a
            collection of files.

    Raises:
        TelegramError

    """

    def __init__(self, obj, filename=None, attach=None):
        self.filename = None
        self.input_file_content = obj.read()
        self.attach = 'attached' + uuid4().hex if attach else None

        if filename:
            self.filename = filename
        elif (hasattr(obj, 'name') and
              not isinstance(obj.name, int) and  # py3
              obj.name != '<fdopen>'):  # py2
            # on py2.7, pylint fails to understand this properly
            # pylint: disable=E1101
            self.filename = os.path.basename(obj.name)

        try:
            self.mimetype = self.is_image(self.input_file_content)
        except TelegramError:
            if self.filename:
                self.mimetype = mimetypes.guess_type(
                    self.filename)[0] or DEFAULT_MIME_TYPE
            else:
                self.mimetype = DEFAULT_MIME_TYPE
        if not self.filename or '.' not in self.filename:
            self.filename = self.mimetype.replace('/', '.')

    @property
    def field_tuple(self):
        return self.filename, self.input_file_content, self.mimetype

    @staticmethod
    def is_image(stream):
        """Check if the content file is an image by analyzing its headers.

        Args:
            stream (:obj:`str`): A str representing the content of a file.

        Returns:
            :obj:`str`: The str mime-type of an image.

        """
        image = imghdr.what(None, stream)
        if image:
            return 'image/%s' % image

        raise TelegramError('Could not parse file content')

    @staticmethod
    def is_file(obj):
        return hasattr(obj, 'read')

    def to_dict(self):
        if self.attach:
            return 'attach://' + self.attach

telegram/files/inputfile.py
#!/usr/bin/env python
# pylint: disable=W0622,E0611
#
# 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>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program.  If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram InputFile."""

import imghdr
import mimetypes
import os
from uuid import uuid4

from telegram import TelegramError

DEFAULT_MIME_TYPE = 'application/octet-stream'


class InputFile(object):
    """This object represents a Telegram InputFile.

    Attributes:
        input_file_content (:obj:`bytes`): The binaray content of the file to send.
        filename (:obj:`str`): Optional, Filename for the file to be sent.
        attach (:obj:`str`): Optional, attach id for sending multiple files.

    Args:
        obj (:obj:`File handler`): An open file descriptor.
        filename (:obj:`str`, optional): Filename for this InputFile.
        attach (:obj:`bool`, optional): Whether this should be send as one file or is part of a
            collection of files.

    Raises:
        TelegramError

    """

    def __init__(self, obj, filename=None, attach=None):
        self.filename = None
        self.input_file_content = obj.read()
        self.attach = 'attached' + uuid4().hex if attach else None

        if filename:
            self.filename = filename
        elif (hasattr(obj, 'name') and
              not isinstance(obj.name, int) and  # py3
              obj.name != '<fdopen>'):  # py2
            # on py2.7, pylint fails to understand this properly
            # pylint: disable=E1101
            self.filename = os.path.basename(obj.name)

        try:
            self.mimetype = self.is_image(self.input_file_content)
        except TelegramError:
            if self.filename:
                self.mimetype = mimetypes.guess_type(
                    self.filename)[0] or DEFAULT_MIME_TYPE
            else:
                self.mimetype = DEFAULT_MIME_TYPE
        if not self.filename:
            self.filename = self.mimetype.replace('/', '.')

    @property
    def field_tuple(self):
        return self.filename, self.input_file_content, self.mimetype

    @staticmethod
    def is_image(stream):
        """Check if the content file is an image by analyzing its headers.

        Args:
            stream (:obj:`str`): A str representing the content of a file.

        Returns:
            :obj:`str`: The str mime-type of an image.

        """
        image = imghdr.what(None, stream)
        if image:
            return 'image/%s' % image

        raise TelegramError('Could not parse file content')

    @staticmethod
    def is_file(obj):
        return hasattr(obj, 'read')

    def to_dict(self):
        if self.attach:
            return 'attach://' + self.attach

telegram/files/inputfile.py
diff --git a/telegram/files/inputfile.py b/telegram/files/inputfile.py
index 0d614db3439..977c325c525 100644
--- a/telegram/files/inputfile.py
+++ b/telegram/files/inputfile.py
@@ -70,7 +70,7 @@ def __init__(self, obj, filename=None, attach=None):
                     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
diff --git a/tests/data/telegram b/tests/data/telegram
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/tests/test_inputfile.py b/tests/test_inputfile.py
index fd34c841b6a..1c667d3df0b 100644
--- a/tests/test_inputfile.py
+++ b/tests/test_inputfile.py
@@ -63,3 +63,34 @@ def test_mimetypes(self):
         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'

Can't change filename when send document after upgrading to v11.1.0
### Steps to reproduce
1. Generate a pickle file "test" (I didn't test other common files yet)

2. Send this file to user

`bot.send_document(chat_id=user_chat_id, document=open('./test', 'rb'), filename="test")`

or

`bot.send_document(chat_id=user_chat_id, document=open('./test', 'rb'))`

### Expected behaviour
User will receive a file named **test**

### Actual behaviour
User received a file named **application.octet-stream**

### Configuration
**Operating System:** 

Debian (Server, where I first found this issue)

Ubuntu(Local, **I test on v10.1.0, everything is fine**, so I upgrade to v11.1.0, then I have the same issue as Debian Server)

**Version of Python, python-telegram-bot & dependencies:**

``$ python -m telegram``

*My Local Ubuntu After Upgrade:*
python-telegram-bot 11.1.0
certifi 2018.08.24
future 0.16.0
Python 3.6.6 (default, Sep 12 2018, 18:26:19)  [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]

The pictures shows results of python-telegram-bot v10.1.0 (the first one) and v11.1.0 (the second one) :

![screenshot from 2018-09-29 14-16-06](https://user-images.githubusercontent.com/16657782/46246951-2382a080-c3f4-11e8-9f01-b4dd90960ccd.png)


