Files
king b576dd1a38 feat(asset): add photos, monitor links and deletion
Apply asset_db_002_photos_monitors.sql before rebuilding web.

Restrict soft deletion to the super admin; retain reports and audit.
2026-10-06 02:29:19 +09:00

39 lines
2.1 KiB
Python

"""사진을 실제 이미지로 확인하고 위치 정보 등 메타데이터를 제거합니다."""
import io
import warnings
from PIL import Image, ImageOps, UnidentifiedImageError
MAX_PHOTO_BYTES = 8 * 1024 * 1024
MAX_STORED_BYTES = 5 * 1024 * 1024
MAX_PHOTOS = 10
def prepare_photo(raw):
if not raw or len(raw) > MAX_PHOTO_BYTES:
raise ValueError("사진은 비어 있지 않은 8 MB 이하 파일이어야 합니다.")
try:
with warnings.catch_warnings():
warnings.simplefilter("error", Image.DecompressionBombWarning)
with Image.open(io.BytesIO(raw)) as source:
if source.format not in {"JPEG", "PNG", "WEBP"}:
raise ValueError("JPG, PNG, WebP 사진만 등록할 수 있습니다.")
if source.width * source.height > 20_000_000:
raise ValueError("사진은 2천만 화소 이하로 등록하세요.")
source.load()
image = ImageOps.exif_transpose(source)
image.thumbnail((2400, 2400))
transparent = "A" in image.getbands() or "transparency" in image.info
mode = "RGBA" if transparent else "RGB"
# 새 캔버스에 복사해 EXIF, GPS, 촬영기기 정보를 저장하지 않습니다.
clean = Image.new(mode, image.size)
clean.paste(image.convert(mode))
output = io.BytesIO()
clean.save(output, format="PNG" if transparent else "JPEG", **({} if transparent else {"quality": 88}))
data = output.getvalue()
if len(data) > MAX_STORED_BYTES:
raise ValueError("사진을 압축해 다시 등록하세요. 저장 크기는 5 MB 이하입니다.")
return data, "image/png" if transparent else "image/jpeg", clean.width, clean.height
except (UnidentifiedImageError, OSError, SyntaxError, Image.DecompressionBombError, Image.DecompressionBombWarning) as exc:
raise ValueError("사진을 읽을 수 없습니다. 정상적인 JPG, PNG, WebP 파일을 선택하세요.") from exc