View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.cms.beans.runtime;
35  
36  import info.magnolia.cms.beans.config.MIMEMapping;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.NodeData;
39  
40  import javax.jcr.PropertyType;
41  
42  import org.apache.commons.lang3.StringUtils;
43  import org.apache.commons.lang3.math.NumberUtils;
44  
45  
46  /**
47   * Similar to info.magnolia.cms.beans.runtime.File but exposes the binary's properties using constants.
48   */
49  public class FileProperties {
50  
51      public static final String PROPERTIES_CONTENTNODE = "properties";
52  
53      public static final String PROPERTY_CONTENTTYPE = "jcr:mimeType";
54  
55      public static final String PROPERTY_ENCODING = "jcr:encoding";
56  
57      public static final String PROPERTY_LASTMODIFIED = "jcr:lastModified";
58  
59      public static final String PROPERTY_SIZE = "size";
60  
61      public static final String PROPERTY_TEMPLATE = "nodeDataTemplate";
62  
63      public static final String PROPERTY_EXTENSION = "extension";
64  
65      public static final String PROPERTY_MIMETYPE = "mimeType";
66  
67      public static final String PROPERTY_FILENAME = "fileName";
68  
69      public static final String PROPERTY_ICON = "icon";
70  
71      public static final String PROPERTY_WIDTH = "width";
72  
73      public static final String PROPERTY_HEIGHT = "height";
74  
75      public static final String EXTENSION = "extension"; // Pdf
76  
77      public static final String EXTENSION_LOWER_CASE = "extensionLowerCase"; // pdf
78  
79      public static final String EXTENSION_UPPER_CASE = "extensionUpperCase"; // PDF
80  
81      public static final String NAME = "name"; // report2004.Pdf
82  
83      public static final String NAME_WITHOUT_EXTENSION = "nameWithoutExtension"; // report2004
84  
85      public static final String CONTENT_TYPE = "jcr:mimeType"; // application/pdf
86  
87      public static final String ICON = "icon"; // the icon for this type
88  
89      public static final String TEMPLATE = "template"; // ((according to dialog))
90  
91      public static final String HANDLE = "handle"; // /en/mainColumnParagraph/04/file
92  
93      /**
94       * /en/mainColumnParagraph/04/file.Pdf.
95       */
96      public static final String PATH_WITHOUT_NAME = "pathWithoutName";
97  
98      /**
99       * path including fileName: <code>/en/mainColumnParagraph/04/file/report2004.Pdf</code>.
100      */
101     public static final String PATH = "path";
102 
103     /**
104      * size in bytes: <code>263492</code>.
105      */
106     public static final String SIZE_BYTES = "sizeBytes";
107 
108     /**
109      * size in KB: <code>257.3</code>.
110      */
111     public static final String SIZE_KB = "sizeKB";
112 
113     /**
114      * size in MB: <code>0.2</code>.
115      */
116     public static final String SIZE_MB = "sizeMB";
117 
118     /**
119      * size and unit depending of size in bytes, KB, or MB: <code>257.3</code>.
120      */
121     public static final String SIZE = "size";
122 
123     private Content content;
124 
125     private String nodeDataName;
126 
127     public FileProperties(Content content, String nodeDataName) {
128         this.setContent(content);
129         this.setNodeDataName(nodeDataName);
130     }
131 
132     public void setContent(Content c) {
133         this.content = c;
134     }
135 
136     public Content getContent() {
137         return this.content;
138     }
139 
140     public void setNodeDataName(String s) {
141         this.nodeDataName = s;
142     }
143 
144     public String getNodeDataName() {
145         return this.nodeDataName;
146     }
147 
148     public String getProperty(String property) {
149         String value = StringUtils.EMPTY;
150 
151         if (this.getContent() == null) {
152             return null;
153         }
154 
155         NodeData props = this.getContent().getNodeData(this.nodeDataName);
156         if (props.getType() != PropertyType.BINARY) {
157             return null;
158         }
159 
160         String filename = props.getAttribute(PROPERTY_FILENAME);
161         String ext = props.getAttribute(PROPERTY_EXTENSION);
162         String fullName = filename;
163         String fullExt = StringUtils.EMPTY;
164         if (StringUtils.isNotEmpty(ext)) {
165             fullExt = "." + ext;
166             fullName += fullExt;
167         }
168         if (property.equals(EXTENSION)) {
169             value = ext;
170         } else if (property.equals(EXTENSION_LOWER_CASE)) {
171             value = StringUtils.lowerCase(ext);
172         } else if (property.equals(EXTENSION_UPPER_CASE)) {
173             value = StringUtils.upperCase(ext);
174         } else if (property.equals(NAME_WITHOUT_EXTENSION)) {
175             value = filename;
176         } else if (property.equals(CONTENT_TYPE)) {
177             value = props.getAttribute(PROPERTY_CONTENTTYPE);
178         } else if (property.equals(TEMPLATE)) {
179             value = props.getAttribute(PROPERTY_TEMPLATE);
180         } else if (property.equals(HANDLE)) {
181             value = this.getContent().getHandle() + "/" + this.getNodeDataName();
182         } else if (property.equals(NAME)) {
183             value = fullName;
184         } else if (property.equals(PATH_WITHOUT_NAME)) {
185             value = this.getContent().getHandle() + "/" + this.getNodeDataName() + fullExt;
186         } else if (property.equals(ICON) && ext != null) {
187             value = MIMEMapping.getMIMETypeIcon(ext);
188         } else if (property.equals(SIZE_BYTES)) {
189             value = props.getAttribute(PROPERTY_SIZE);
190         } else if (property.equals(SIZE_KB)) {
191             String sizestring = props.getAttribute(PROPERTY_SIZE);
192             if (!NumberUtils.isNumber(sizestring)) {
193                 return null;
194             }
195             double size = Long.parseLong(sizestring);
196             String sizeStr;
197             size = size / 1024;
198             sizeStr = Double.toString(size);
199             sizeStr = sizeStr.substring(0, sizeStr.indexOf(".") + 2);
200             value = sizeStr;
201         } else if (property.equals(SIZE_MB)) {
202             String sizestring = props.getAttribute(PROPERTY_SIZE);
203             if (!NumberUtils.isNumber(sizestring)) {
204                 return null;
205             }
206             double size = Long.parseLong(sizestring);
207             String sizeStr;
208             size = size / (1024 * 1024);
209             sizeStr = Double.toString(size);
210             sizeStr = sizeStr.substring(0, sizeStr.indexOf(".") + 2);
211             value = sizeStr;
212         } else if (property.equals(SIZE)) {
213             String sizestring = props.getAttribute(PROPERTY_SIZE);
214             if (!NumberUtils.isNumber(sizestring)) {
215                 return null;
216             }
217             double size = Long.parseLong(sizestring);
218             String unit = "bytes";
219             String sizeStr;
220             if (size >= 1000) {
221                 size = size / 1024;
222                 unit = "KB";
223                 if (size >= 1000) {
224                     size = size / 1024;
225                     unit = "MB";
226                 }
227                 sizeStr = Double.toString(size);
228                 sizeStr = sizeStr.substring(0, sizeStr.indexOf(".") + 2);
229             } else {
230                 sizeStr = Double.toString(size);
231                 sizeStr = sizeStr.substring(0, sizeStr.indexOf("."));
232             }
233             value = sizeStr + " " + unit;
234         } else if (property.equals(PROPERTY_WIDTH)) {
235             value = props.getAttribute(PROPERTY_WIDTH);
236         } else if (property.equals(PROPERTY_HEIGHT)) {
237             value = props.getAttribute(PROPERTY_HEIGHT);
238         } else { // property.equals(PATH|null|""|any other value)
239             value = this.getContent().getHandle() + "/" + this.getNodeDataName() + "/" + fullName;
240         }
241         return value;
242     }
243 }