Greg Claude Opus 4.7 (1M context) commited on
Commit
34ae3e8
·
1 Parent(s): aa308f0

feat(ta-01): add photo_pipeline with WebP compression and validation

Browse files

Adds image processing service with:
- MIME type validation (PNG, JPEG, WebP only)
- EXIF transpose handling
- Thumbnail scaling to max 800x800
- WebP conversion with quality 85
- Test suite with 5 comprehensive test cases

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

backend/app/services/__init__.py ADDED
File without changes
backend/app/services/photo_pipeline.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import BytesIO
2
+ from PIL import Image, ImageOps
3
+ import magic
4
+
5
+ ALLOWED_MIME = {"image/png", "image/jpeg", "image/webp"}
6
+ MAX_INPUT_BYTES = 5 * 1024 * 1024
7
+ TARGET_MAX_DIM = 800
8
+ WEBP_QUALITY = 85
9
+
10
+
11
+ def process_user_photo(raw: bytes) -> bytes:
12
+ if len(raw) > MAX_INPUT_BYTES:
13
+ raise ValueError(f"photo exceeds {MAX_INPUT_BYTES} bytes")
14
+ mime = magic.from_buffer(raw, mime=True)
15
+ if mime not in ALLOWED_MIME:
16
+ raise ValueError(f"unsupported mime: {mime}")
17
+ img = Image.open(BytesIO(raw))
18
+ img = ImageOps.exif_transpose(img)
19
+ img.thumbnail((TARGET_MAX_DIM, TARGET_MAX_DIM), Image.LANCZOS)
20
+ if img.mode in ("RGBA", "P", "LA"):
21
+ img = img.convert("RGB")
22
+ out = BytesIO()
23
+ img.save(out, format="WEBP", quality=WEBP_QUALITY, method=6)
24
+ return out.getvalue()
backend/tests/services/__init__.py ADDED
File without changes
backend/tests/services/test_photo_pipeline.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import BytesIO
2
+ import pytest
3
+ from PIL import Image
4
+ from app.services.photo_pipeline import process_user_photo, MAX_INPUT_BYTES, TARGET_MAX_DIM
5
+
6
+
7
+ def _png(width: int, height: int) -> bytes:
8
+ img = Image.new("RGB", (width, height), color=(123, 45, 67))
9
+ buf = BytesIO()
10
+ img.save(buf, format="PNG")
11
+ return buf.getvalue()
12
+
13
+
14
+ def _jpeg(width: int, height: int) -> bytes:
15
+ img = Image.new("RGB", (width, height), color=(10, 20, 30))
16
+ buf = BytesIO()
17
+ img.save(buf, format="JPEG")
18
+ return buf.getvalue()
19
+
20
+
21
+ def test_accepts_png_and_returns_webp():
22
+ out = process_user_photo(_png(400, 300))
23
+ img = Image.open(BytesIO(out))
24
+ assert img.format == "WEBP"
25
+ assert img.size == (400, 300)
26
+
27
+
28
+ def test_accepts_jpeg_and_resizes_large_image():
29
+ out = process_user_photo(_jpeg(2000, 1000))
30
+ img = Image.open(BytesIO(out))
31
+ assert img.format == "WEBP"
32
+ assert max(img.size) == TARGET_MAX_DIM
33
+
34
+
35
+ def test_rejects_oversize_input():
36
+ big = _png(50, 50) + b"\x00" * (MAX_INPUT_BYTES + 1)
37
+ with pytest.raises(ValueError, match="exceeds"):
38
+ process_user_photo(big)
39
+
40
+
41
+ def test_rejects_gif():
42
+ img = Image.new("RGB", (10, 10))
43
+ buf = BytesIO()
44
+ img.save(buf, format="GIF")
45
+ with pytest.raises(ValueError, match="unsupported"):
46
+ process_user_photo(buf.getvalue())
47
+
48
+
49
+ def test_rejects_non_image_bytes():
50
+ with pytest.raises(ValueError, match="unsupported"):
51
+ process_user_photo(b"not-an-image" * 10)