MediaFileType.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2019 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.handler.media;

  21. import java.util.EnumSet;
  22. import java.util.Set;
  23. import java.util.stream.Collectors;
  24. import java.util.stream.Stream;

  25. import org.apache.commons.lang3.StringUtils;
  26. import org.jetbrains.annotations.NotNull;
  27. import org.jetbrains.annotations.Nullable;
  28. import org.osgi.annotation.versioning.ProviderType;

  29. import io.wcm.wcm.commons.contenttype.ContentType;
  30. import io.wcm.wcm.commons.contenttype.FileExtension;

  31. /**
  32.  * File types supported by Media Handler.
  33.  */
  34. @ProviderType
  35. public enum MediaFileType {

  36.   /**
  37.    * JPEG
  38.    */
  39.   JPEG(new String[] { ContentType.JPEG }, new String[] { FileExtension.JPEG, "jpeg" }, true),

  40.   /**
  41.    * PNG
  42.    */
  43.   PNG(new String[] { ContentType.PNG }, new String[] { FileExtension.PNG }, false),

  44.   /**
  45.    * GIF
  46.    */
  47.   GIF(new String[] { ContentType.GIF }, new String[] { FileExtension.GIF }, false),

  48.   /**
  49.    * TIFF
  50.    */
  51.   TIFF(new String[] { ContentType.TIFF }, new String[] { FileExtension.TIFF, "tiff" }, false),

  52.   /**
  53.    * SVG
  54.    */
  55.   SVG(new String[] { ContentType.SVG }, new String[] { FileExtension.SVG }, false);


  56.   private final Set<String> contentTypes;
  57.   private final Set<String> extensions;
  58.   private final boolean imageQualityPercentage;

  59.   @SuppressWarnings("null")
  60.   MediaFileType(@NotNull String @NotNull [] contentTypes,
  61.       @NotNull String @NotNull [] extensions,
  62.       boolean imageQualityPercentage) {
  63.     this.contentTypes = Set.of(contentTypes);
  64.     this.extensions = Set.of(extensions);
  65.     this.imageQualityPercentage = imageQualityPercentage;
  66.   }

  67.   /**
  68.    * @return Content types
  69.    */
  70.   public Set<String> getContentTypes() {
  71.     return this.contentTypes;
  72.   }

  73.   /**
  74.    * @return File extensions
  75.    */
  76.   public Set<String> getExtensions() {
  77.     return extensions;
  78.   }

  79.   /**
  80.    * @return true if this image type has lossy compression and image quality is specified in percentage
  81.    */
  82.   public boolean isImageQualityPercentage() {
  83.     return imageQualityPercentage;
  84.   }

  85.   /**
  86.    * All file types that are supported by the Media Handler for rendering as image.
  87.    */
  88.   private static final EnumSet<MediaFileType> IMAGE_FILE_TYPES = EnumSet.of(
  89.       GIF,
  90.       JPEG,
  91.       PNG,
  92.       TIFF,
  93.       SVG);

  94.   /**
  95.    * All file types that are supported by the browser for direct display.
  96.    */
  97.   private static final EnumSet<MediaFileType> BROWSER_IMAGE_FILE_TYPES = EnumSet.of(
  98.       GIF,
  99.       JPEG,
  100.       PNG,
  101.       SVG);

  102.   /**
  103.    * All file types that are vector formats and can be scaled by the browser.
  104.    */
  105.   private static final EnumSet<MediaFileType> VECTOR_IMAGE_FILE_TYPES = EnumSet.of(
  106.       SVG);

  107.   /**
  108.    * Check if the given file extension is supported by the Media Handler for rendering as image.
  109.    * @param fileExtension File extension
  110.    * @return true if image
  111.    */
  112.   public static boolean isImage(@Nullable String fileExtension) {
  113.     return isExtension(IMAGE_FILE_TYPES, fileExtension);
  114.   }

  115.   /**
  116.    * @return Image file extensions supported by the Media Handler for rendering as image.
  117.    */
  118.   public static @NotNull Set<String> getImageFileExtensions() {
  119.     return getFileExtensions(IMAGE_FILE_TYPES);
  120.   }

  121.   /**
  122.    * @return Image content types supported by the Media Handler for rendering as image.
  123.    */
  124.   public static @NotNull Set<String> getImageContentTypes() {
  125.     return getContentTypes(IMAGE_FILE_TYPES);
  126.   }

  127.   /**
  128.    * Check if the given file extension is supported for direct display in a browser.
  129.    * @param fileExtension File extension
  130.    * @return true if image is supported in browsers
  131.    */
  132.   public static boolean isBrowserImage(@Nullable String fileExtension) {
  133.     return isExtension(BROWSER_IMAGE_FILE_TYPES, fileExtension);
  134.   }

  135.   /**
  136.    * @return Image file extensions supported for direct display in a browser.
  137.    */
  138.   public static @NotNull Set<String> getBrowserImageFileExtensions() {
  139.     return getFileExtensions(BROWSER_IMAGE_FILE_TYPES);
  140.   }

  141.   /**
  142.    * @return Image content types supported for direct display in a browser.
  143.    */
  144.   public static @NotNull Set<String> getBrowserImageContentTypes() {
  145.     return getContentTypes(BROWSER_IMAGE_FILE_TYPES);
  146.   }

  147.   /**
  148.    * Check if the given file extension is a vector image file extension.
  149.    * @param fileExtension File extension
  150.    * @return true if image is a vector image.
  151.    */
  152.   public static boolean isVectorImage(@Nullable String fileExtension) {
  153.     return isExtension(VECTOR_IMAGE_FILE_TYPES, fileExtension);
  154.   }

  155.   /**
  156.    * @return Image file extensions that are vector images.
  157.    */
  158.   public static @NotNull Set<String> getVectorImageFileExtensions() {
  159.     return getFileExtensions(VECTOR_IMAGE_FILE_TYPES);
  160.   }

  161.   /**
  162.    * @return Image content types that are vector images.
  163.    */
  164.   public static @NotNull Set<String> getVectorImageContentTypes() {
  165.     return getContentTypes(VECTOR_IMAGE_FILE_TYPES);
  166.   }

  167.   private static Set<String> getContentTypes(@NotNull EnumSet<MediaFileType> fileTypes) {
  168.     return fileTypes.stream()
  169.         .flatMap(type -> type.getContentTypes().stream())
  170.         .collect(Collectors.toSet());
  171.   }

  172.   private static boolean isExtension(@NotNull EnumSet<MediaFileType> fileTypes, @Nullable String fileExtension) {
  173.     if (StringUtils.isEmpty(fileExtension)) {
  174.       return false;
  175.     }
  176.     return fileTypes.stream()
  177.         .anyMatch(type -> type.getExtensions().contains(StringUtils.lowerCase(fileExtension)));
  178.   }

  179.   private static Set<String> getFileExtensions(@NotNull EnumSet<MediaFileType> fileTypes) {
  180.     return fileTypes.stream()
  181.         .flatMap(type -> type.getExtensions().stream())
  182.         .collect(Collectors.toSet());
  183.   }

  184.   /**
  185.    * Get Media file type by content type.
  186.    * @param contentType Content type
  187.    * @return Media file type or null if not found
  188.    */
  189.   @SuppressWarnings("null")
  190.   public static @Nullable MediaFileType getByContentType(@Nullable String contentType) {
  191.     if (contentType == null) {
  192.       return null;
  193.     }
  194.     String contentTypeLowerCase = StringUtils.toRootLowerCase(contentType);
  195.     return Stream.of(values())
  196.         .filter(type -> type.getContentTypes().contains(contentTypeLowerCase))
  197.         .findFirst()
  198.         .orElse(null);
  199.   }

  200.   /**
  201.    * Get Media file type by file extension.
  202.    * @param extension File extension
  203.    * @return Media file type or null if not found
  204.    */
  205.   @SuppressWarnings("null")
  206.   public static @Nullable MediaFileType getByFileExtensions(@Nullable String extension) {
  207.     if (extension == null) {
  208.       return null;
  209.     }
  210.     String extensionLowerCase = StringUtils.toRootLowerCase(extension);
  211.     return Stream.of(values())
  212.         .filter(type -> type.getExtensions().contains(extensionLowerCase))
  213.         .findFirst()
  214.         .orElse(null);
  215.   }

  216. }