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
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";
79
80 public static final String EXTENSION_LOWER_CASE = "extensionLowerCase";
81
82 public static final String EXTENSION_UPPER_CASE = "extensionUpperCase";
83
84 public static final String NAME = "name";
85
86 public static final String NAME_WITHOUT_EXTENSION = "nameWithoutExtension";
87
88 public static final String CONTENT_TYPE = "jcr:mimeType";
89
90 public static final String ICON = "icon";
91
92 public static final String TEMPLATE = "template";
93
94 public static final String HANDLE = "handle";
95
96
97
98
99 public static final String PATH_WITHOUT_NAME = "pathWithoutName";
100
101
102
103
104 public static final String PATH = "path";
105
106
107
108
109 public static final String SIZE_BYTES = "sizeBytes";
110
111
112
113
114 public static final String SIZE_KB = "sizeKB";
115
116
117
118
119 public static final String SIZE_MB = "sizeMB";
120
121
122
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 {
242 value = this.getContent().getHandle() + "/" + this.getNodeDataName() + "/" + fullName;
243 }
244 return value;
245 }
246 }