root / web_client / src / gr / grnet / pithos / web / client / FileUploadDialog.java @ 9e6e0572
History | View | Annotate | Download (15.5 kB)
1 | ab1eb3f8 | Christos Stathis | /*
|
---|---|---|---|
2 | 6dd67d1c | Christos Stathis | * Copyright (c) 2011 Greek Research and Technology Network
|
3 | ab1eb3f8 | Christos Stathis | */
|
4 | ab1eb3f8 | Christos Stathis | package gr.grnet.pithos.web.client; |
5 | ab1eb3f8 | Christos Stathis | |
6 | ab1eb3f8 | Christos Stathis | import gr.grnet.pithos.web.client.rest.GetCommand; |
7 | ab1eb3f8 | Christos Stathis | import gr.grnet.pithos.web.client.rest.PostCommand; |
8 | ab1eb3f8 | Christos Stathis | import gr.grnet.pithos.web.client.rest.RestCommand; |
9 | ab1eb3f8 | Christos Stathis | import gr.grnet.pithos.web.client.rest.RestException; |
10 | ab1eb3f8 | Christos Stathis | import gr.grnet.pithos.web.client.rest.resource.FileResource; |
11 | ab1eb3f8 | Christos Stathis | import gr.grnet.pithos.web.client.rest.resource.FolderResource; |
12 | ab1eb3f8 | Christos Stathis | import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper; |
13 | ab1eb3f8 | Christos Stathis | import gr.grnet.pithos.web.client.rest.resource.UploadStatusResource; |
14 | ab1eb3f8 | Christos Stathis | |
15 | ab1eb3f8 | Christos Stathis | import java.util.ArrayList; |
16 | ab1eb3f8 | Christos Stathis | import java.util.List; |
17 | ab1eb3f8 | Christos Stathis | |
18 | ab1eb3f8 | Christos Stathis | import com.google.gwt.core.client.GWT; |
19 | ab1eb3f8 | Christos Stathis | import com.google.gwt.dom.client.NativeEvent; |
20 | ab1eb3f8 | Christos Stathis | import com.google.gwt.event.dom.client.ClickEvent; |
21 | ab1eb3f8 | Christos Stathis | import com.google.gwt.event.dom.client.ClickHandler; |
22 | ab1eb3f8 | Christos Stathis | import com.google.gwt.event.dom.client.KeyCodes; |
23 | ab1eb3f8 | Christos Stathis | import com.google.gwt.http.client.URL; |
24 | ab1eb3f8 | Christos Stathis | import com.google.gwt.json.client.JSONObject; |
25 | ab1eb3f8 | Christos Stathis | import com.google.gwt.json.client.JSONString; |
26 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.DeferredCommand; |
27 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.Event.NativePreviewEvent; |
28 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.Timer; |
29 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.Button; |
30 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.DialogBox; |
31 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.FileUpload; |
32 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.FormPanel; |
33 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent; |
34 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler; |
35 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.FormPanel.SubmitEvent; |
36 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.FormPanel.SubmitHandler; |
37 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.Grid; |
38 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.HTML; |
39 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
40 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.Hidden; |
41 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.HorizontalPanel; |
42 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.Label; |
43 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.VerticalPanel; |
44 | ab1eb3f8 | Christos Stathis | |
45 | ab1eb3f8 | Christos Stathis | /**
|
46 | ab1eb3f8 | Christos Stathis | * The 'File upload' dialog box implementation.
|
47 | ab1eb3f8 | Christos Stathis | */
|
48 | ab1eb3f8 | Christos Stathis | public class FileUploadDialog extends DialogBox implements Updateable { |
49 | ab1eb3f8 | Christos Stathis | |
50 | ab1eb3f8 | Christos Stathis | protected int prgBarInterval = 1500; |
51 | ab1eb3f8 | Christos Stathis | |
52 | ab1eb3f8 | Christos Stathis | private ProgressBar progressBar;
|
53 | ab1eb3f8 | Christos Stathis | |
54 | ab1eb3f8 | Christos Stathis | protected RepeatingTimer repeater = new RepeatingTimer(this, prgBarInterval); |
55 | ab1eb3f8 | Christos Stathis | |
56 | ab1eb3f8 | Christos Stathis | public static final boolean DONE = true; |
57 | ab1eb3f8 | Christos Stathis | |
58 | ab1eb3f8 | Christos Stathis | /**
|
59 | ab1eb3f8 | Christos Stathis | * The Form element that performs the file upload.
|
60 | ab1eb3f8 | Christos Stathis | */
|
61 | ab1eb3f8 | Christos Stathis | private final FormPanel form = new FormPanel(); |
62 | ab1eb3f8 | Christos Stathis | |
63 | ab1eb3f8 | Christos Stathis | private final FileUpload upload = new FileUpload(); |
64 | ab1eb3f8 | Christos Stathis | |
65 | ab1eb3f8 | Christos Stathis | protected final Label filenameLabel = new Label(""); |
66 | ab1eb3f8 | Christos Stathis | |
67 | ab1eb3f8 | Christos Stathis | protected List<FileResource> files; |
68 | ab1eb3f8 | Christos Stathis | |
69 | ab1eb3f8 | Christos Stathis | protected boolean cancelEvent = false; |
70 | ab1eb3f8 | Christos Stathis | |
71 | ab1eb3f8 | Christos Stathis | protected String fileNameToUse; |
72 | ab1eb3f8 | Christos Stathis | |
73 | ab1eb3f8 | Christos Stathis | protected FolderResource folder;
|
74 | ab1eb3f8 | Christos Stathis | |
75 | ab1eb3f8 | Christos Stathis | /**
|
76 | ab1eb3f8 | Christos Stathis | * The widget's constructor.
|
77 | ab1eb3f8 | Christos Stathis | */
|
78 | ab1eb3f8 | Christos Stathis | public FileUploadDialog() {
|
79 | ab1eb3f8 | Christos Stathis | // Set the dialog's caption.
|
80 | ab1eb3f8 | Christos Stathis | setText("File upload");
|
81 | ab1eb3f8 | Christos Stathis | setAnimationEnabled(true);
|
82 | ab1eb3f8 | Christos Stathis | // Since we're going to add a FileUpload widget, we'll need to set the
|
83 | ab1eb3f8 | Christos Stathis | // form to use the POST method, and multipart MIME encoding.
|
84 | ab1eb3f8 | Christos Stathis | form.setEncoding(FormPanel.ENCODING_MULTIPART); |
85 | ab1eb3f8 | Christos Stathis | form.setMethod(FormPanel.METHOD_POST); |
86 | ab1eb3f8 | Christos Stathis | |
87 | ab1eb3f8 | Christos Stathis | // Create a panel to hold all of the form widgets.
|
88 | ab1eb3f8 | Christos Stathis | VerticalPanel panel = new VerticalPanel();
|
89 | ab1eb3f8 | Christos Stathis | form.setWidget(panel); |
90 | ab1eb3f8 | Christos Stathis | final HTML info = new HTML("You may select a file to upload. Install" + |
91 | ab1eb3f8 | Christos Stathis | " <a href='http://gears.google.com/' target='_blank'>Google " +
|
92 | ab1eb3f8 | Christos Stathis | "Gears</a><br> for uploading multiple files simultaneously.");
|
93 | ab1eb3f8 | Christos Stathis | info.addStyleName("pithos-uploadNote");
|
94 | ab1eb3f8 | Christos Stathis | panel.add(info); |
95 | ab1eb3f8 | Christos Stathis | final Hidden date = new Hidden("Date", ""); |
96 | ab1eb3f8 | Christos Stathis | panel.add(date); |
97 | ab1eb3f8 | Christos Stathis | final Hidden auth = new Hidden("Authorization", ""); |
98 | ab1eb3f8 | Christos Stathis | panel.add(auth); |
99 | ab1eb3f8 | Christos Stathis | // Add an informative label with the folder name.
|
100 | ab1eb3f8 | Christos Stathis | Object selection = GSS.get().getTreeView().getSelection();
|
101 | ab1eb3f8 | Christos Stathis | folder = ((RestResourceWrapper) selection).getResource(); |
102 | ab1eb3f8 | Christos Stathis | upload.setName("file");
|
103 | ab1eb3f8 | Christos Stathis | filenameLabel.setText("");
|
104 | ab1eb3f8 | Christos Stathis | filenameLabel.setVisible(false);
|
105 | ab1eb3f8 | Christos Stathis | filenameLabel.setStyleName("props-labels");
|
106 | ab1eb3f8 | Christos Stathis | HorizontalPanel fileUloadPanel = new HorizontalPanel();
|
107 | ab1eb3f8 | Christos Stathis | fileUloadPanel.add(filenameLabel); |
108 | ab1eb3f8 | Christos Stathis | fileUloadPanel.add(upload); |
109 | ab1eb3f8 | Christos Stathis | upload.getElement().setId("fileUploadDiallog.uploadPanel");
|
110 | ab1eb3f8 | Christos Stathis | Grid generalTable = new Grid(2, 2); |
111 | ab1eb3f8 | Christos Stathis | generalTable.setText(0, 0, "Folder"); |
112 | ab1eb3f8 | Christos Stathis | generalTable.setText(1, 0, "File"); |
113 | ab1eb3f8 | Christos Stathis | generalTable.setText(0, 1, folder.getName()); |
114 | ab1eb3f8 | Christos Stathis | generalTable.setWidget(1, 1, fileUloadPanel); |
115 | ab1eb3f8 | Christos Stathis | generalTable.getCellFormatter().setStyleName(0, 0, "props-labels"); |
116 | ab1eb3f8 | Christos Stathis | generalTable.getCellFormatter().setStyleName(1, 0, "props-labels"); |
117 | ab1eb3f8 | Christos Stathis | generalTable.getCellFormatter().setStyleName(0, 1, "props-values"); |
118 | ab1eb3f8 | Christos Stathis | generalTable.getCellFormatter().setStyleName(1, 1, "props-values"); |
119 | ab1eb3f8 | Christos Stathis | generalTable.setCellSpacing(4);
|
120 | ab1eb3f8 | Christos Stathis | |
121 | ab1eb3f8 | Christos Stathis | panel.add(generalTable); |
122 | ab1eb3f8 | Christos Stathis | |
123 | ab1eb3f8 | Christos Stathis | // Create a panel to hold the buttons.
|
124 | ab1eb3f8 | Christos Stathis | HorizontalPanel buttons = new HorizontalPanel();
|
125 | ab1eb3f8 | Christos Stathis | |
126 | ab1eb3f8 | Christos Stathis | // Create the 'upload' button, along with a listener that submits the
|
127 | ab1eb3f8 | Christos Stathis | // form.
|
128 | ab1eb3f8 | Christos Stathis | final Button submit = new Button("Upload", new ClickHandler() { |
129 | ab1eb3f8 | Christos Stathis | @Override
|
130 | ab1eb3f8 | Christos Stathis | public void onClick(ClickEvent event) { |
131 | ab1eb3f8 | Christos Stathis | prepareAndSubmit(); |
132 | ab1eb3f8 | Christos Stathis | } |
133 | ab1eb3f8 | Christos Stathis | }); |
134 | ab1eb3f8 | Christos Stathis | submit.getElement().setId("fileUploadDialog.button.upload");
|
135 | ab1eb3f8 | Christos Stathis | buttons.add(submit); |
136 | ab1eb3f8 | Christos Stathis | buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER); |
137 | ab1eb3f8 | Christos Stathis | // Create the 'Cancel' button, along with a listener that hides the
|
138 | ab1eb3f8 | Christos Stathis | // dialog when the button is clicked.
|
139 | ab1eb3f8 | Christos Stathis | final Button cancel = new Button("Cancel", new ClickHandler() { |
140 | ab1eb3f8 | Christos Stathis | @Override
|
141 | ab1eb3f8 | Christos Stathis | public void onClick(ClickEvent event) { |
142 | ab1eb3f8 | Christos Stathis | repeater.finish(); |
143 | ab1eb3f8 | Christos Stathis | hide(); |
144 | ab1eb3f8 | Christos Stathis | } |
145 | ab1eb3f8 | Christos Stathis | }); |
146 | ab1eb3f8 | Christos Stathis | cancel.getElement().setId("fileUploadDialog.button.cancel");
|
147 | ab1eb3f8 | Christos Stathis | buttons.add(cancel); |
148 | ab1eb3f8 | Christos Stathis | buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER); |
149 | ab1eb3f8 | Christos Stathis | buttons.setSpacing(8);
|
150 | ab1eb3f8 | Christos Stathis | buttons.addStyleName("pithos-DialogBox");
|
151 | ab1eb3f8 | Christos Stathis | |
152 | ab1eb3f8 | Christos Stathis | // Add an event handler to the form.
|
153 | ab1eb3f8 | Christos Stathis | form.addSubmitHandler(new SubmitHandler() {
|
154 | ab1eb3f8 | Christos Stathis | |
155 | ab1eb3f8 | Christos Stathis | @Override
|
156 | ab1eb3f8 | Christos Stathis | public void onSubmit(SubmitEvent event) { |
157 | ab1eb3f8 | Christos Stathis | GSS app = GSS.get(); |
158 | ab1eb3f8 | Christos Stathis | // This event is fired just before the form is submitted. We can
|
159 | ab1eb3f8 | Christos Stathis | // take this opportunity to perform validation.
|
160 | ab1eb3f8 | Christos Stathis | if (upload.getFilename().length() == 0) { |
161 | ab1eb3f8 | Christos Stathis | app.displayError("You must select a file!");
|
162 | ab1eb3f8 | Christos Stathis | event.cancel(); |
163 | ab1eb3f8 | Christos Stathis | hide(); |
164 | ab1eb3f8 | Christos Stathis | } else {
|
165 | ab1eb3f8 | Christos Stathis | |
166 | ab1eb3f8 | Christos Stathis | canContinue(); |
167 | ab1eb3f8 | Christos Stathis | GWT.log("Cancel:" + cancelEvent, null); |
168 | ab1eb3f8 | Christos Stathis | if (cancelEvent) {
|
169 | ab1eb3f8 | Christos Stathis | cancelEvent = false;
|
170 | ab1eb3f8 | Christos Stathis | app.displayError("The specified file name already exists in this folder");
|
171 | ab1eb3f8 | Christos Stathis | event.cancel(); |
172 | ab1eb3f8 | Christos Stathis | hide(); |
173 | ab1eb3f8 | Christos Stathis | } else {
|
174 | ab1eb3f8 | Christos Stathis | |
175 | ab1eb3f8 | Christos Stathis | fileNameToUse = getFilename(upload.getFilename()); |
176 | ab1eb3f8 | Christos Stathis | String apath;
|
177 | ab1eb3f8 | Christos Stathis | FileResource selectedFile = getFileForName(fileNameToUse); |
178 | ab1eb3f8 | Christos Stathis | if (selectedFile == null ) { |
179 | ab1eb3f8 | Christos Stathis | //we are going to create a file
|
180 | ab1eb3f8 | Christos Stathis | apath = folder.getUri(); |
181 | ab1eb3f8 | Christos Stathis | if (!apath.endsWith("/")) |
182 | ab1eb3f8 | Christos Stathis | apath = apath + "/";
|
183 | ab1eb3f8 | Christos Stathis | apath = apath + encodeComponent(fileNameToUse); |
184 | ab1eb3f8 | Christos Stathis | } else
|
185 | ab1eb3f8 | Christos Stathis | apath = selectedFile.getUri(); |
186 | ab1eb3f8 | Christos Stathis | form.setAction(apath); |
187 | ab1eb3f8 | Christos Stathis | String dateString = RestCommand.getDate();
|
188 | ab1eb3f8 | Christos Stathis | String resource = apath.substring(app.getApiPath().length() - 1, apath.length()); |
189 | ab1eb3f8 | Christos Stathis | String sig = RestCommand.calculateSig("POST", dateString, resource, RestCommand.base64decode(app.getToken())); |
190 | ab1eb3f8 | Christos Stathis | date.setValue(dateString); |
191 | ab1eb3f8 | Christos Stathis | auth.setValue(app.getCurrentUserResource().getUsername() + " " + sig);
|
192 | ab1eb3f8 | Christos Stathis | GWT.log("FolderPATH:" + folder.getUri(), null); |
193 | ab1eb3f8 | Christos Stathis | submit.setEnabled(false);
|
194 | ab1eb3f8 | Christos Stathis | upload.setVisible(false);
|
195 | ab1eb3f8 | Christos Stathis | filenameLabel.setText(fileNameToUse); |
196 | ab1eb3f8 | Christos Stathis | filenameLabel.setVisible(true);
|
197 | ab1eb3f8 | Christos Stathis | repeater.start(); |
198 | ab1eb3f8 | Christos Stathis | progressBar.setVisible(true);
|
199 | ab1eb3f8 | Christos Stathis | } |
200 | ab1eb3f8 | Christos Stathis | } |
201 | ab1eb3f8 | Christos Stathis | |
202 | ab1eb3f8 | Christos Stathis | } |
203 | ab1eb3f8 | Christos Stathis | }); |
204 | ab1eb3f8 | Christos Stathis | form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
|
205 | ab1eb3f8 | Christos Stathis | |
206 | ab1eb3f8 | Christos Stathis | @Override
|
207 | ab1eb3f8 | Christos Stathis | public void onSubmitComplete(SubmitCompleteEvent event) { |
208 | ab1eb3f8 | Christos Stathis | // When the form submission is successfully completed, this
|
209 | ab1eb3f8 | Christos Stathis | // event is fired. Assuming the service returned a response
|
210 | ab1eb3f8 | Christos Stathis | // of type text/html, we can get the result text here (see
|
211 | ab1eb3f8 | Christos Stathis | // the FormPanel documentation for further explanation).
|
212 | ab1eb3f8 | Christos Stathis | String results = event.getResults();
|
213 | ab1eb3f8 | Christos Stathis | |
214 | ab1eb3f8 | Christos Stathis | // Unfortunately the results are never empty, even in
|
215 | ab1eb3f8 | Christos Stathis | // the absense of errors, so we have to check for '<pre></pre>'.
|
216 | ab1eb3f8 | Christos Stathis | if (!results.equalsIgnoreCase("<pre></pre>")) { |
217 | ab1eb3f8 | Christos Stathis | GWT.log(results, null);
|
218 | ab1eb3f8 | Christos Stathis | GSS.get().displayError(results); |
219 | ab1eb3f8 | Christos Stathis | } |
220 | ab1eb3f8 | Christos Stathis | progressBar.setProgress(100);
|
221 | ab1eb3f8 | Christos Stathis | cancelUpload(); |
222 | ab1eb3f8 | Christos Stathis | GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection()); |
223 | ab1eb3f8 | Christos Stathis | GSS.get().getStatusPanel().updateStats(); |
224 | ab1eb3f8 | Christos Stathis | |
225 | ab1eb3f8 | Christos Stathis | } |
226 | ab1eb3f8 | Christos Stathis | }); |
227 | ab1eb3f8 | Christos Stathis | |
228 | ab1eb3f8 | Christos Stathis | |
229 | ab1eb3f8 | Christos Stathis | panel.add(buttons); |
230 | ab1eb3f8 | Christos Stathis | progressBar = new ProgressBar(50, ProgressBar.SHOW_TIME_REMAINING); |
231 | ab1eb3f8 | Christos Stathis | panel.add(progressBar); |
232 | ab1eb3f8 | Christos Stathis | progressBar.setVisible(false);
|
233 | ab1eb3f8 | Christos Stathis | panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER); |
234 | ab1eb3f8 | Christos Stathis | panel.setCellHorizontalAlignment(progressBar, HasHorizontalAlignment.ALIGN_CENTER); |
235 | ab1eb3f8 | Christos Stathis | panel.addStyleName("pithos-DialogBox");
|
236 | ab1eb3f8 | Christos Stathis | addStyleName("pithos-DialogBox");
|
237 | ab1eb3f8 | Christos Stathis | setWidget(form); |
238 | ab1eb3f8 | Christos Stathis | } |
239 | ab1eb3f8 | Christos Stathis | |
240 | ab1eb3f8 | Christos Stathis | @Override
|
241 | ab1eb3f8 | Christos Stathis | protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
242 | ab1eb3f8 | Christos Stathis | super.onPreviewNativeEvent(preview);
|
243 | ab1eb3f8 | Christos Stathis | |
244 | ab1eb3f8 | Christos Stathis | NativeEvent evt = preview.getNativeEvent(); |
245 | ab1eb3f8 | Christos Stathis | if (evt.getType().equals("keydown")) |
246 | ab1eb3f8 | Christos Stathis | // Use the popup's key preview hooks to close the dialog when either
|
247 | ab1eb3f8 | Christos Stathis | // enter or escape is pressed.
|
248 | ab1eb3f8 | Christos Stathis | switch (evt.getKeyCode()) {
|
249 | ab1eb3f8 | Christos Stathis | case KeyCodes.KEY_ENTER:
|
250 | ab1eb3f8 | Christos Stathis | prepareAndSubmit(); |
251 | ab1eb3f8 | Christos Stathis | break;
|
252 | ab1eb3f8 | Christos Stathis | case KeyCodes.KEY_ESCAPE:
|
253 | ab1eb3f8 | Christos Stathis | cancelUpload(); |
254 | ab1eb3f8 | Christos Stathis | break;
|
255 | ab1eb3f8 | Christos Stathis | } |
256 | ab1eb3f8 | Christos Stathis | } |
257 | ab1eb3f8 | Christos Stathis | |
258 | ab1eb3f8 | Christos Stathis | |
259 | ab1eb3f8 | Christos Stathis | |
260 | ab1eb3f8 | Christos Stathis | /**
|
261 | ab1eb3f8 | Christos Stathis | * Cancels the file upload.
|
262 | ab1eb3f8 | Christos Stathis | */
|
263 | ab1eb3f8 | Christos Stathis | private void cancelUpload() { |
264 | ab1eb3f8 | Christos Stathis | repeater.finish(); |
265 | ab1eb3f8 | Christos Stathis | hide(); |
266 | ab1eb3f8 | Christos Stathis | } |
267 | ab1eb3f8 | Christos Stathis | |
268 | ab1eb3f8 | Christos Stathis | /**
|
269 | ab1eb3f8 | Christos Stathis | * Make any last minute checks and start the upload.
|
270 | ab1eb3f8 | Christos Stathis | */
|
271 | ab1eb3f8 | Christos Stathis | public void prepareAndSubmit() { |
272 | ab1eb3f8 | Christos Stathis | final String fname = getFilename(upload.getFilename()); |
273 | ab1eb3f8 | Christos Stathis | if (getFileForName(fname) == null) { |
274 | ab1eb3f8 | Christos Stathis | //we are going to create a file, so we check to see if there is a trashed file with the same name
|
275 | ab1eb3f8 | Christos Stathis | FileResource same = null;
|
276 | ab1eb3f8 | Christos Stathis | for (FileResource fres : folder.getFiles())
|
277 | ab1eb3f8 | Christos Stathis | if (fres.isDeleted() && fres.getName().equals(fname))
|
278 | ab1eb3f8 | Christos Stathis | same = fres; |
279 | ab1eb3f8 | Christos Stathis | if (same == null) |
280 | ab1eb3f8 | Christos Stathis | form.submit(); |
281 | ab1eb3f8 | Christos Stathis | else {
|
282 | ab1eb3f8 | Christos Stathis | final FileResource sameFile = same;
|
283 | ab1eb3f8 | Christos Stathis | GWT.log("Same deleted file", null); |
284 | ab1eb3f8 | Christos Stathis | ConfirmationDialog confirm = new ConfirmationDialog("A file with " + |
285 | ab1eb3f8 | Christos Stathis | "the same name exists in the trash. If you continue,<br/>the trashed " +
|
286 | ab1eb3f8 | Christos Stathis | "file '" + fname + "' will be renamed automatically for you.", "Continue") { |
287 | ab1eb3f8 | Christos Stathis | |
288 | ab1eb3f8 | Christos Stathis | @Override
|
289 | ab1eb3f8 | Christos Stathis | public void cancel() { |
290 | ab1eb3f8 | Christos Stathis | FileUploadDialog.this.hide(); |
291 | ab1eb3f8 | Christos Stathis | } |
292 | ab1eb3f8 | Christos Stathis | |
293 | ab1eb3f8 | Christos Stathis | @Override
|
294 | ab1eb3f8 | Christos Stathis | public void confirm() { |
295 | ab1eb3f8 | Christos Stathis | updateTrashedFile(getBackupFilename(fname), sameFile); |
296 | ab1eb3f8 | Christos Stathis | } |
297 | ab1eb3f8 | Christos Stathis | |
298 | ab1eb3f8 | Christos Stathis | }; |
299 | ab1eb3f8 | Christos Stathis | confirm.center(); |
300 | ab1eb3f8 | Christos Stathis | } |
301 | ab1eb3f8 | Christos Stathis | } |
302 | ab1eb3f8 | Christos Stathis | else {
|
303 | ab1eb3f8 | Christos Stathis | // We are going to update an existing file, so show a confirmation dialog.
|
304 | ab1eb3f8 | Christos Stathis | ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " + |
305 | ab1eb3f8 | Christos Stathis | "you want to update " + fname + "?", "Update") { |
306 | ab1eb3f8 | Christos Stathis | |
307 | ab1eb3f8 | Christos Stathis | @Override
|
308 | ab1eb3f8 | Christos Stathis | public void cancel() { |
309 | ab1eb3f8 | Christos Stathis | FileUploadDialog.this.hide(); |
310 | ab1eb3f8 | Christos Stathis | } |
311 | ab1eb3f8 | Christos Stathis | |
312 | ab1eb3f8 | Christos Stathis | @Override
|
313 | ab1eb3f8 | Christos Stathis | public void confirm() { |
314 | ab1eb3f8 | Christos Stathis | form.submit(); |
315 | ab1eb3f8 | Christos Stathis | } |
316 | ab1eb3f8 | Christos Stathis | |
317 | ab1eb3f8 | Christos Stathis | }; |
318 | ab1eb3f8 | Christos Stathis | confirm.center(); |
319 | ab1eb3f8 | Christos Stathis | } |
320 | ab1eb3f8 | Christos Stathis | } |
321 | ab1eb3f8 | Christos Stathis | |
322 | ab1eb3f8 | Christos Stathis | /**
|
323 | ab1eb3f8 | Christos Stathis | * Returns the file name from a potential full path argument. Apparently IE
|
324 | ab1eb3f8 | Christos Stathis | * insists on sending the full path name of a file when uploading, forcing
|
325 | ab1eb3f8 | Christos Stathis | * us to trim the extra path info. Since this is only observed on Windows we
|
326 | ab1eb3f8 | Christos Stathis | * get to check for a single path separator value.
|
327 | ab1eb3f8 | Christos Stathis | *
|
328 | ab1eb3f8 | Christos Stathis | * @param name the potentially full path name of a file
|
329 | ab1eb3f8 | Christos Stathis | * @return the file name without extra path information
|
330 | ab1eb3f8 | Christos Stathis | */
|
331 | ab1eb3f8 | Christos Stathis | protected String getFilename(String name) { |
332 | ab1eb3f8 | Christos Stathis | int pathSepIndex = name.lastIndexOf("\\"); |
333 | ab1eb3f8 | Christos Stathis | if (pathSepIndex == -1) { |
334 | ab1eb3f8 | Christos Stathis | pathSepIndex = name.lastIndexOf("/");
|
335 | ab1eb3f8 | Christos Stathis | if (pathSepIndex == -1) |
336 | ab1eb3f8 | Christos Stathis | return name;
|
337 | ab1eb3f8 | Christos Stathis | } |
338 | ab1eb3f8 | Christos Stathis | return name.substring(pathSepIndex + 1); |
339 | ab1eb3f8 | Christos Stathis | } |
340 | ab1eb3f8 | Christos Stathis | |
341 | ab1eb3f8 | Christos Stathis | /**
|
342 | ab1eb3f8 | Christos Stathis | * Check whether the file name exists in selected folder.
|
343 | ab1eb3f8 | Christos Stathis | *
|
344 | ab1eb3f8 | Christos Stathis | * @return
|
345 | ab1eb3f8 | Christos Stathis | */
|
346 | ab1eb3f8 | Christos Stathis | private boolean canContinue() { |
347 | ab1eb3f8 | Christos Stathis | if (files == null) |
348 | ab1eb3f8 | Christos Stathis | return false; |
349 | ab1eb3f8 | Christos Stathis | String fileName = getFilename(upload.getFilename());
|
350 | ab1eb3f8 | Christos Stathis | if (getFileForName(fileName) == null) { |
351 | ab1eb3f8 | Christos Stathis | // For file creation, check to see if the file already exists.
|
352 | ab1eb3f8 | Christos Stathis | GWT.log("filename to upload:" + fileName, null); |
353 | ab1eb3f8 | Christos Stathis | for (FileResource dto : files) {
|
354 | ab1eb3f8 | Christos Stathis | GWT.log("Check:" + dto.getName() + "/" + fileName, null); |
355 | ab1eb3f8 | Christos Stathis | if (!dto.isDeleted() && dto.getName().equals(fileName)) {
|
356 | ab1eb3f8 | Christos Stathis | cancelEvent = true;
|
357 | ab1eb3f8 | Christos Stathis | return true; |
358 | ab1eb3f8 | Christos Stathis | } |
359 | ab1eb3f8 | Christos Stathis | } |
360 | ab1eb3f8 | Christos Stathis | } |
361 | ab1eb3f8 | Christos Stathis | return true; |
362 | ab1eb3f8 | Christos Stathis | } |
363 | ab1eb3f8 | Christos Stathis | |
364 | ab1eb3f8 | Christos Stathis | class RepeatingTimer extends Timer { |
365 | ab1eb3f8 | Christos Stathis | |
366 | ab1eb3f8 | Christos Stathis | private Updateable updateable;
|
367 | ab1eb3f8 | Christos Stathis | |
368 | ab1eb3f8 | Christos Stathis | private int interval = 1500; |
369 | ab1eb3f8 | Christos Stathis | |
370 | ab1eb3f8 | Christos Stathis | private boolean running = true; |
371 | ab1eb3f8 | Christos Stathis | |
372 | ab1eb3f8 | Christos Stathis | RepeatingTimer(Updateable _updateable, int _interval) {
|
373 | ab1eb3f8 | Christos Stathis | updateable = _updateable; |
374 | ab1eb3f8 | Christos Stathis | interval = _interval; |
375 | ab1eb3f8 | Christos Stathis | } |
376 | ab1eb3f8 | Christos Stathis | |
377 | ab1eb3f8 | Christos Stathis | @Override
|
378 | ab1eb3f8 | Christos Stathis | public void run() { |
379 | ab1eb3f8 | Christos Stathis | updateable.update(); |
380 | ab1eb3f8 | Christos Stathis | } |
381 | ab1eb3f8 | Christos Stathis | |
382 | ab1eb3f8 | Christos Stathis | public void start() { |
383 | ab1eb3f8 | Christos Stathis | running = true;
|
384 | ab1eb3f8 | Christos Stathis | |
385 | ab1eb3f8 | Christos Stathis | scheduleRepeating(interval); |
386 | ab1eb3f8 | Christos Stathis | } |
387 | ab1eb3f8 | Christos Stathis | |
388 | ab1eb3f8 | Christos Stathis | public void finish() { |
389 | ab1eb3f8 | Christos Stathis | running = false;
|
390 | ab1eb3f8 | Christos Stathis | cancel(); |
391 | ab1eb3f8 | Christos Stathis | } |
392 | ab1eb3f8 | Christos Stathis | |
393 | ab1eb3f8 | Christos Stathis | public int getInterval() { |
394 | ab1eb3f8 | Christos Stathis | return interval;
|
395 | ab1eb3f8 | Christos Stathis | } |
396 | ab1eb3f8 | Christos Stathis | |
397 | ab1eb3f8 | Christos Stathis | public void setInterval(int anInterval) { |
398 | ab1eb3f8 | Christos Stathis | if (interval != anInterval) {
|
399 | ab1eb3f8 | Christos Stathis | interval = anInterval; |
400 | ab1eb3f8 | Christos Stathis | if (running) {
|
401 | ab1eb3f8 | Christos Stathis | finish(); |
402 | ab1eb3f8 | Christos Stathis | start(); |
403 | ab1eb3f8 | Christos Stathis | } |
404 | ab1eb3f8 | Christos Stathis | } |
405 | ab1eb3f8 | Christos Stathis | } |
406 | ab1eb3f8 | Christos Stathis | } |
407 | ab1eb3f8 | Christos Stathis | |
408 | ab1eb3f8 | Christos Stathis | @Override
|
409 | ab1eb3f8 | Christos Stathis | public void update() { |
410 | ab1eb3f8 | Christos Stathis | String apath = folder.getUri();
|
411 | ab1eb3f8 | Christos Stathis | if (!apath.endsWith("/")) |
412 | ab1eb3f8 | Christos Stathis | apath = apath + "/";
|
413 | ab1eb3f8 | Christos Stathis | apath = apath + encodeComponent(fileNameToUse) + "?progress=" + encodeComponent(fileNameToUse);
|
414 | ab1eb3f8 | Christos Stathis | GetCommand eg = new GetCommand<UploadStatusResource>(UploadStatusResource.class, apath, false, null) { |
415 | ab1eb3f8 | Christos Stathis | |
416 | ab1eb3f8 | Christos Stathis | @Override
|
417 | ab1eb3f8 | Christos Stathis | public void onComplete() { |
418 | ab1eb3f8 | Christos Stathis | UploadStatusResource res = getResult(); |
419 | ab1eb3f8 | Christos Stathis | progressBar.setProgress(res.percent()); |
420 | ab1eb3f8 | Christos Stathis | } |
421 | ab1eb3f8 | Christos Stathis | |
422 | ab1eb3f8 | Christos Stathis | @Override
|
423 | ab1eb3f8 | Christos Stathis | public void onError(Throwable t) { |
424 | ab1eb3f8 | Christos Stathis | GWT.log("", t);
|
425 | ab1eb3f8 | Christos Stathis | } |
426 | ab1eb3f8 | Christos Stathis | |
427 | ab1eb3f8 | Christos Stathis | }; |
428 | ab1eb3f8 | Christos Stathis | DeferredCommand.addCommand(eg); |
429 | ab1eb3f8 | Christos Stathis | } |
430 | ab1eb3f8 | Christos Stathis | |
431 | ab1eb3f8 | Christos Stathis | protected String getBackupFilename(String filename) { |
432 | ab1eb3f8 | Christos Stathis | List<FileResource> filesInSameFolder = new ArrayList<FileResource>(); |
433 | ab1eb3f8 | Christos Stathis | for (FileResource deleted : folder.getFiles())
|
434 | ab1eb3f8 | Christos Stathis | if (deleted.isDeleted())
|
435 | ab1eb3f8 | Christos Stathis | filesInSameFolder.add(deleted); |
436 | ab1eb3f8 | Christos Stathis | int i = 1; |
437 | ab1eb3f8 | Christos Stathis | for (FileResource same : filesInSameFolder)
|
438 | ab1eb3f8 | Christos Stathis | if (same.getName().startsWith(filename)) {
|
439 | ab1eb3f8 | Christos Stathis | String toCheck = same.getName().substring(filename.length(), same.getName().length());
|
440 | ab1eb3f8 | Christos Stathis | if (toCheck.startsWith(" ")) { |
441 | ab1eb3f8 | Christos Stathis | int test = -1; |
442 | ab1eb3f8 | Christos Stathis | try {
|
443 | ab1eb3f8 | Christos Stathis | test = Integer.valueOf(toCheck.replace(" ", "")); |
444 | ab1eb3f8 | Christos Stathis | } catch (NumberFormatException e) { |
445 | ab1eb3f8 | Christos Stathis | // Do nothing since string is not a number.
|
446 | ab1eb3f8 | Christos Stathis | } |
447 | ab1eb3f8 | Christos Stathis | if (test >= i)
|
448 | ab1eb3f8 | Christos Stathis | i = test + 1;
|
449 | ab1eb3f8 | Christos Stathis | } |
450 | ab1eb3f8 | Christos Stathis | } |
451 | ab1eb3f8 | Christos Stathis | |
452 | ab1eb3f8 | Christos Stathis | return filename + " " + i; |
453 | ab1eb3f8 | Christos Stathis | } |
454 | ab1eb3f8 | Christos Stathis | |
455 | ab1eb3f8 | Christos Stathis | /**
|
456 | ab1eb3f8 | Christos Stathis | * Rename the conflicting trashed file with the supplied new name.
|
457 | ab1eb3f8 | Christos Stathis | */
|
458 | ab1eb3f8 | Christos Stathis | private void updateTrashedFile(String newName, FileResource trashedFile) { |
459 | ab1eb3f8 | Christos Stathis | JSONObject json = new JSONObject();
|
460 | ab1eb3f8 | Christos Stathis | json.put("name", new JSONString(newName)); |
461 | ab1eb3f8 | Christos Stathis | PostCommand cf = new PostCommand(trashedFile.getUri() + "?update=", json.toString(), 200) { |
462 | ab1eb3f8 | Christos Stathis | |
463 | ab1eb3f8 | Christos Stathis | @Override
|
464 | ab1eb3f8 | Christos Stathis | public void onComplete() { |
465 | ab1eb3f8 | Christos Stathis | form.submit(); |
466 | ab1eb3f8 | Christos Stathis | } |
467 | ab1eb3f8 | Christos Stathis | |
468 | ab1eb3f8 | Christos Stathis | @Override
|
469 | ab1eb3f8 | Christos Stathis | public void onError(Throwable t) { |
470 | ab1eb3f8 | Christos Stathis | GSS app = GSS.get(); |
471 | ab1eb3f8 | Christos Stathis | GWT.log("", t);
|
472 | ab1eb3f8 | Christos Stathis | if (t instanceof RestException) { |
473 | ab1eb3f8 | Christos Stathis | int statusCode = ((RestException) t).getHttpStatusCode();
|
474 | ab1eb3f8 | Christos Stathis | if (statusCode == 405) |
475 | ab1eb3f8 | Christos Stathis | app.displayError("You don't have the necessary permissions");
|
476 | ab1eb3f8 | Christos Stathis | else if (statusCode == 404) |
477 | ab1eb3f8 | Christos Stathis | app.displayError("User in permissions does not exist");
|
478 | ab1eb3f8 | Christos Stathis | else if (statusCode == 409) |
479 | ab1eb3f8 | Christos Stathis | app.displayError("A file with the same name already exists");
|
480 | ab1eb3f8 | Christos Stathis | else if (statusCode == 413) |
481 | ab1eb3f8 | Christos Stathis | app.displayError("Your quota has been exceeded");
|
482 | ab1eb3f8 | Christos Stathis | else
|
483 | ab1eb3f8 | Christos Stathis | app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
|
484 | ab1eb3f8 | Christos Stathis | } else
|
485 | ab1eb3f8 | Christos Stathis | app.displayError("System error modifying file:" + t.getMessage());
|
486 | ab1eb3f8 | Christos Stathis | } |
487 | ab1eb3f8 | Christos Stathis | |
488 | ab1eb3f8 | Christos Stathis | }; |
489 | ab1eb3f8 | Christos Stathis | DeferredCommand.addCommand(cf); |
490 | ab1eb3f8 | Christos Stathis | } |
491 | ab1eb3f8 | Christos Stathis | |
492 | ab1eb3f8 | Christos Stathis | protected FileResource getFileForName(String name){ |
493 | ab1eb3f8 | Christos Stathis | for (FileResource f : folder.getFiles())
|
494 | ab1eb3f8 | Christos Stathis | if (!f.isDeleted() && f.getName().equals(name))
|
495 | ab1eb3f8 | Christos Stathis | return f;
|
496 | ab1eb3f8 | Christos Stathis | return null; |
497 | ab1eb3f8 | Christos Stathis | } |
498 | ab1eb3f8 | Christos Stathis | |
499 | ab1eb3f8 | Christos Stathis | |
500 | ab1eb3f8 | Christos Stathis | /**
|
501 | ab1eb3f8 | Christos Stathis | * Same as URL.encodeComponent, but also
|
502 | ab1eb3f8 | Christos Stathis | * encode apostrophe since browsers aren't consistent about it
|
503 | ab1eb3f8 | Christos Stathis | * (FF encodes, IE does not).
|
504 | ab1eb3f8 | Christos Stathis | */
|
505 | ab1eb3f8 | Christos Stathis | private String encodeComponent(String decodedURLComponent) { |
506 | ab1eb3f8 | Christos Stathis | String retv = URL.encodeComponent(decodedURLComponent); |
507 | ab1eb3f8 | Christos Stathis | retv = retv.replaceAll("'", "%27"); |
508 | ab1eb3f8 | Christos Stathis | return retv;
|
509 | ab1eb3f8 | Christos Stathis | } |
510 | ab1eb3f8 | Christos Stathis | |
511 | ab1eb3f8 | Christos Stathis | /**
|
512 | ab1eb3f8 | Christos Stathis | * Modify the files.
|
513 | ab1eb3f8 | Christos Stathis | *
|
514 | ab1eb3f8 | Christos Stathis | * @param newFiles the files to set
|
515 | ab1eb3f8 | Christos Stathis | */
|
516 | ab1eb3f8 | Christos Stathis | public void setFiles(List<FileResource> newFiles) { |
517 | ab1eb3f8 | Christos Stathis | files = newFiles; |
518 | ab1eb3f8 | Christos Stathis | } |
519 | ab1eb3f8 | Christos Stathis | } |