Class ImageSanitizationUtil

java.lang.Object
com.evolveum.midpoint.web.component.input.ImageSanitizationUtil

public final class ImageSanitizationUtil extends Object
Provides optional image sanitization, including format conversion and metadata removal. Sanitization rewrites the image using ImageIO. The rewrite does not preserve the original image metadata.
Author:
matisovaa
  • Method Details

    • getFileExtensionFromFileMagicNumber

      public static String getFileExtensionFromFileMagicNumber(byte[] fileBytes)
      Determines file extension by comparing first bytes of file byte array with known magic numbers.
      Parameters:
      fileBytes - file byte array to determine file extension
      Returns:
      file extension or null if file extension was not possible to determine
    • sanitizeImage

      public static byte[] sanitizeImage(byte[] originalBytes, ImageFormatType convertImageTo, boolean stripMetadata) throws ImageSanitizationException
      Applies the requested image processing. If neither conversion nor metadata stripping is requested, the original byte array is returned unchanged. Otherwise, the image is read and written again. When no target format is specified, the original format detected from the magic number is preserved.
      Parameters:
      originalBytes - image content to process
      convertImageTo - requested output format, or null to preserve the detected original format
      stripMetadata - whether the image should be rewritten to remove metadata
      Returns:
      processed image content, original content if processing is not requested, or null if the input is null
      Throws:
      ImageSanitizationException - if the image format cannot be determined or the image cannot be read or written