|
46 | 46 | import codecs |
47 | 47 | import datetime |
48 | 48 | import enum |
| 49 | +import filetype |
49 | 50 | import functools |
50 | | -import imghdr |
51 | 51 | import logging |
52 | 52 | import math |
53 | 53 | import os |
|
78 | 78 | 'wav': 'WAVE', |
79 | 79 | } |
80 | 80 |
|
81 | | -PREFERRED_IMAGE_EXTENSIONS = {'jpeg': 'jpg'} |
82 | | - |
83 | 81 |
|
84 | 82 | # Exceptions. |
85 | 83 |
|
@@ -346,52 +344,15 @@ def _sc_encode(gain, peak): |
346 | 344 |
|
347 | 345 |
|
348 | 346 | # Cover art and other images. |
349 | | -def _imghdr_what_wrapper(data): |
350 | | - """A wrapper around imghdr.what to account for jpeg files that can only be |
351 | | - identified as such using their magic bytes |
352 | | - See #1545 |
353 | | - See https://github.com/file/file/blob/master/magic/Magdir/jpeg#L12 |
354 | | - """ |
355 | | - # imghdr.what returns none for jpegs with only the magic bytes, so |
356 | | - # _wider_test_jpeg is run in that case. It still returns None if it didn't |
357 | | - # match such a jpeg file. |
358 | | - return imghdr.what(None, h=data) or _wider_test_jpeg(data) |
359 | | - |
360 | | - |
361 | | -def _wider_test_jpeg(data): |
362 | | - """Test for a jpeg file following the UNIX file implementation which |
363 | | - uses the magic bytes rather than just looking for the bytes that |
364 | | - represent 'JFIF' or 'EXIF' at a fixed position. |
365 | | - """ |
366 | | - if data[:2] == b'\xff\xd8': |
367 | | - return 'jpeg' |
368 | | - |
369 | 347 |
|
370 | 348 | def image_mime_type(data): |
371 | 349 | """Return the MIME type of the image data (a bytestring). |
372 | 350 | """ |
373 | | - # This checks for a jpeg file with only the magic bytes (unrecognized by |
374 | | - # imghdr.what). imghdr.what returns none for that type of file, so |
375 | | - # _wider_test_jpeg is run in that case. It still returns None if it didn't |
376 | | - # match such a jpeg file. |
377 | | - kind = _imghdr_what_wrapper(data) |
378 | | - if kind in ['gif', 'jpeg', 'png', 'tiff', 'bmp']: |
379 | | - return 'image/{0}'.format(kind) |
380 | | - elif kind == 'pgm': |
381 | | - return 'image/x-portable-graymap' |
382 | | - elif kind == 'pbm': |
383 | | - return 'image/x-portable-bitmap' |
384 | | - elif kind == 'ppm': |
385 | | - return 'image/x-portable-pixmap' |
386 | | - elif kind == 'xbm': |
387 | | - return 'image/x-xbitmap' |
388 | | - else: |
389 | | - return 'image/x-{0}'.format(kind) |
| 351 | + return filetype.guess_mime(data) |
390 | 352 |
|
391 | 353 |
|
392 | 354 | def image_extension(data): |
393 | | - ext = _imghdr_what_wrapper(data) |
394 | | - return PREFERRED_IMAGE_EXTENSIONS.get(ext, ext) |
| 355 | + return filetype.guess_extension(data) |
395 | 356 |
|
396 | 357 |
|
397 | 358 | class ImageType(enum.Enum): |
|
0 commit comments