Skip to content

Commit

Permalink
Validate svg icons with ET
Browse files Browse the repository at this point in the history
  • Loading branch information
Xpirix committed Jun 24, 2024
1 parent 6268c9a commit a5de0aa
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion qgis-app/plugins/templatetags/plugin_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django import template
from PIL import Image, UnidentifiedImageError
import xml.etree.ElementTree as ET

register = template.Library()

Expand Down Expand Up @@ -34,8 +35,23 @@ def file_extension(value):
def is_image_valid(image):
if not image:
return False
# Check if the file is an SVG by extension
if image.path.lower().endswith('.svg'):
return _validate_svg(image.path)
return _validate_image(image.path)


def _validate_svg(file_path):
try:
# Parse the SVG file to ensure it's well-formed XML
ET.parse(file_path)
return True
except (ET.ParseError, FileNotFoundError):
return False

def _validate_image(file_path):
try:
img = Image.open(image.path)
img = Image.open(file_path)
img.verify()
return True
except (FileNotFoundError, UnidentifiedImageError):
Expand Down

0 comments on commit a5de0aa

Please sign in to comment.