Skip to content

Commit

Permalink
More liberal jpeg detection (Fixes #58) (#59)
Browse files Browse the repository at this point in the history
* More liberal jpeg detection (Fixes #58)

imghdr does not detect all jpeg files, let's accept any data
starting with the JPEG SOI marker.
See https://stackoverflow.com/questions/36870661/imghdr-python-cant-detec-type-of-some-images-image-extension
for more details.
  • Loading branch information
kingosticks committed Feb 24, 2021
1 parent 00e3ef4 commit 019468e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mopidy_local/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ def get_image_size_jpeg(data):
return width, height


def test_jpeg(data, file_handle):
# Additional JPEG detection looking for JPEG SOI marker
if data[:2] == b"\xff\xd8":
return "jpeg"


imghdr.tests.append(test_jpeg)


class LocalStorageProvider:
def __init__(self, config):
self._config = ext_config = config[Extension.ext_name]
Expand Down
16 changes: 16 additions & 0 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

from mopidy_local import storage


@pytest.mark.parametrize(
"data",
[
pytest.param("ffd8ffe000104a46494600", id="JFIF"),
pytest.param("ffd8ffe100184578696600", id="Exif"),
pytest.param("ffd8ffe1095068747470", id="XMP"),
],
)
def test_jpeg_detection(data):
data_bytes = bytes.fromhex(data)
assert storage.imghdr.what(None, data_bytes) is not None

0 comments on commit 019468e

Please sign in to comment.