-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from eea/develop
Improve svg upgrade step to know which svgs are fixed and not revisit them
- Loading branch information
Showing
3 changed files
with
20 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
5.6 | ||
5.7 |