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