1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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";
76
77 public static final String EXTENSION_LOWER_CASE = "extensionLowerCase";
78
79 public static final String EXTENSION_UPPER_CASE = "extensionUpperCase";
80
81 public static final String NAME = "name";
82
83 public static final String NAME_WITHOUT_EXTENSION = "nameWithoutExtension";
84
85 public static final String CONTENT_TYPE = "jcr:mimeType";
86
87 public static final String ICON = "icon";
88
89 public static final String TEMPLATE = "template";
90
91 public static final String HANDLE = "handle";
92
93
94
95
96 public static final String PATH_WITHOUT_NAME = "pathWithoutName";
97
98
99
100
101 public static final String PATH = "path";
102
103
104
105
106 public static final String SIZE_BYTES = "sizeBytes";
107
108
109
110
111 public static final String SIZE_KB = "sizeKB";
112
113
114
115
116 public static final String SIZE_MB = "sizeMB";
117
118
119
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 {
239 value = this.getContent().getHandle() + "/" + this.getNodeDataName() + "/" + fullName;
240 }
241 return value;
242 }
243 }