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

Improve mime_type guessing by using Python's imghdr.what to detect image types #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ nosetests.xml

venv/
.env
/tmp-emails/
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ With inputs and contributions from (in chronological order):
- `@smihai <https://github.com/smihai>`_
- `@Daviey <https://github.com/Daviey>`_
- `@positiveviking <https://github.com/positiveviking>`_
- `@MrTango <https://github.com/MrTango>`_

See `all Github contributors <https://github.com/lavr/python-emails/graphs/contributors>`_

5 changes: 5 additions & 0 deletions emails/store/file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: utf-8
from __future__ import unicode_literals

import imghdr
import uuid
from mimetypes import guess_type
from email.mime.base import MIMEBase
Expand Down Expand Up @@ -99,6 +100,10 @@ def get_mime_type(self):
filename = self.filename
if filename:
r = self._mime_type = guess_type(filename)[0]
if not r:
img_mime_type = imghdr.what(filename, h=self.data)
if img_mime_type:
r = "image/{0}".format(img_mime_type)
if not r:
r = MIMETYPE_UNKNOWN
self._mime_type = r
Expand Down
29 changes: 29 additions & 0 deletions emails/testsuite/store/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,35 @@ def test_attachment_headers():
assert 'X-Header: X' in part


def test_get_mime_type_by_extension():
f = emails.store.BaseFile(data='x', filename='logo.jpg')
assert f.mime_type == 'image/jpeg'


def test_get_mime_type_from_data_jpeg():
f = emails.store.BaseFile(
data=b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01',
filename='thumb',
)
assert f.mime_type == 'image/jpeg'


def test_get_mime_type_from_data_png():
f = emails.store.BaseFile(
data=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00d',
filename='thumb',
)
assert f.mime_type == 'image/png'


def test_get_mime_type_from_data_gif():
f = emails.store.BaseFile(
data=b'GIF89a\xd3\x00G\x00\xf7\x00\x00\xff\xff',
filename='thumb',
)
assert f.mime_type == 'image/gif'


def test_store_commons():
FILES = [{'data': 'aaa', 'filename': 'aaa.txt'}, {'data': 'bbb', 'filename': 'bbb.txt'}, ]
store = emails.store.MemoryFileStore()
Expand Down