View Javadoc
1   /**
2    * This file Copyright (c) 2014-2016 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.app.ui.field.upload;
35  
36  import info.magnolia.cms.core.Path;
37  import info.magnolia.dam.app.DamAppConfiguration;
38  import info.magnolia.dam.app.ui.field.configuration.EditAssetAppConfiguration;
39  import info.magnolia.i18nsystem.SimpleTranslator;
40  import info.magnolia.ui.form.field.upload.UploadReceiver;
41  
42  import java.io.InputStream;
43  import java.io.OutputStream;
44  
45  import javax.inject.Inject;
46  
47  import org.apache.commons.io.IOUtils;
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  import com.google.common.net.MediaType;
52  
53  /**
54   * Asset specific implementation of {@link UploadReceiver}.
55   */
56  public class AssetUploadReceiver extends UploadReceiver {
57      private static final Logger log = LoggerFactory.getLogger(AssetUploadReceiver.class);
58  
59      private final DamAppConfiguration appConfig;
60      private ImageSize imageSize;
61      private long duration;
62      private EditAssetAppConfiguration editConfig;
63  
64      @Inject
65      public AssetUploadReceiver(SimpleTranslator i18n, DamAppConfiguration damAppConfig) {
66          super(Path.getTempDirectory(), i18n);
67          this.appConfig = damAppConfig;
68      }
69  
70      /**
71       * Set in addition the {@link ImageSize} and duration properties.
72       */
73      @Override
74      public void setValue(Object newValue) {
75          super.setValue(newValue);
76          // reload the app configuration based on the new MediaType
77          updateRelatedProperties();
78      }
79  
80      /**
81       * @return true if the current asset is an image, false otherwise.
82       */
83      public boolean isImage() {
84          return getMimeType() != null && MediaType.parse(getMimeType()).is(MediaType.ANY_IMAGE_TYPE);
85      }
86  
87      /**
88       * @return the current image height. 0 If not yet set.
89       */
90      public long getHeight() {
91          return (imageSize != null) ? imageSize.getHeight() : 0;
92      }
93  
94      /**
95       * @return the current image width. 0 If not yet set.
96       */
97      public long getWidth() {
98          return (imageSize != null) ? imageSize.getWidth() : 0;
99      }
100 
101     /**
102      * @return the current media duration;
103      */
104     public long getDuration() {
105         return duration;
106     }
107 
108     /**
109      * Update the content. Keep file Name and extension. Used when cropping/...
110      */
111     public void updateContent(InputStream in) {
112         OutputStream out = receiveUpload(getFileName(), getMimeType());
113         try {
114             IOUtils.copy(in, out);
115         } catch (Exception e) {
116             log.warn("Not able to update the file content", e);
117         } finally {
118             IOUtils.closeQuietly(in);
119             IOUtils.closeQuietly(out);
120         }
121         updateRelatedProperties();
122     }
123 
124     public EditAssetAppConfiguration getEditAssetAppConfiguration() {
125         return this.editConfig;
126     }
127 
128     /**
129      * Update {@link ImageSize} and {@link EditAssetAppConfiguration} based on current {@link AssetUploadReceiver} values.
130      */
131     public void updateRelatedProperties() {
132         reloadEditAssetAppConfiguration();
133         if (!isEmpty() && isImage()) {
134             imageSize = ImageSize.valueOf(getContentAsStream());
135             duration = 0;
136         } else {
137             // FIXME EHE define a way to extract the duration of a video or audio.
138             duration = 0;
139             imageSize = null;
140         }
141     }
142 
143     /**
144      * Set EditAssetAppConfiguration based on the mimeType.
145      */
146     private void reloadEditAssetAppConfiguration() {
147         try {
148             if (this.getMimeType() != null) {
149                 editConfig = appConfig.getEditAssetAppConfigurationForMediaType(MediaType.parse(this.getMimeType()));
150                 return;
151             }
152             log.warn("Could not set EditAssetAppConfiguration based on the MimeType '{}'. Upload Field may not work property", this.getMimeType());
153 
154         } catch (IllegalArgumentException e) {
155             log.warn("Could not get a valid  EditAssetAppConfiguration based on the MimeType '{}'. Upload Field may not work property", this.getMimeType());
156         }
157         editConfig = null;
158     }
159 
160 }