1 | package org.greenstone.gsdl3;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.FileOutputStream;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.PrintWriter;
|
---|
8 | import java.nio.channels.FileChannel;
|
---|
9 | import java.util.Iterator;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import javax.servlet.ServletException;
|
---|
14 | import javax.servlet.ServletOutputStream;
|
---|
15 | import javax.servlet.http.HttpServletRequest;
|
---|
16 | import javax.servlet.http.HttpServletResponse;
|
---|
17 |
|
---|
18 | import org.apache.commons.fileupload.FileItem;
|
---|
19 | import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
---|
20 | import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
---|
21 | import org.greenstone.gsdl3.util.GSParams;
|
---|
22 | import org.greenstone.util.GlobalProperties;
|
---|
23 |
|
---|
24 | public class FileLoaderServlet extends LibraryServlet
|
---|
25 | {
|
---|
26 | public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
---|
27 | {
|
---|
28 | if (request.getMethod().equals("POST"))
|
---|
29 | {
|
---|
30 | DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
|
---|
31 |
|
---|
32 | int sizeLimit = System.getProperties().containsKey("servlet.upload.filesize.limit") ? Integer.parseInt(System.getProperty("servlet.upload.filesize.limit")) : 20 * 1024 * 1024;
|
---|
33 |
|
---|
34 | fileItemFactory.setSizeThreshold(sizeLimit);
|
---|
35 | fileItemFactory.setRepository(new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp"));
|
---|
36 |
|
---|
37 | ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
|
---|
38 |
|
---|
39 | String storageLocation = "";
|
---|
40 | File uploadedFile = null;
|
---|
41 | Boolean ajaxUpload = false;
|
---|
42 | StringBuilder json = new StringBuilder("[");
|
---|
43 | try
|
---|
44 | {
|
---|
45 | List items = uploadHandler.parseRequest(request);
|
---|
46 | Iterator iter = items.iterator();
|
---|
47 | while (iter.hasNext())
|
---|
48 | {
|
---|
49 | FileItem current = (FileItem) iter.next();
|
---|
50 | if (current.isFormField())
|
---|
51 | {
|
---|
52 | if (current.getFieldName().equals(GSParams.FILE_LOCATION))
|
---|
53 | {
|
---|
54 | storageLocation = current.getString();
|
---|
55 | }
|
---|
56 |
|
---|
57 | if (current.getFieldName().equals("ajaxUpload") && current.getString().equals("true"))
|
---|
58 | {
|
---|
59 | ajaxUpload = true;
|
---|
60 | }
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | File file = new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp" + File.separator + current.getName());
|
---|
65 | File tmpFolder = new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp");
|
---|
66 | if (!tmpFolder.exists())
|
---|
67 | {
|
---|
68 | tmpFolder.mkdirs();
|
---|
69 | }
|
---|
70 | current.write(file);
|
---|
71 |
|
---|
72 | uploadedFile = file;
|
---|
73 |
|
---|
74 | if (!json.toString().equals("["))
|
---|
75 | {
|
---|
76 | json.append(",");
|
---|
77 | }
|
---|
78 |
|
---|
79 | json.append("{");
|
---|
80 | json.append("\"name\":\"" + file.getName() + "\",");
|
---|
81 | json.append("\"size\":\"" + file.length() + "\",");
|
---|
82 | json.append("\"url\":\"" + "tmp/" + current.getName() + "\",");
|
---|
83 | json.append("\"thumbnail_url\":\"" + "tmp/" + current.getName() + "\",");
|
---|
84 | json.append("\"delete_url\":\"\",");
|
---|
85 | json.append("\"delete_type\":\"\"");
|
---|
86 | json.append("}");
|
---|
87 | }
|
---|
88 | }
|
---|
89 | json.append("]");
|
---|
90 |
|
---|
91 | if (ajaxUpload)
|
---|
92 | {
|
---|
93 | response.setContentType("application/json");
|
---|
94 | PrintWriter writer = response.getWriter();
|
---|
95 | writer.write(json.toString());
|
---|
96 | writer.flush();
|
---|
97 |
|
---|
98 | return;
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (!storageLocation.equals("") && uploadedFile != null)
|
---|
102 | {
|
---|
103 | String[] locations = storageLocation.split(":");
|
---|
104 |
|
---|
105 | for (String location : locations)
|
---|
106 | {
|
---|
107 | File toFile = new File(GlobalProperties.getGSDL3Home() + location);
|
---|
108 | if (toFile.exists())
|
---|
109 | {
|
---|
110 | File backupFile = new File(toFile.getAbsolutePath() + System.currentTimeMillis());
|
---|
111 |
|
---|
112 | logger.info("Backing up file (" + toFile.getAbsolutePath() + ") to " + backupFile.getAbsolutePath());
|
---|
113 | toFile.renameTo(backupFile);
|
---|
114 | }
|
---|
115 |
|
---|
116 | FileChannel source = null;
|
---|
117 | FileChannel destination = null;
|
---|
118 | try
|
---|
119 | {
|
---|
120 | logger.info("Moving uploaded file (" + uploadedFile.getAbsolutePath() + ") to " + toFile.getAbsolutePath());
|
---|
121 | source = new FileInputStream(uploadedFile).getChannel();
|
---|
122 | destination = new FileOutputStream(toFile).getChannel();
|
---|
123 | destination.transferFrom(source, 0, source.size());
|
---|
124 | }
|
---|
125 | finally
|
---|
126 | {
|
---|
127 | if (source != null)
|
---|
128 | {
|
---|
129 | source.close();
|
---|
130 | }
|
---|
131 | if (destination != null)
|
---|
132 | {
|
---|
133 | destination.close();
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | catch (Exception e)
|
---|
141 | {
|
---|
142 | logger.error("Exception in LibraryServlet -> " + e.getMessage());
|
---|
143 | }
|
---|
144 | }
|
---|
145 | else
|
---|
146 | {
|
---|
147 | Map<String, String[]> queryMap = request.getParameterMap();
|
---|
148 | Iterator<String> queryIter = queryMap.keySet().iterator();
|
---|
149 |
|
---|
150 | while (queryIter.hasNext())
|
---|
151 | {
|
---|
152 | String q = queryIter.next();
|
---|
153 | if (q.equals("downloadFile"))
|
---|
154 | {
|
---|
155 | String fileLocation = queryMap.get(q)[0];
|
---|
156 | File fileToGet = new File(GlobalProperties.getGSDL3Home() + File.separator + fileLocation);
|
---|
157 |
|
---|
158 | if (fileToGet.exists())
|
---|
159 | {
|
---|
160 | response.setContentType("application/octet-stream");
|
---|
161 | response.addHeader("Content-Disposition", "attachment;filename=" + fileToGet.getName());
|
---|
162 | FileInputStream fis = new FileInputStream(fileToGet);
|
---|
163 | ServletOutputStream sos = response.getOutputStream();
|
---|
164 |
|
---|
165 | byte[] buffer = new byte[4096];
|
---|
166 | int len;
|
---|
167 | while ((len = fis.read(buffer)) != -1)
|
---|
168 | {
|
---|
169 | sos.write(buffer, 0, len);
|
---|
170 | }
|
---|
171 | sos.flush();
|
---|
172 | fis.close();
|
---|
173 | sos.close();
|
---|
174 |
|
---|
175 | return;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | super.doGet(request, response);
|
---|
182 | }
|
---|
183 |
|
---|
184 | public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
---|
185 | {
|
---|
186 | doGet(request, response);
|
---|
187 | }
|
---|
188 | }
|
---|