Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A valid JPEG file is rejected due to it's file name. #1060

Closed
arizonagroovejet opened this issue Jun 19, 2018 · 0 comments · Fixed by #1086
Closed

A valid JPEG file is rejected due to it's file name. #1060

arizonagroovejet opened this issue Jun 19, 2018 · 0 comments · Fixed by #1086
Labels
Improvement This is regarding an improvement to an existing feature
Milestone

Comments

@arizonagroovejet
Copy link

A valid JPEG file is rejected due to it's file name.

Steps to recreate:

$ imgFile=$(mktemp)
$ convert rose:- JPEG:- > ${imgFile}
$ file ${imgFile}
/tmp/tmp.29bSaJRIFj: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 70x46, frames 3
$ cat tweetIt.py 
#!/usr/bin/python
import tweepy
import sys
consumer_key        = 'wgrg'
consumer_secret     = 'ergr'
access_token        = 'reg'
access_token_secret = 'oino'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
media_ids=[api.media_upload(sys.argv[2]).media_id_string]
api.update_status(status=sys.argv[1],media_ids=media_ids)
$ ./tweetIt.py "foo" "${imgFile}"

Expected results:

The JPEG file /tmp/tmp.29bSaJRIFj is tweeted along with the text "foo".

Actual results:

Traceback (most recent call last):
  File "./tweetIt.py", line 18, in <module>
    media_ids=[api.media_upload(sys.argv[2]).media_id_string]
  File "/usr/lib/python2.7/site-packages/tweepy/api.py", line 201, in media_upload
    headers, post_data = API._pack_image(filename, 3072, form_field='media', f=f)
  File "/usr/lib/python2.7/site-packages/tweepy/api.py", line 1323, in _pack_image
    raise TweepError('Invalid file type for image: %s' % file_type)
tweepy.error.TweepError: Invalid file type for image: None

Additional information:

The version of Tweepy I'm using is a bit out of date

$ rpm -qf /usr/lib/python2.7/site-packages/tweepy/api.py
python2-tweepy-3.5.0-6.fc28.noarch

but I can see the issue will occur with the current version too because the behaviour results from using mimetypes.guess_type()

file_type = mimetypes.guess_type(filename)

which guesses the file type based on the file name.

It's very common on Unix-like operating systems for shell scripts to use files the name of which is generated by mktemp. That's how I encountered this issue. Some versions of mktemp support providing a suffix, e.g. $ imgFile=$(mktemp --suffix=.jpg) but some versions of mktemp do not support providing a suffix.

imghdr https://docs.python.org/2/library/imghdr.html determines the type of image contained in a file by looking at the file's contents.

Python 2.7.15 (default, May 16 2018, 17:50:09) 
[GCC 8.1.1 20180502 (Red Hat 8.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import imghdr
>>> imghdr.what('tmp.29bSaJRIFj')
'jpeg'
@Harmon758 Harmon758 added the Improvement This is regarding an improvement to an existing feature label Nov 24, 2019
@Harmon758 Harmon758 added this to the 3.9 milestone Nov 24, 2019
@Harmon758 Harmon758 linked a pull request Feb 13, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Improvement This is regarding an improvement to an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants