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