Groovy BinariesInfoScript

Overview

The BinariesInfoScript is an updater script that logs details about binaries—such as assets or images—selected using a specified XPath query.

Functionality

This script locates assets and images in the repository that match the provided size constraints. For each binary found, it logs the primary type, MIME type, size, and path. The script also reports the total number of binaries visited and the number that meet the defined size limits.

XPath Query Examples

To use the script, provide an XPath query that selects JCR hippo:resource nodes.

  • All images:
content/gallery//element(*,hippo:resource)
  • All assets:
content/assets//element(*,hippo:resource)
  • All Microsoft Excel assets:
content/assets//element(*,hippo:resource)[@jcr:mimeType="application/vnd.ms-excel"]
  • All PDF resources embedded in documents:
content/documents//element(*, hippo:resource)[@jcr:mimeType='application/pdf']

Script Parameters

Configure the script using the following parameters:

{"lowerLimit": "1k", "upperLimit": "10M", "verbose":  "true"}

lowerLimit/upperLimit: Optional. Set the lower and upper size limits (inclusive) for binaries to include. Supported suffixes: k, M, G.
verbose: true (default): Lists binaries within the limits (shows primaryType, mimeType, size, path) and a summary.
        false: Outputs only the summary (number of visited binaries and number within the limits).

Script

package org.hippoecm.frontend.plugins.cms.admin.updater import org.onehippo.repository.update.BaseNodeUpdateVisitor import javax.jcr.Node import javax.jcr.RepositoryException import javax.jcr.Session /** * Finds binaries (assets or images) in the repository and logs their type, MIME type, path, and size if their size * is within the configured limits. * * XPath query (example): content/assets//element(*,hippo:resource) * * Parameters: {"lowerLimit": "1k", "upperLimit": "10M", "verbose": "true"} * lowerLimit/upperLimit: Optional. Lower and upper size limits for binaries (inclusive). Supports k, M, G suffixes. * verbose: true (default): Outputs the list of binaries within the limits and a summary. * false: Outputs only the summary. */ class BinariesInfoScript extends BaseNodeUpdateVisitor { Boolean verbose = null String configuredLowerLimit = null String configuredUpperLimit = null Long lowerLimit = null Long upperLimit = null List<BinariesInfo> binariesWithinLimits = new ArrayList<BinariesInfo>() int totalVisitedNodes = 0 void initialize(Session session) throws RepositoryException { verbose = !("false" == parametersMap.get("verbose")) configuredLowerLimit = parametersMap.get("lowerLimit") configuredUpperLimit = parametersMap.get("upperLimit") lowerLimit = getSize(configuredLowerLimit, 0L) upperLimit = getSize(configuredUpperLimit, Long.MAX_VALUE) log.info "BinariesInfoScript initialized with parameters lowerLimit=${configuredLowerLimit}, " + "upperLimit=${configuredUpperLimit}, verbose=${verbose}." } boolean logSkippedNodePaths() { return false // Do not log skipped node paths } boolean skipCheckoutNodes() { return false // Return true for read-only visitors or updates unrelated to versioned content } Node firstNode(final Session session) throws RepositoryException { return null // Implement for custom node selection/navigation if needed } Node nextNode() throws RepositoryException { return null // Implement for custom node selection/navigation if needed } boolean doUpdate(Node node) { totalVisitedNodes++ log.debug "BinariesInfoScript visiting node ${node.path} of type ${node.primaryNodeType.name}" if (node.hasProperty("jcr:data")) { long size = node.getProperty("jcr:data").getLength() if ((size >= lowerLimit) && (size <= upperLimit)) { final String primaryType = node.getProperty("jcr:primaryType").getString(); final String mimeType = node.hasProperty("jcr:mimeType") ? node.getProperty("jcr:mimeType").getString() : "none" binariesWithinLimits.add(new BinariesInfo(primaryType, mimeType, size, node.getPath())) } return true } log.warn "Unexpectedly, node ${node.getPath()} does not have property jcr:data" return false } boolean undoUpdate(Node node) { throw new UnsupportedOperationException('Updater does not implement undoUpdate method') } void destroy() { logOutput() } void logOutput() { StringBuilder sb = new StringBuilder() if (verbose && binariesWithinLimits.size() > 0) { sb.append("Detailed binaries information (primaryType, mimeType, size, path):\n") for (BinariesInfo info : binariesWithinLimits) { sb.append(" ").append(info.getPrimaryType()).append(", ").append(info.getMimeType()).append(", ") sb.append(info.getSize()).append(", ").append(info.getPath()).append("\n") } } sb.append("Summary of binaries information:\n") sb.append(" Total of visited binaries is ").append(totalVisitedNodes).append(".\n") sb.append(" Found ").append(binariesWithinLimits.size()).append(" binaries") sb.append(" larger than ").append(lowerLimit).append(" bytes") if ((configuredLowerLimit != null) && (configuredLowerLimit.length() > 0)) { sb.append(" (").append(configuredLowerLimit).append(")") } if (upperLimit != Long.MAX_VALUE) { sb.append(" and smaller than ").append(upperLimit).append(" bytes (").append(configuredUpperLimit).append(")") } sb.append(".\n") log.info(sb.toString()) } Long getSize(final String configuredSize, final Long defaultValue) { if ((configuredSize == null) || configuredSize.length() == 0) { return defaultValue } try { if (configuredSize.endsWith("G")) { return Long.parseLong(configuredSize.substring(0, configuredSize.length()-1)) * 1024 * 1024 * 1024; } else if (configuredSize.endsWith("M")) { return Long.parseLong(configuredSize.substring(0, configuredSize.length()-1)) * 1024 * 1024 } else if (configuredSize.endsWith("k")) { return Long.parseLong(configuredSize.substring(0, configuredSize.length()-1)) * 1024 } else { return Long.parseLong(configuredSize) } } catch (NumberFormatException nfe) { log.error("Configured size {} cannot be parsed into a long. Defaulting to {}", configuredSize, defaultValue); return defaultValue } } /** * Information about a found binary. */ class BinariesInfo { private final String primaryType private final String mimeType private final long size private final String path BinariesInfo(final String primaryType, final String mimeType, final long size, final String path) { this.primaryType = primaryType this.mimeType = mimeType this.size = size this.path = path } String getPrimaryType() { return primaryType } String getMimeType() { return mimeType } long getSize() { return size } String getPath() { return path } } }
Share Feedback
Page: /build/content-updates/groovy-binariesinfoscript
Section: Build
Category *
Groovy BinariesInfoScript | Bloomreach Content Documentation