1 package net.sourceforge.blogentis.modules.screens;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.Date;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.StringTokenizer;
32
33 import net.sourceforge.blogentis.om.Blog;
34 import net.sourceforge.blogentis.storage.AbstractFileResource;
35 import net.sourceforge.blogentis.storage.FileResourceFilter;
36 import net.sourceforge.blogentis.storage.FileRetrieverService;
37 import net.sourceforge.blogentis.storage.TemplateFolder;
38 import net.sourceforge.blogentis.turbine.BlogParameterParser;
39 import net.sourceforge.blogentis.turbine.SecureBlogScreen;
40 import net.sourceforge.blogentis.utils.AbsoluteLinkURL;
41 import net.sourceforge.blogentis.utils.BlogConstants;
42 import net.sourceforge.blogentis.utils.LinkFactoryService;
43
44 import org.apache.commons.collections.keyvalue.DefaultMapEntry;
45 import org.apache.commons.io.FileUtils;
46 import org.apache.turbine.util.RunData;
47 import org.apache.velocity.context.Context;
48
49 public class BlogMedia
50 extends SecureBlogScreen {
51 public static class FileFilter
52 implements FileResourceFilter {
53 public boolean ignored(String path) {
54 return path.endsWith("/CVS");
55 }
56
57 public boolean descendInto(String folder) {
58 return false;
59 }
60 }
61
62 public static class NameComparator
63 implements Comparator {
64 private int mult = 1;
65
66 public NameComparator(int mult) {
67 this.mult = mult;
68 }
69
70 public int compare(Object o1, Object o2) {
71 return mult
72 * ((AbstractFileResource)o1).getPath()
73 .compareTo(((AbstractFileResource)o2).getPath());
74 }
75 }
76
77 public static class MediaComparator
78 implements Comparator {
79 private int mult = 1;
80
81 public MediaComparator(int mult) {
82 this.mult = mult;
83 }
84
85 public int compare(Object o1, Object o2) {
86 return mult
87 * ((AbstractFileResource)o1)
88 .getMimeType()
89 .compareTo(((AbstractFileResource)o2).getMimeType());
90 }
91 }
92
93 public static class SizeComparator
94 implements Comparator {
95 private int mult = 1;
96
97 public SizeComparator(int mult) {
98 this.mult = mult;
99 }
100
101 public int compare(Object o1, Object o2) {
102 return mult
103 * (((AbstractFileResource)o2).getSize() - ((AbstractFileResource)o1)
104 .getSize());
105 }
106 }
107
108 public static class DateComparator
109 implements Comparator {
110 private int mult = 1;
111
112 public DateComparator(int mult) {
113 this.mult = mult;
114 }
115
116 public int compare(Object o1, Object o2) {
117 return mult
118 * ((AbstractFileResource)o1).getLastModified()
119 .compareTo(
120 ((AbstractFileResource)o2)
121 .getLastModified());
122 }
123 }
124
125 public static class MediaFileAdaptor {
126 AbstractFileResource afr;
127
128 MediaFileAdaptor(AbstractFileResource file) {
129 this.afr = file;
130 }
131
132 public String getName() {
133 return afr.getPath().substring(afr.getPath().lastIndexOf('/') + 1);
134 }
135
136 public String getSize() {
137 return FileUtils.byteCountToDisplaySize(afr.getSize());
138 }
139
140 public Date getLastModified() {
141 return afr.getLastModified();
142 }
143
144 public String getMimeType() {
145 return afr.getMimeType();
146 }
147
148 public String getPath() {
149 return afr.getPath();
150 }
151 }
152
153 public static class MediaFolderAdaptor {
154 TemplateFolder tf;
155
156 MediaFolderAdaptor(TemplateFolder file) {
157 this.tf = file;
158 }
159
160 public String getName() {
161 return tf.getPath().substring(tf.getPath().lastIndexOf('/') + 1);
162 }
163
164 public String getSize() {
165 return "(" + (tf.getDirectories().size() + tf.getFiles().size())
166 + ")";
167 }
168
169 public Date getLastModified() {
170 return null;
171 }
172
173 public String getMimeType() {
174 return "(folder)";
175 }
176
177 public String getPath() {
178 return tf.getPath().substring("/media".length());
179 }
180
181 public String getRealPath() {
182 return tf.getPath();
183 }
184 }
185
186 protected List getParentLinks(RunData d, Blog b, String path) {
187 StringTokenizer st = new StringTokenizer(path, "/");
188 ArrayList l = new ArrayList(st.countTokens() + 1);
189 l.add(new DefaultMapEntry("Media files", LinkFactoryService.getLink()
190 .setBlog(b).setPage("BlogMedia").addQueryData("path", "/")));
191 String curPath = "";
192 for(; st.hasMoreElements();) {
193 String s = st.nextToken();
194 curPath = curPath + "/" + s;
195 AbsoluteLinkURL link = LinkFactoryService.getLink();
196 link.setBlog(b);
197 link.setPage("BlogMedia");
198 link.addQueryData("path", curPath);
199 l.add(new DefaultMapEntry(s, link));
200 }
201 return l;
202 }
203
204 protected void doBuildTemplate(RunData data, Context context)
205 throws Exception {
206 super.doBuildTemplate(data, context);
207 BlogParameterParser pp = (BlogParameterParser)data.getParameters();
208 Blog blog = pp.getBlog();
209
210 context.put("path", pp.getString("path", "/"));
211
212 String path = "media/" + pp.getString("path", "");
213 path = path.replaceAll("(/*[.][.]/*)|//+", "/");
214 path = path.replaceAll("(^/|/$)", "");
215
216 TemplateFolder tf = FileRetrieverService.getInstance()
217 .getTemplateList(data, blog, path, new FileFilter());
218 List l = tf.getFiles();
219
220 int i = pp.getInt("sort", -1);
221 Comparator c = null;
222 switch (i) {
223 case 1:
224 c = new NameComparator(1);
225 break;
226 case 2:
227 c = new SizeComparator(-1);
228 break;
229 case 4:
230 c = new MediaComparator(1);
231 break;
232 default:
233 c = new DateComparator(-1);
234 break;
235 }
236 Collections.sort(l, c);
237
238 List files = new ArrayList(l.size());
239 for(Iterator it = l.iterator(); it.hasNext();) {
240 files.add(new MediaFileAdaptor((AbstractFileResource)it.next()));
241 }
242 context.put("mediaFiles", files);
243
244 List folders = new ArrayList(tf.getDirectories().size());
245 for(Iterator it = tf.getDirectories().iterator(); it.hasNext();) {
246 folders.add(new MediaFolderAdaptor((TemplateFolder)it.next()));
247 }
248 context.put("mediaFolders", folders);
249
250 context.put("curFolder", tf);
251 context.put("pathStack", getParentLinks(data, blog, path
252 .substring("media".length())));
253 context.put("state", pp.getIntObject("s", new Integer(0)));
254 }
255
256 protected String[] getPermissions() {
257 return new String[] {BlogConstants.PERM_ADMIN_BLOG};
258 }
259 }