From 7c60eddc42f614a3362ab63dcd4792b289edd379 Mon Sep 17 00:00:00 2001 From: Harmon Date: Sun, 2 Aug 2020 14:49:41 -0500 Subject: [PATCH] Use mimetypes.guess_type as fallback for determining image file type Fixes #1411, regression introduced with #1086 where imghdr.what is unable to determine the file type --- tweepy/api.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tweepy/api.py b/tweepy/api.py index 48450d1..ba047d3 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -3,6 +3,7 @@ # See LICENSE for details. import imghdr +import mimetypes import os import six @@ -220,7 +221,7 @@ class API(object): """ f = kwargs.pop('file', None) - file_type = imghdr.what(filename) + file_type = imghdr.what(filename) or mimetypes.guess_type(filename)[0] if file_type == 'gif': max_size = 14649 else: @@ -1417,10 +1418,10 @@ class API(object): # image must be gif, jpeg, png, webp if not file_type: - file_type = imghdr.what(filename) + file_type = imghdr.what(filename) or mimetypes.guess_type(filename)[0] if file_type is None: raise TweepError('Could not determine file type') - if file_type not in ['gif', 'jpeg', 'png', 'webp']: + if file_type not in ['gif', 'jpeg', 'png', 'webp', 'image/gif', 'image/jpeg', 'image/png']: raise TweepError('Invalid file type for image: %s' % file_type) if isinstance(filename, six.text_type): -- 2.25.1