View Javadoc
1   /**
2    * This file Copyright (c) 2013-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.imageprovider;
35  
36  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
37  
38  import org.apache.commons.lang3.StringUtils;
39  
40  import com.vaadin.v7.data.Item;
41  
42  /**
43   * This abstract implementation of {@link ImageProvider} covers resolution of icon class names from the Magnolia icon-font,
44   * as well as "translating" arbitrary itemIds to proper Vaadin {@link Item Items}, through the {@link ContentConnector}.
45   *
46   * @see DefaultImageProvider
47   * @deprecated since 6.0. Use a custom column formatter to resolve the icon.
48   */
49  @Deprecated
50  public abstract class AbstractImageProvider implements ImageProvider {
51  
52      private final ContentConnector contentConnector;
53  
54      protected AbstractImageProvider(ContentConnector contentConnector) {
55          this.contentConnector = contentConnector;
56      }
57  
58      /**
59       * Resolves the image {@link com.vaadin.server.Resource Resource} or icon class name, as expected by {@link #getThumbnailResource(Object, String)}.
60       *
61       * @see ImageProvider#getThumbnailResource(Object, String)
62       */
63      protected abstract Object resolveImageResource(Item item, String generator);
64  
65      /**
66       * Resolves the link to a preview image for the given content and {@link info.magnolia.imaging.ImageGenerator ImageGenerator}.
67       *
68       * @see ImageProvider#getPortraitPath(Object)
69       * @see ImageProvider#getThumbnailPath(Object)
70       */
71      protected abstract String resolveImagePath(Item item, String generator);
72  
73      @Override
74      public String getPortraitPath(Object itemId) {
75          Item item = contentConnector.getItem(itemId);
76          return resolveImagePath(item, PORTRAIT_GENERATOR);
77      }
78  
79      @Override
80      public String getThumbnailPath(Object itemId) {
81          Item item = contentConnector.getItem(itemId);
82          return resolveImagePath(item, THUMBNAIL_GENERATOR);
83      }
84  
85      @Override
86      public Object getThumbnailResource(Object itemId, String generator) {
87          Item item = contentConnector.getItem(itemId);
88          return resolveImageResource(item, generator);
89      }
90  
91      @Override
92      public String resolveIconClassName(String mimeType) {
93          String fileType = resolveFileTypeFromMimeType(mimeType);
94  
95          if (!"".equals(fileType)) {
96              return "file-" + fileType;
97          }
98  
99          return "file";
100     }
101 
102     private String resolveFileTypeFromMimeType(String mimeType) {
103         if (StringUtils.isBlank(mimeType)) {
104             return StringUtils.EMPTY;
105         }
106         if (mimeType.contains("application/pdf")) {
107             return "pdf";
108         }
109         if (mimeType.matches("application.*(msword)")) {
110             return "word";
111         }
112         if (mimeType.matches("application.*(excel|xls)")) {
113             return "excel";
114         }
115         if (mimeType.matches("application.*(powerpoint)")) {
116             return "powerpoint";
117         }
118         if (mimeType.contains("text/")) {
119             return "text";
120         }
121         if (mimeType.contains("image/")) {
122             return "image";
123         }
124         if (mimeType.contains("video/")) {
125             return "video";
126         }
127         if (mimeType.contains("audio/")) {
128             return "audio";
129         }
130         if (mimeType.matches(".*(zip|compress)")) {
131             return StringUtils.EMPTY;
132         }
133 
134         return StringUtils.EMPTY;
135     }
136 
137     protected boolean isImage(String mimeType) {
138         return mimeType != null && mimeType.matches("image.*");
139     }
140 }