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.lang3.StringUtils;
42 import org.apache.commons.lang3.math.NumberUtils;
43
44
45
46
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";
75
76 public static final String EXTENSION_LOWER_CASE = "extensionLowerCase";
77
78 public static final String EXTENSION_UPPER_CASE = "extensionUpperCase";
79
80 public static final String NAME = "name";
81
82 public static final String NAME_WITHOUT_EXTENSION = "nameWithoutExtension";
83
84 public static final String CONTENT_TYPE = "jcr:mimeType";
85
86 public static final String ICON = "icon";
87
88 public static final String TEMPLATE = "template";
89
90 public static final String HANDLE = "handle";
91
92
93
94
95 public static final String PATH_WITHOUT_NAME = "pathWithoutName";
96
97
98
99
100 public static final String PATH = "path";
101
102
103
104
105 public static final String SIZE_BYTES = "sizeBytes";
106
107
108
109
110 public static final String SIZE_KB = "sizeKB";
111
112
113
114
115 public static final String SIZE_MB = "sizeMB";
116
117
118
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 {
255 value = this.getContent().getHandle() + "/" + this.getNodeDataName() + "/" + fullName;
256 }
257 return value;
258 }
259 }