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.ByteArrayOutputStream;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.Enumeration;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.zip.ZipEntry;
35 import java.util.zip.ZipInputStream;
36
37 import net.sourceforge.blogentis.om.Blog;
38 import net.sourceforge.blogentis.slide.SlideMemoryFile;
39 import net.sourceforge.blogentis.slide.SlideService;
40 import net.sourceforge.blogentis.turbine.BlogParameterParser;
41 import net.sourceforge.blogentis.turbine.SecureBlogAction;
42 import net.sourceforge.blogentis.utils.BlogConstants;
43 import net.sourceforge.blogentis.utils.BlogRequestWrapper;
44
45 import org.apache.commons.io.CopyUtils;
46 import org.apache.torque.TorqueException;
47 import org.apache.turbine.services.mimetype.TurbineMimeTypes;
48 import org.apache.turbine.util.RunData;
49 import org.apache.velocity.context.Context;
50
51 /***
52 * @author abas
53 */
54 public class BlogFileRestoreZip
55 extends SecureBlogAction {
56 private SlideMemoryFile getBlogFile(ZipInputStream zip, ZipEntry ze, Blog b)
57 throws TorqueException, IOException {
58 SlideMemoryFile smf = new SlideMemoryFile(b, ze.getName());
59 smf.setLastModified(new Date(ze.getTime()));
60 ByteArrayOutputStream bos = new ByteArrayOutputStream();
61 CopyUtils.copy(zip, bos);
62 smf.setMimeType(TurbineMimeTypes.getContentType(smf.getPath()));
63 smf.setContents(bos.toByteArray());
64 zip.closeEntry();
65 return smf;
66 }
67
68 public void doPerform(RunData data, Context context)
69 throws Exception {
70 BlogRequestWrapper brw = (BlogRequestWrapper)data.getRequest();
71
72 if (!brw.isMultipart()) {
73 data.setMessage("No file has been uploaded");
74 return;
75 }
76 Enumeration e = brw.getFileNames();
77 if (!e.hasMoreElements()) {
78 data.setMessage("No file has been uploaded");
79 return;
80 }
81 BlogParameterParser pp = (BlogParameterParser)data.getParameters();
82 String uploadName = (String)e.nextElement();
83 File f = brw.getFile(uploadName);
84
85 ZipInputStream zip = null;
86 List fileList = new ArrayList();
87 try {
88 zip = new ZipInputStream(new FileInputStream(f));
89 ZipEntry ze;
90 while ((ze = zip.getNextEntry()) != null) {
91 fileList.add(getBlogFile(zip, ze, pp.getBlog()));
92 }
93 } catch (IOException ioe) {
94 data.setMessage("Error: perhaps not a ZIP file? "
95 + ioe.getMessage());
96 return;
97 }
98
99 if (fileList.size() == 0) {
100 data.setMessage("No files found: perhaps not a ZIP file?");
101 return;
102 }
103
104 for(Iterator i = fileList.iterator(); i.hasNext();) {
105 SlideMemoryFile smf = (SlideMemoryFile)i.next();
106 if (SlideService.getInstance().saveFile(null, smf)) {
107 log.warn("Hmm... some problems with" + smf.getPath());
108 }
109 }
110 }
111
112 protected String[] getPermissions() {
113 return new String[]{BlogConstants.PERM_ADMIN_BLOG};
114 }
115 }