1 package net.sourceforge.blogentis.modules.actions;
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.io.File;
26 import java.io.FileInputStream;
27 import java.util.Date;
28 import java.util.Enumeration;
29
30 import net.sourceforge.blogentis.om.Blog;
31 import net.sourceforge.blogentis.slide.SlideMemoryFile;
32 import net.sourceforge.blogentis.slide.SlideService;
33 import net.sourceforge.blogentis.turbine.BlogParameterParser;
34 import net.sourceforge.blogentis.turbine.SecureBlogAction;
35 import net.sourceforge.blogentis.utils.BlogConstants;
36 import net.sourceforge.blogentis.utils.BlogRequestWrapper;
37
38 import org.apache.turbine.services.mimetype.TurbineMimeTypes;
39 import org.apache.turbine.util.RunData;
40 import org.apache.velocity.context.Context;
41
42 public class BlogFileMod
43 extends SecureBlogAction {
44 public void doEdit(RunData data, Context context)
45 throws Exception {
46 String fileId = data.getParameters().getString("fileId", "");
47 BlogParameterParser bparam = (BlogParameterParser)(data.getParameters());
48 SlideMemoryFile smf = new SlideMemoryFile(bparam.getBlog(), fileId);
49 smf.setContents(data.getParameters().getString("contents")
50 .getBytes("utf-8"));
51 smf.setLastModified(new Date());
52 smf.setMimeType(TurbineMimeTypes.getMimeContentType(fileId).toString());
53 SlideService.getInstance().saveFile(bparam.getBlog(), smf);
54 data.getParameters().remove("fileId");
55 }
56
57 public void doMkcol(RunData data, Context context)
58 throws Exception {
59 BlogParameterParser bparam = (BlogParameterParser)(data.getParameters());
60 String path = bparam.getString("path", null);
61 if (path == null)
62 return;
63 String folderName = bparam.getString("folderName", null);
64 if (folderName == null || folderName.length() < 1
65 || folderName.indexOf('/') > 0)
66 return;
67 if (bparam.getString("media", null) != null)
68 path = "/media/" + path;
69 SlideService.getInstance().makeCollection(bparam.getBlog(),
70 path + "/" + folderName);
71 }
72
73 public void doAddnew(RunData data, Context context)
74 throws Exception {
75 BlogParameterParser pp = (BlogParameterParser)(data.getParameters());
76 Blog blog = pp.getBlog();
77 String path = pp.getString("dir");
78 String file = pp.getString("file");
79 if (path == null || path.length() < 2)
80 return;
81 if (file == null || file.length() < 2)
82 return;
83
84 path = path.trim().replaceAll("[./ ]+", "");
85 file = file.trim().replaceAll("^.*[/]", "");
86 file = path + "/" + file.replaceAll("[ +<>&/]", "_");
87
88 SlideMemoryFile smf = new SlideMemoryFile(blog, file);
89 smf.setLastModified(new Date());
90 smf.setContents(new byte[] {});
91 SlideService.getInstance().saveFile(blog, smf);
92 }
93
94 public void doAdd(RunData data, Context context)
95 throws Exception {
96 BlogRequestWrapper brw = (BlogRequestWrapper)data.getRequest();
97 if (!brw.isMultipart()) {
98 data.setMessage("No file has been uploaded");
99 return;
100 }
101 Enumeration e = brw.getFileNames();
102 if (!e.hasMoreElements()) {
103 data.setMessage("No file has been uploaded");
104 return;
105 }
106 BlogParameterParser pp = (BlogParameterParser)(data.getParameters());
107 String paramName = (String)e.nextElement();
108 File f = brw.getFile(paramName);
109 if (f == null) {
110 data.setMessage("No file has been uploaded");
111 return;
112 }
113 String path = pp.getString("path", null);
114 if (path == null)
115 return;
116
117 byte[] contents = new byte[(int)f.length()];
118
119 FileInputStream fio = new FileInputStream(f);
120 fio.read(contents, 0, contents.length);
121 fio.close();
122
123 Blog blog = pp.getBlog();
124 String file = brw.getFilesystemName(paramName);
125 file = file.trim().replaceAll("^.*[/]", "");
126 file = (pp.containsKey("media") ? ("media/") : "") + path + "/"
127 + file.replaceAll("[ +<>&/]", "_");
128
129 SlideMemoryFile smf = new SlideMemoryFile(blog, file);
130 smf.setLastModified(new Date());
131 smf.setMimeType(TurbineMimeTypes.getContentType(smf.getPath()));
132 smf.setContents(contents);
133 SlideService.getInstance().saveFile(blog, smf);
134 }
135
136 public void doRemove(RunData data, Context context)
137 throws Exception {
138 String fileId = data.getParameters().getString("fileId", "");
139 BlogParameterParser bparam = (BlogParameterParser)(data.getParameters());
140 SlideService.getInstance().deleteObject(data, bparam.getBlog(), fileId);
141 data.getParameters().remove("fileId");
142 }
143
144 public void doPerform(RunData data, Context arg1)
145 throws Exception {
146 data.setMessage("Hmm... Unknown action requested!");
147 }
148
149 protected String[] getPermissions() {
150 return new String[] {BlogConstants.PERM_ADMIN_BLOG};
151 }
152 }