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