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