fix: resize oversized image attachments during import

This commit is contained in:
2026-08-05 15:56:09 +02:00
parent 80469ede34
commit 05e33a87a9
6 changed files with 163 additions and 18 deletions
+40
View File
@@ -4,6 +4,7 @@ from django.core.files.storage import InMemoryStorage
from django.db import models
from PIL import Image
from netbox_export.services import importer as importer_module
from netbox_export.services.importer import _cleanup_files, _set_files
@@ -65,3 +66,42 @@ def test_file_import_keeps_derived_dimensions_after_storage_save(monkeypatch):
_cleanup_files(saved_files)
assert not storage.exists(obj.image.name)
def test_oversized_image_is_resized_after_pillow_bomb_error(monkeypatch):
monkeypatch.setattr(importer_module, "NETBOX_IMAGE_MAX_PIXELS", 50)
monkeypatch.setattr(importer_module, "IMPORTED_IMAGE_TARGET_PIXELS", 40)
monkeypatch.setattr(importer_module, "IMPORTED_IMAGE_SOURCE_MAX_PIXELS", 200)
monkeypatch.setattr(Image, "MAX_IMAGE_PIXELS", 50)
field = ImageAsset._meta.get_field("image")
storage = InMemoryStorage()
monkeypatch.setattr(field, "storage", storage)
obj = ImageAsset()
record = image_record()
record["id"] = "extras.imageattachment:17"
assets = {"assets/extras.imageattachment_17/image/server-room.png": image_bytes(11, 10)}
import_warnings = []
saved_files = []
_set_files(
obj,
record,
assets,
saved_files,
dry_run=False,
import_warnings=import_warnings,
)
assert obj.image_width * obj.image_height <= 40
with storage.open(obj.image.name, "rb") as stored_file, Image.open(stored_file) as stored_image:
assert stored_image.size == (obj.image_width, obj.image_height)
assert stored_image.format == "PNG"
assert import_warnings == [
(
f"Bild für extras.imageattachment:17 wurde von 11×10 auf "
f"{obj.image_width}×{obj.image_height} Pixel verkleinert."
)
]
assert Image.MAX_IMAGE_PIXELS == 50
_cleanup_files(saved_files)