Skip to content

Commit 3d56cd3

Browse files
committed
Refactor image type detection and update media type handling in catalog image task
- Introduced a new function, _detect_media_type, to determine the media type of images based on file magic bytes. - Replaced the previous content type validation with the new detection logic to enhance accuracy in identifying image formats. - Updated the image download process to utilize the new media type detection, improving overall robustness.
1 parent 4444876 commit 3d56cd3

1 file changed

Lines changed: 26 additions & 15 deletions

File tree

app/tasks/catalog_image.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,27 @@
3030
)
3131
}
3232

33-
_ALLOWED_MEDIA_TYPES = {"image/jpeg", "image/png", "image/gif", "image/webp"}
33+
_IMGTYPE_TO_MEDIA = {
34+
"jpeg": "image/jpeg",
35+
"png": "image/png",
36+
"gif": "image/gif",
37+
"webp": "image/webp",
38+
}
39+
40+
41+
def _detect_media_type(data: bytes) -> str | None:
42+
"""Detect the actual media type from file magic bytes."""
43+
if data[:3] == b"\xff\xd8\xff":
44+
return "image/jpeg"
45+
if data[:8] == b"\x89PNG\r\n\x1a\n":
46+
return "image/png"
47+
if data[:4] == b"RIFF" and data[8:12] == b"WEBP":
48+
return "image/webp"
49+
if data[:6] in (b"GIF87a", b"GIF89a"):
50+
return "image/gif"
51+
import imghdr
52+
kind = imghdr.what(None, h=data)
53+
return _IMGTYPE_TO_MEDIA.get(kind) if kind else None
3454

3555

3656
# ---------------------------------------------------------------------------
@@ -114,23 +134,14 @@ def _download_images(urls: list[str]) -> list[tuple[str, bytes, str]]:
114134
if resp.status_code != 200:
115135
continue
116136

117-
content_type = resp.headers.get("Content-Type", "").split(";")[0].strip()
118-
if content_type not in _ALLOWED_MEDIA_TYPES:
119-
if url.lower().endswith((".jpg", ".jpeg")):
120-
content_type = "image/jpeg"
121-
elif url.lower().endswith(".png"):
122-
content_type = "image/png"
123-
elif url.lower().endswith(".webp"):
124-
content_type = "image/webp"
125-
elif url.lower().endswith(".gif"):
126-
content_type = "image/gif"
127-
else:
128-
continue
129-
130137
if len(resp.content) < 1000:
131138
continue
132139

133-
results.append((url, resp.content, content_type))
140+
media_type = _detect_media_type(resp.content)
141+
if not media_type:
142+
continue
143+
144+
results.append((url, resp.content, media_type))
134145
except requests.RequestException:
135146
continue
136147
return results

0 commit comments

Comments
 (0)