Skip to content

Commit

Permalink
Merge pull request #44 from eea/develop
Browse files Browse the repository at this point in the history
Improve svg upgrade step to know which svgs are fixed and not revisit them
  • Loading branch information
avoinea authored Nov 25, 2024
2 parents 88e9676 + 385616d commit 3316c72
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
5 changes: 5 additions & 0 deletions docs/HISTORY.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

5.7 - (2024-11-24)
---------------------------
* Change: Improve svg upgrade step to know which svgs are fixed and not revisit them
[avoinea]

5.6 - (2024-11-11)
---------------------------
* Feature: customized actions endpoint to allow passing of props to the
Expand Down
27 changes: 14 additions & 13 deletions eea/volto/policy/upgrades/upgrade_svgs/upgrade_svg.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,61 @@
"""Upgrade step for svgs to fix width and height"""

import logging
import transaction
from plone.namedfile.utils import getImageInfo
from zope.lifecycleevent import modified
from zope.annotation.interfaces import IAnnotations

logger = logging.getLogger("upgrade_svgs")
logger.setLevel(logging.INFO)
from Products.ZCatalog.ProgressHandler import ZLogHandler


def upgrade_svgs(portal):
"""Upgrade SVG dimensions"""

i = 0
brains = portal.portal_catalog()
total = len(brains)
pghandler = ZLogHandler(100)
pghandler.init("Recalculate svgs width and height", len(brains))
for idx, brain in enumerate(brains):
pghandler.report(idx)
obj = dict()
try:
obj = brain.getObject()
except Exception:
continue # Skip to the next item if there's an error

# Process the main image only if dimensions are less than 5
if (
hasattr(obj, "image") and
hasattr(obj.image, "_width") and
hasattr(obj.image, "_height")
hasattr(obj.image, "_height") and
(obj.image._width < 5 or obj.image._height < 5)
):

contentType, width, height = getImageInfo(obj.image.data)
if contentType == "image/svg+xml":
logger.info("Processing %s", obj.absolute_url())
obj.image._width = width
obj.image._height = height
anno = IAnnotations(obj)
if "plone.scale" in anno:
del anno["plone.scale"]
modified(obj)
i += 1

# Process the preview image only if dimensions are less than 5
if (
hasattr(obj, "preview_image") and
hasattr(obj.preview_image, "_width") and
hasattr(obj.preview_image, "_height")
hasattr(obj.preview_image, "_height") and
(obj.preview_image._width < 5 or obj.preview_image._height < 5)
):
contentType, width, height = getImageInfo(obj.preview_image.data)
if contentType == "image/svg+xml":
logger.info("Processing %s", obj.absolute_url())
obj.preview_image._width = width
obj.preview_image._height = height
anno = IAnnotations(obj)
if "plone.scale" in anno:
del anno["plone.scale"]
modified(obj)
i += 1

if not i % 100:
logger.warning("Progress %s/%s", idx, total)
logger.warning("Modified %s items", i)
transaction.commit()
pghandler.finish()
transaction.commit()
2 changes: 1 addition & 1 deletion eea/volto/policy/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.6
5.7

0 comments on commit 3316c72

Please sign in to comment.