root / src / gr / ebs / gss / client / FileUploadGearsDialog.java @ 5210ea4c
History | View | Annotate | Download (13.4 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2009 Electronic Business Systems Ltd.
|
3 |
*
|
4 |
* This file is part of GSS.
|
5 |
*
|
6 |
* GSS is free software: you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation, either version 3 of the License, or
|
9 |
* (at your option) any later version.
|
10 |
*
|
11 |
* GSS is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with GSS. If not, see <http://www.gnu.org/licenses/>.
|
18 |
*/
|
19 |
package gr.ebs.gss.client; |
20 |
|
21 |
import gr.ebs.gss.client.rest.PostCommand; |
22 |
import gr.ebs.gss.client.rest.RestCommand; |
23 |
import gr.ebs.gss.client.rest.RestException; |
24 |
import gr.ebs.gss.client.rest.resource.FileResource; |
25 |
import gr.ebs.gss.client.rest.resource.FolderResource; |
26 |
|
27 |
import java.util.ArrayList; |
28 |
import java.util.Arrays; |
29 |
import java.util.HashMap; |
30 |
import java.util.List; |
31 |
import java.util.Map; |
32 |
|
33 |
import com.google.gwt.core.client.GWT; |
34 |
import com.google.gwt.event.dom.client.ClickEvent; |
35 |
import com.google.gwt.event.dom.client.ClickHandler; |
36 |
import com.google.gwt.gears.client.Factory; |
37 |
import com.google.gwt.gears.client.desktop.Desktop; |
38 |
import com.google.gwt.gears.client.desktop.File; |
39 |
import com.google.gwt.gears.client.desktop.OpenFilesHandler; |
40 |
import com.google.gwt.gears.client.httprequest.HttpRequest; |
41 |
import com.google.gwt.gears.client.httprequest.ProgressEvent; |
42 |
import com.google.gwt.gears.client.httprequest.ProgressHandler; |
43 |
import com.google.gwt.gears.client.httprequest.RequestCallback; |
44 |
import com.google.gwt.http.client.URL; |
45 |
import com.google.gwt.json.client.JSONObject; |
46 |
import com.google.gwt.json.client.JSONString; |
47 |
import com.google.gwt.user.client.DeferredCommand; |
48 |
import com.google.gwt.user.client.ui.Button; |
49 |
import com.google.gwt.user.client.ui.FlexTable; |
50 |
import com.google.gwt.user.client.ui.HTML; |
51 |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
52 |
import com.google.gwt.user.client.ui.HorizontalPanel; |
53 |
import com.google.gwt.user.client.ui.VerticalPanel; |
54 |
|
55 |
/**
|
56 |
* The 'File upload' dialog box implementation with Google Gears support.
|
57 |
*/
|
58 |
public class FileUploadGearsDialog extends FileUploadDialog implements Updateable { |
59 |
|
60 |
protected final Factory factory = Factory.getInstance(); |
61 |
|
62 |
/**
|
63 |
* The array of files to upload.
|
64 |
*/
|
65 |
private File[] fileObjects; |
66 |
|
67 |
/**
|
68 |
* A list of files to upload, created from files array. Used to signal
|
69 |
* finished state when empty.
|
70 |
*/
|
71 |
protected List<File> selectedFiles = new ArrayList<File>(); |
72 |
|
73 |
/**
|
74 |
* The list of progress bars for individual files.
|
75 |
*/
|
76 |
protected List<ProgressBar> progressBars = new ArrayList<ProgressBar>(); |
77 |
|
78 |
private Button browse; |
79 |
|
80 |
private Button submit; |
81 |
|
82 |
private FlexTable generalTable;
|
83 |
|
84 |
private Map<String, FileResource> toRename; |
85 |
|
86 |
protected List<HttpRequest> requests = new ArrayList<HttpRequest>(); |
87 |
|
88 |
private boolean canContinue = true; |
89 |
|
90 |
/**
|
91 |
* The widget's constructor.
|
92 |
*/
|
93 |
public FileUploadGearsDialog() {
|
94 |
// Set the dialog's caption.
|
95 |
setText("File upload");
|
96 |
setAnimationEnabled(true);
|
97 |
// Create a panel to hold all of the dialog widgets.
|
98 |
VerticalPanel panel = new VerticalPanel();
|
99 |
final HTML info = new HTML("You may select one or more files to upload."); |
100 |
info.addStyleName("gss-uploadNote");
|
101 |
panel.add(info); |
102 |
// Add an informative label with the folder name.
|
103 |
Object selection = GSS.get().getFolders().getCurrent().getUserObject();
|
104 |
folder = (FolderResource) selection; |
105 |
|
106 |
browse = new Button("Browse..."); |
107 |
|
108 |
HorizontalPanel fileUploadPanel = new HorizontalPanel();
|
109 |
fileUploadPanel.add(browse); |
110 |
|
111 |
generalTable = new FlexTable();
|
112 |
generalTable.setText(0, 0, "Folder"); |
113 |
generalTable.setText(1, 0, "File"); |
114 |
generalTable.setText(0, 1, folder.getName()); |
115 |
generalTable.setWidget(1, 1, fileUploadPanel); |
116 |
generalTable.getCellFormatter().setStyleName(0, 0, "props-labels"); |
117 |
generalTable.getCellFormatter().setStyleName(1, 0, "props-labels"); |
118 |
generalTable.getCellFormatter().setStyleName(0, 1, "props-values"); |
119 |
generalTable.getCellFormatter().setStyleName(1, 1, "props-values"); |
120 |
generalTable.setCellSpacing(4);
|
121 |
|
122 |
panel.add(generalTable); |
123 |
|
124 |
// Create a panel to hold the buttons.
|
125 |
HorizontalPanel buttons = new HorizontalPanel();
|
126 |
|
127 |
submit = new Button("Upload"); |
128 |
submit.addClickHandler(new ClickHandler() {
|
129 |
@Override
|
130 |
public void onClick(ClickEvent event) { |
131 |
prepareAndSubmit(); |
132 |
} |
133 |
}); |
134 |
submit.setEnabled(false);
|
135 |
buttons.add(submit); |
136 |
buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER); |
137 |
// Create the 'Cancel' button, along with a listener that hides the
|
138 |
// dialog when the button is clicked.
|
139 |
Button cancel = new Button("Cancel", new ClickHandler() { |
140 |
@Override
|
141 |
public void onClick(ClickEvent event) { |
142 |
canContinue = false;
|
143 |
cancelUpload(); |
144 |
} |
145 |
}); |
146 |
buttons.add(cancel); |
147 |
buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER); |
148 |
buttons.setSpacing(8);
|
149 |
buttons.addStyleName("gss-DialogBox");
|
150 |
|
151 |
browse.addClickHandler(new ClickHandler() {
|
152 |
@Override
|
153 |
public void onClick(ClickEvent event) { |
154 |
Desktop desktop = factory.createDesktop(); |
155 |
desktop.openFiles(new OpenFilesHandler() {
|
156 |
|
157 |
@Override
|
158 |
public void onOpenFiles(OpenFilesEvent ofevent) { |
159 |
fileObjects = ofevent.getFiles(); |
160 |
selectedFiles.addAll(Arrays.asList(fileObjects));
|
161 |
for (int i = 0; i< selectedFiles.size(); i++) { |
162 |
generalTable.setText(i+1, 0, "File"); |
163 |
generalTable.setText(i+1, 1, selectedFiles.get(i).getName()); |
164 |
ProgressBar progress = new ProgressBar(20, 0); |
165 |
generalTable.setWidget(i+1, 2, progress); |
166 |
progressBars.add(progress); |
167 |
generalTable.getCellFormatter().setStyleName(i+1, 0, "props-labels"); |
168 |
generalTable.getCellFormatter().setStyleName(i+1, 1, "props-values"); |
169 |
} |
170 |
submit.setEnabled(true);
|
171 |
} |
172 |
}); |
173 |
} |
174 |
}); |
175 |
|
176 |
panel.add(buttons); |
177 |
panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER); |
178 |
panel.addStyleName("gss-DialogBox");
|
179 |
addStyleName("gss-DialogBox");
|
180 |
setWidget(panel); |
181 |
} |
182 |
|
183 |
/**
|
184 |
* Cancels the file upload.
|
185 |
*/
|
186 |
private void cancelUpload() { |
187 |
for (HttpRequest request: requests)
|
188 |
request.abort(); |
189 |
hide(); |
190 |
} |
191 |
|
192 |
/**
|
193 |
* Check whether the specified file name exists in the selected folder.
|
194 |
*/
|
195 |
private boolean canContinue(File file) { |
196 |
String fileName = getFilename(file.getName());
|
197 |
if (getFileForName(fileName) == null) |
198 |
// For file creation, check to see if the file already exists.
|
199 |
for (FileResource fileRes : files)
|
200 |
if (!fileRes.isDeleted() && fileRes.getName().equals(fileName))
|
201 |
return false; |
202 |
return true; |
203 |
} |
204 |
|
205 |
@Override
|
206 |
public void prepareAndSubmit() { |
207 |
GSS app = GSS.get(); |
208 |
if (selectedFiles.size() == 0) { |
209 |
app.displayError("You must select a file!");
|
210 |
hide(); |
211 |
return;
|
212 |
} |
213 |
for (File file: selectedFiles) |
214 |
if (!canContinue(file)) {
|
215 |
app.displayError("The file name " + file.getName() +
|
216 |
" already exists in this folder");
|
217 |
hide(); |
218 |
return;
|
219 |
} |
220 |
submit.setEnabled(false);
|
221 |
browse.setVisible(false);
|
222 |
List<String> toUpdate = new ArrayList<String>(); |
223 |
toRename = new HashMap<String, FileResource>(); |
224 |
for (File file: selectedFiles) { |
225 |
String fname = getFilename(file.getName());
|
226 |
if (getFileForName(fname) == null) { |
227 |
// We are going to create a file, so we check to see if there is a
|
228 |
// trashed file with the same name.
|
229 |
FileResource same = null;
|
230 |
for (FileResource fres : folder.getFiles())
|
231 |
if (fres.isDeleted() && fres.getName().equals(fname))
|
232 |
same = fres; |
233 |
// In that case add it to the list of files to rename.
|
234 |
if (same != null) |
235 |
toRename.put(getBackupFilename(fname), same); |
236 |
} else
|
237 |
// If we are updating a file add it to the list of files to update.
|
238 |
toUpdate.add(fname); |
239 |
} |
240 |
|
241 |
if (!toUpdate.isEmpty()) {
|
242 |
StringBuffer sb = new StringBuffer(); |
243 |
for (String name: toUpdate) |
244 |
sb.append(name).append("<br/>");
|
245 |
// We are going to update existing files, so show a confirmation dialog.
|
246 |
ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " + |
247 |
"you want to update the following files?<br/><i>" + sb +
|
248 |
"</i>", "Update") { |
249 |
|
250 |
@Override
|
251 |
public void cancel() { |
252 |
hide(); |
253 |
} |
254 |
|
255 |
@Override
|
256 |
public void confirm() { |
257 |
confirmRename(); |
258 |
} |
259 |
|
260 |
}; |
261 |
confirm.center(); |
262 |
} else
|
263 |
confirmRename(); |
264 |
} |
265 |
|
266 |
/**
|
267 |
* Confirm the renames of synonymous files already in the trash.
|
268 |
*/
|
269 |
private void confirmRename() { |
270 |
if (!toRename.isEmpty()) {
|
271 |
StringBuffer sb = new StringBuffer(); |
272 |
for (FileResource file: toRename.values())
|
273 |
sb.append(file.getName()).append("<br/>");
|
274 |
ConfirmationDialog confirm = new ConfirmationDialog("Files " + |
275 |
"with the following names already exist in the trash. If" +
|
276 |
" you continue,<br/>the trashed files will be renamed " +
|
277 |
"automatically for you:<br/><i>" + sb + "</i>", "Continue") { |
278 |
|
279 |
@Override
|
280 |
public void cancel() { |
281 |
hide(); |
282 |
} |
283 |
|
284 |
@Override
|
285 |
public void confirm() { |
286 |
updateTrashedFiles(); |
287 |
} |
288 |
|
289 |
}; |
290 |
confirm.center(); |
291 |
} else
|
292 |
uploadFiles(); |
293 |
} |
294 |
|
295 |
/**
|
296 |
* Rename the conflicting trashed files with the supplied new names.
|
297 |
*/
|
298 |
private void updateTrashedFiles() { |
299 |
for (final String name: toRename.keySet()) { |
300 |
JSONObject json = new JSONObject();
|
301 |
json.put("name", new JSONString(name)); |
302 |
PostCommand cf = new PostCommand(toRename.get(name).getUri() + "?update=", json.toString(), 200) { |
303 |
|
304 |
@Override
|
305 |
public void onComplete() { |
306 |
toRename.remove(name); |
307 |
uploadFiles(); |
308 |
} |
309 |
|
310 |
@Override
|
311 |
public void onError(Throwable t) { |
312 |
GSS app = GSS.get(); |
313 |
GWT.log("", t);
|
314 |
if (t instanceof RestException) { |
315 |
int statusCode = ((RestException) t).getHttpStatusCode();
|
316 |
if (statusCode == 405) |
317 |
app.displayError("You don't have the necessary permissions");
|
318 |
else if (statusCode == 404) |
319 |
app.displayError("User in permissions does not exist");
|
320 |
else if (statusCode == 409) |
321 |
app.displayError("A file with the same name already exists");
|
322 |
else if (statusCode == 413) |
323 |
app.displayError("Your quota has been exceeded");
|
324 |
else
|
325 |
app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
|
326 |
} else
|
327 |
app.displayError("System error modifying file:" + t.getMessage());
|
328 |
} |
329 |
|
330 |
}; |
331 |
DeferredCommand.addCommand(cf); |
332 |
} |
333 |
} |
334 |
|
335 |
/**
|
336 |
* Checks if the renaming step for already trashed files is complete and
|
337 |
* starts file uploads.
|
338 |
*/
|
339 |
private void uploadFiles() { |
340 |
if (!toRename.isEmpty()) return; |
341 |
if (canContinue){
|
342 |
doSend(selectedFiles); |
343 |
} |
344 |
} |
345 |
|
346 |
/**
|
347 |
* Perform the HTTP request to upload the specified file.
|
348 |
*/
|
349 |
protected void doSend(final List<File> filesRemaining) { |
350 |
final GSS app = GSS.get();
|
351 |
HttpRequest request = factory.createHttpRequest(); |
352 |
requests.add(request); |
353 |
String method = "PUT"; |
354 |
|
355 |
String path;
|
356 |
final String filename = getFilename(filesRemaining.get(0).getName()); |
357 |
path = folder.getUri(); |
358 |
if (!path.endsWith("/")) |
359 |
path = path + "/";
|
360 |
path = path + encode(filename); |
361 |
|
362 |
String token = app.getToken();
|
363 |
String resource = path.substring(app.getApiPath().length()-1, path.length()); |
364 |
String date = RestCommand.getDate();
|
365 |
String sig = RestCommand.calculateSig(method, date, resource, RestCommand.base64decode(token));
|
366 |
request.open(method, path); |
367 |
request.setRequestHeader("X-GSS-Date", date);
|
368 |
request.setRequestHeader("Authorization", app.getCurrentUserResource().getUsername() + " " + sig); |
369 |
request.setRequestHeader("Accept", "application/json; charset=utf-8"); |
370 |
request.setCallback(new RequestCallback() {
|
371 |
@Override
|
372 |
public void onResponseReceived(HttpRequest req) { |
373 |
int state = req.getReadyState();
|
374 |
if (state != 4) return; |
375 |
switch(req.getStatus()) {
|
376 |
case 201: // Created falls through to updated. |
377 |
case 204: |
378 |
filesRemaining.remove(0);
|
379 |
if(filesRemaining.isEmpty()){
|
380 |
finish(); |
381 |
break;
|
382 |
} |
383 |
doSend(filesRemaining); |
384 |
break;
|
385 |
case 403: |
386 |
SessionExpiredDialog dlg = new SessionExpiredDialog();
|
387 |
dlg.center(); |
388 |
break;
|
389 |
case 405: |
390 |
app.displayError("You don't have permission to " +
|
391 |
"upload file " + filename);
|
392 |
break;
|
393 |
case 409: |
394 |
app.displayError("A folder with the name " + filename +
|
395 |
" already exists at this level");
|
396 |
break;
|
397 |
case 413: |
398 |
app.displayError("There is not enough free space " +
|
399 |
"available for uploading " + filename);
|
400 |
break;
|
401 |
default:
|
402 |
app.displayError("Error uploading file " + filename +
|
403 |
": " + req.getStatus());
|
404 |
} |
405 |
} |
406 |
}); |
407 |
request.getUpload().setProgressHandler(new ProgressHandler() {
|
408 |
@Override
|
409 |
public void onProgress(ProgressEvent event) { |
410 |
double pcnt = (double) event.getLoaded() / event.getTotal(); |
411 |
progressBars.get(0).setProgress((int) Math.floor(pcnt * 100)); |
412 |
if(pcnt*100 == 100) |
413 |
progressBars.remove(0);
|
414 |
} |
415 |
}); |
416 |
request.send(filesRemaining.get(0).getBlob());
|
417 |
} |
418 |
|
419 |
/**
|
420 |
* Perform the final actions after the files are uploaded.
|
421 |
*/
|
422 |
protected void finish() { |
423 |
hide(); |
424 |
GSS.get().showFileList(true);
|
425 |
GSS.get().getStatusPanel().updateStats(); |
426 |
} |
427 |
|
428 |
/**
|
429 |
* Same as URL.encode, but also encode apostrophe since browsers aren't
|
430 |
* consistent about it (FF encodes, IE does not).
|
431 |
*/
|
432 |
protected String encode(String decodedURL) { |
433 |
String retv = decodedURL.replaceAll("@", "_"); // Replace bad character |
434 |
retv = URL.encodeComponent(retv);
|
435 |
retv = retv.replaceAll("'", "%27"); |
436 |
return retv;
|
437 |
} |
438 |
|
439 |
} |