View Javadoc
1   /**
2    * This file Copyright (c) 2014-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.dam.preview.imageprovider;
35  
36  import info.magnolia.link.LinkUtil;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.jcr.util.NodeTypes;
39  import info.magnolia.ui.imageprovider.DefaultImageProvider;
40  import info.magnolia.ui.imageprovider.definition.ImageProviderDefinition;
41  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
42  
43  import java.util.Calendar;
44  
45  import javax.inject.Inject;
46  import javax.jcr.Node;
47  import javax.jcr.RepositoryException;
48  
49  import org.apache.commons.lang3.StringUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  /**
54   * This implementation of image provider, is able to provide portrait or thumbnail images only for objects of mime
55   * type image/*. It will not store preview of image under the image node itself, instead it relies on imaging module
56   * to generate and store the preview.
57   */
58  public class MultiGeneratorImageProvider extends DefaultImageProvider {
59  
60      private static final Logger log = LoggerFactory.getLogger(MultiGeneratorImageProvider.class);
61  
62      public final String ICON_CLASS_DEFAULT = "file";
63  
64      private MultiGeneratorImageProviderDefinition definition;
65  
66      private ContentConnector contentConnector;
67  
68      @Inject
69      public MultiGeneratorImageProvider(ImageProviderDefinition definition, ContentConnector contentConnector) {
70          super(definition, contentConnector);
71          this.definition = (MultiGeneratorImageProviderDefinition) definition;
72          this.contentConnector = contentConnector;
73      }
74  
75      @Override
76      protected String getGeneratorImagePath(String workspace, Node node, String defaultGenerator) {
77          String imagePath = null;
78  
79          if (node != null) {
80              try {
81                  Node imageNode = node.getNode(definition.getOriginalImageNodeName());
82  
83                  if (imageNode.hasProperty("extension")) {
84                      String ext = imageNode.getProperty("extension").getString();
85                      String generator = definition.getGenerators().get(ext);
86                      if (StringUtils.isNotBlank(generator)) {
87                          defaultGenerator += "-" + generator;
88                      }
89                  }
90                  String imageName;
91                  if (imageNode.hasProperty("fileName")) {
92                      imageName = imageNode.getProperty("fileName").getString();
93                  } else {
94                      imageName = node.getName();
95                  }
96  
97                  imagePath = MgnlContext.getContextPath() + "/" + definition.getImagingServletPath() + "/" + defaultGenerator + "/" + workspace + "/" + imageNode.getIdentifier() + "/" + imageName + "." + definition.getImageExtension();
98  
99                  // Add cache fingerprint so that browser caches asset only until asset is modified.
100                 Calendar lastModified = NodeTypes.LastModified.getLastModified(node);
101                 imagePath = LinkUtil.addFingerprintToLink(imagePath, lastModified);
102 
103             } catch (RepositoryException e) {
104                 log.warn("Could not get name or identifier from imageNode: {}", e.getMessage());
105             }
106         }
107 
108         return imagePath;
109     }
110 
111     @Override
112     protected boolean isImage(String mimeType) {
113         return super.isImage(mimeType) || (mimeType != null && definition.getGenerators().keySet().contains(StringUtils.substringAfter(mimeType, "/").toLowerCase()));
114     }
115 }