feat(thumb-backfill): _thumb_is_valid helper — JPEG/PNG magic-byte check on the on-disk thumbnail file

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-26 10:35:06 -04:00
parent 2505b197ae
commit 7aa7f5a3d6
2 changed files with 59 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
"""Thumbnail backfill: _thumb_is_valid helper + backfill_thumbnails planner."""
import pytest
from backend.app.tasks.thumbnail import _thumb_is_valid
pytestmark = pytest.mark.integration
def test_thumb_is_valid_jpeg(tmp_path):
p = tmp_path / "good.jpg"
p.write_bytes(b"\xff\xd8\xff\xe0" + b"\x00" * 100)
assert _thumb_is_valid(p) is True
def test_thumb_is_valid_png(tmp_path):
p = tmp_path / "good.png"
p.write_bytes(b"\x89PNG\r\n\x1a\n" + b"\x00" * 100)
assert _thumb_is_valid(p) is True
def test_thumb_is_valid_too_short(tmp_path):
p = tmp_path / "tiny"
p.write_bytes(b"\xff\xd8")
assert _thumb_is_valid(p) is False
def test_thumb_is_valid_wrong_magic(tmp_path):
p = tmp_path / "garbage"
p.write_bytes(b"\x00" * 12)
assert _thumb_is_valid(p) is False
def test_thumb_is_valid_missing_file(tmp_path):
assert _thumb_is_valid(tmp_path / "nope") is False