root / src / pithos / content / connection.js @ 70:068d99580dd7
History | View | Annotate | Download (16.1 kB)
1 |
// The Pithos File Manager Firefox Extension is funded by GRNET S.A.
|
---|---|
2 |
// (http://www.grnet.gr)
|
3 |
//
|
4 |
// Copyright (c) 2009, Christos KK Loverdos, Vassilios Karakoidas.
|
5 |
// All rights reserved.
|
6 |
//
|
7 |
// Redistribution and use in source and binary forms, with or without
|
8 |
// modification, are permitted provided that the following conditions are
|
9 |
// met:
|
10 |
//
|
11 |
// - Redistributions of source code must retain the above copyright
|
12 |
// notice, this list of conditions and the following disclaimer.
|
13 |
// - Redistributions in binary form must reproduce the above
|
14 |
// copyright notice, this list of conditions and the following
|
15 |
// disclaimer in the documentation and/or other materials provided
|
16 |
// with the distribution.
|
17 |
// - Neither the name of GRNET S.A. nor the names of its contributors
|
18 |
// may be used to endorse or promote products derived from this
|
19 |
// software without specific prior written permission.
|
20 |
//
|
21 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
22 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
23 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
24 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
25 |
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
26 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
27 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
28 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
29 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
30 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
31 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32 |
|
33 |
connection_obj = null;
|
34 |
|
35 |
function ConnectionObject() { |
36 |
// public properties
|
37 |
this.token = 'ERROR: no token'; |
38 |
this.username = 'ERROR: no username'; |
39 |
this.login = 'ERROR: no login'; |
40 |
this.email = 'ERROR: no email'; |
41 |
this.rest_url = 'https://pithos.grnet.gr/pithos/rest'; |
42 |
this.async_op_status = null; |
43 |
|
44 |
// private properties
|
45 |
var user_homepage = null; |
46 |
var shared_data = null; |
47 |
var current = this; |
48 |
|
49 |
// public methods
|
50 |
this.isShared = function(uri) { |
51 |
if (shared_data == null) { |
52 |
this.get_shared_data();
|
53 |
} |
54 |
|
55 |
for (var i = 0; i < shared_data.length; i++) { |
56 |
if(uri.indexOf(shared_data[i]) >= 0) { |
57 |
return true; |
58 |
} |
59 |
} |
60 |
|
61 |
return false; |
62 |
} |
63 |
|
64 |
this.get_shared_data = function() { |
65 |
shared_data = new Array();
|
66 |
|
67 |
var d = vfs.getDirectory(user_homepage.shared);
|
68 |
|
69 |
for (var i in d.files) { |
70 |
shared_data.push(d.files[i].URI); |
71 |
} |
72 |
for (var i in d.folders) { |
73 |
shared_data.push(d.folders[i].URI); |
74 |
} |
75 |
|
76 |
return shared_data;
|
77 |
} |
78 |
|
79 |
this.user_homepage = function() { |
80 |
if(user_homepage == null) { |
81 |
user_homepage = new UserHomePage();
|
82 |
} |
83 |
|
84 |
return user_homepage;
|
85 |
} |
86 |
|
87 |
this.create_request_get = function(rest_req) { |
88 |
if ( vfs.isRemoteResource(rest_req) ) {
|
89 |
var gmtstring = (new Date()).toGMTString(); |
90 |
var sign_url = rest_req.replace(this.rest_url, ""); |
91 |
var hash = sign(this.token, sign_url, gmtstring, "GET"); |
92 |
|
93 |
return rest_req + ('?Date=' + encodeURIComponent(gmtstring) + '&Authorization=' + encodeURIComponent(this.username) + '%20' + encodeURIComponent(hash)); |
94 |
} else {
|
95 |
if ( isUnix() ) {
|
96 |
return 'file://' + rest_req; |
97 |
} |
98 |
if ( isWindows() ) {
|
99 |
return 'file:///' + rest_req.replace('\\','/'); |
100 |
} |
101 |
} |
102 |
|
103 |
return rest_req;
|
104 |
} |
105 |
|
106 |
this.rest_request_get_inline = function(rest_req) { |
107 |
var url = this.create_request_get(rest_req); |
108 |
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalFileRead UniversalBrowserRead");
|
109 |
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest); |
110 |
req.open("GET", url, false); |
111 |
req.overrideMimeType('text/plain; charset=x-user-defined');
|
112 |
req.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE; |
113 |
req.send(null);
|
114 |
|
115 |
return req;
|
116 |
} |
117 |
|
118 |
this.rest_request_get_inline_async = function(rest_req,params,destination) { |
119 |
var url = this.create_request_get(rest_req); |
120 |
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalFileRead UniversalBrowserRead");
|
121 |
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest); |
122 |
|
123 |
req.onload = this.onLoadDefault;
|
124 |
req.onprogress = this.onProgressDefault;
|
125 |
req.onerror = this.onErrorDefault;
|
126 |
|
127 |
req.addEventListener("progress", params.progress, false); |
128 |
req.addEventListener("load", params.complete, false); |
129 |
req.addEventListener("error", params.failed, false); |
130 |
req.addEventListener("abort", params.cancelled, false); |
131 |
|
132 |
req.open("GET", url, true); |
133 |
|
134 |
req.overrideMimeType('text/plain; charset=x-user-defined');
|
135 |
|
136 |
req.send(null);
|
137 |
|
138 |
return req;
|
139 |
} |
140 |
|
141 |
this.rest_request_put_upload_async = function(source, rest_req, data, mime, params) { |
142 |
var url = rest_req;
|
143 |
var sign_url = rest_req.replace(current.rest_url, ''); |
144 |
var gmtstring = (new Date()).toGMTString(); |
145 |
|
146 |
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalFileRead UniversalBrowserRead");
|
147 |
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); |
148 |
try {
|
149 |
file.initWithPath(source); |
150 |
} catch(error) {
|
151 |
alert(error); |
152 |
return null; |
153 |
} |
154 |
|
155 |
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalFileRead UniversalBrowserRead");
|
156 |
var stream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream); |
157 |
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalFileRead UniversalBrowserRead");
|
158 |
stream.init(file, 0x01, 0644, 0x04); // file is an nsIFile instance |
159 |
|
160 |
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalFileRead UniversalBrowserRead");
|
161 |
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest); |
162 |
|
163 |
req.onload = this.onLoadDefault;
|
164 |
req.onprogress = this.onProgressDefault;
|
165 |
req.onerror = this.onErrorDefault;
|
166 |
|
167 |
req.upload.addEventListener("progress", params.progress, false); |
168 |
req.upload.addEventListener("load", params.complete, false); |
169 |
req.upload.addEventListener("error", params.failed, false); |
170 |
req.upload.addEventListener("abort", params.cancelled, false); |
171 |
|
172 |
req.open("PUT", url, true); |
173 |
|
174 |
req.setRequestHeader('Authorization', current.username + " " + sign(current.token, sign_url, gmtstring, "PUT")); |
175 |
req.setRequestHeader('X-GSS-Date', gmtstring);
|
176 |
|
177 |
req.setRequestHeader("Content-type", mime + "; charset=utf-8"); |
178 |
//req.setRequestHeader("Content-length", data.length);
|
179 |
|
180 |
req.send(stream); |
181 |
|
182 |
return req;
|
183 |
} |
184 |
|
185 |
this.rest_request_put_upload = function(source, rest_req, data, mime) { |
186 |
var url = rest_req;
|
187 |
var sign_url = rest_req.replace(current.rest_url, ''); |
188 |
var gmtstring = (new Date()).toGMTString(); |
189 |
|
190 |
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalFileRead UniversalBrowserRead");
|
191 |
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); |
192 |
try {
|
193 |
file.initWithPath(source); |
194 |
} catch(error) {
|
195 |
alert(error); |
196 |
return null; |
197 |
} |
198 |
|
199 |
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalFileRead UniversalBrowserRead");
|
200 |
var stream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream); |
201 |
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalFileRead UniversalBrowserRead");
|
202 |
stream.init(file, 0x01, 0644, 0x04); // file is an nsIFile instance |
203 |
|
204 |
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalFileRead UniversalBrowserRead");
|
205 |
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest); |
206 |
req.open("PUT", url, false); |
207 |
|
208 |
req.setRequestHeader('Authorization', current.username + " " + sign(current.token, sign_url, gmtstring, "PUT")); |
209 |
req.setRequestHeader('X-GSS-Date', gmtstring);
|
210 |
|
211 |
req.setRequestHeader("Content-type", mime + "; charset=utf-8"); |
212 |
//req.setRequestHeader("Content-length", data.length);
|
213 |
req.send(stream); |
214 |
|
215 |
return req;
|
216 |
} |
217 |
|
218 |
this.rest_request_post_async = function(rest_req, params, data) { |
219 |
return rest_request_generic(rest_req, params, "POST", data, true, this.onLoadDefault, this.onProgressDefault, this.onErrorDefault); |
220 |
} |
221 |
|
222 |
this.rest_request_put_async = function(rest_req, params, data) { |
223 |
return rest_request_generic(rest_req, params, "PUT", data, true, this.onLoadDefault, this.onProgressDefault, this.onErrorDefault); |
224 |
} |
225 |
|
226 |
this.rest_request_head = function(rest_req, params) { |
227 |
return rest_request_generic(rest_req, params, "HEAD", null, false, null, null, null); |
228 |
} |
229 |
|
230 |
this.rest_request_delete = function(rest_req, params) { |
231 |
return rest_request_generic(rest_req, params, "DELETE", null, false, null, null, null); |
232 |
} |
233 |
|
234 |
this.rest_request_post = function(rest_req, params, data) { |
235 |
return rest_request_generic(rest_req, params, "POST", data, false, null, null, null); |
236 |
} |
237 |
|
238 |
this.rest_request_get = function(rest_req, params) { |
239 |
return this.rest_request_sync(rest_req, params); |
240 |
} |
241 |
|
242 |
this.rest_request_put = function(rest_req, params, data) { |
243 |
return rest_request_generic(rest_req, params, "PUT", data, false, null, null, null); |
244 |
} |
245 |
|
246 |
this.rest_request_sync = function(rest_req, params) { |
247 |
return rest_request_generic(rest_req, params, "GET", null, false, null, null, null); |
248 |
} |
249 |
|
250 |
this.rest_request_async = function(rest_req, params, on_load, on_progress, on_error) { |
251 |
return rest_request_generic(rest_req, params, "GET", null, true, on_load, on_progress, on_error); |
252 |
} |
253 |
|
254 |
this.success = function(status) { |
255 |
return (status >= 200 && status < 300); |
256 |
} |
257 |
|
258 |
// empty implementations of async events
|
259 |
// for easy programming
|
260 |
this.onLoadDefault = function(e) { } |
261 |
this.onProgressDefault = function(e) { } |
262 |
this.onErrorDefault = function(e) { } |
263 |
|
264 |
// private methods
|
265 |
function rest_request_generic(rest_req, params, method, data, async, on_load, on_progress, on_error) { |
266 |
var url = rest_req;
|
267 |
var sign_url = rest_req.replace(current.rest_url, ''); |
268 |
var gmtstring = new Date().toGMTString(); |
269 |
|
270 |
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalFileRead UniversalBrowserRead");
|
271 |
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest); |
272 |
req.open(method, url + params, async); |
273 |
|
274 |
if (async) {
|
275 |
req.onload = on_load; |
276 |
req.onprogress = on_progress; |
277 |
req.onerror = on_error; |
278 |
} |
279 |
|
280 |
req.setRequestHeader('X-GSS-Date', gmtstring);
|
281 |
req.setRequestHeader('Authorization', current.username + " " + sign(current.token, sign_url, gmtstring, method)); |
282 |
|
283 |
if ( method == "GET" || method == "HEAD" || method == "DELETE") { |
284 |
req.send(null);
|
285 |
} else if ( method == "POST" || method == "PUT" ) { |
286 |
req.setRequestHeader("Content-type", "text/plain; charset=utf-8"); |
287 |
req.setRequestHeader("Content-length", data.length);
|
288 |
req.send(data); |
289 |
} |
290 |
|
291 |
return req;
|
292 |
} |
293 |
|
294 |
function check_null_result(r, d) { |
295 |
var res = r.exec(d);
|
296 |
if (res == null) { return 'ERROR: no connection data'; } |
297 |
|
298 |
return res[1]; |
299 |
} |
300 |
|
301 |
function sign(token, thepath, gmtdate, method) { |
302 |
var rawdatastring = method.toUpperCase() + gmtdate + thepath;
|
303 |
var signature = gss_sign(token, rawdatastring);
|
304 |
|
305 |
return (signature);
|
306 |
} |
307 |
|
308 |
|
309 |
// Allow access to cookie database of browser
|
310 |
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalFileRead UniversalBrowserRead");
|
311 |
var cookieManager = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager); |
312 |
var menum = cookieManager.enumerator;
|
313 |
while (menum.hasMoreElements()){
|
314 |
var cookie = menum.getNext().QueryInterface(Components.interfaces.nsICookie);
|
315 |
if ( (cookie.host == '.pithos.grnet.gr') && (cookie.name == '_gss_a') ) { |
316 |
var tokens = cookie.value.split('|'); |
317 |
this.email = 'ERROR: no connection data'; |
318 |
this.username = tokens[0].replace('%40', '@'); |
319 |
this.login = this.username.split('@')[0]; |
320 |
this.token = tokens[1]; |
321 |
break;
|
322 |
} |
323 |
} |
324 |
|
325 |
//document.getElementById('label-username').value = this.username + ' (' + this.login + ')';
|
326 |
$mb = jQuery.noConflict();
|
327 |
$mb('#label-username').val(this.username + ' (' + this.login + ')'); |
328 |
} |
329 |
|
330 |
function UserHomePage() { |
331 |
// public properties
|
332 |
this.fileroot = 'ERROR: fileroot not defined'; |
333 |
this.trash = 'ERROR: trash is not defined'; |
334 |
this.shared = 'ERROR: shared is not defined'; |
335 |
this.others = 'ERROR: others is not defined'; |
336 |
this.tags = 'ERROR: tags is not defined'; |
337 |
this.groups = 'ERROR: groups is not defined'; |
338 |
this.announcement = 'ERROR: announcement is not defined'; |
339 |
this.creationdate = 0; |
340 |
this.modificationDate = 0; |
341 |
this.totalFiles = 0; |
342 |
this.totalBytes = 0; |
343 |
this.bytesRemaining = 0; |
344 |
|
345 |
// private properties
|
346 |
var req = connection_obj.rest_request_sync(connection_obj.rest_url + '/' + connection_obj.username + '/', ''); |
347 |
var homepage_json = JSON.parse(req.responseText);
|
348 |
|
349 |
// public methods
|
350 |
this.fileroot_object = function() { |
351 |
// TODO: do some checking here
|
352 |
return vfs.getDirectory(this.fileroot); |
353 |
} |
354 |
|
355 |
this.others_object = function() { |
356 |
var others_obj = JSON.parse(connection_obj.rest_request_get(connection_obj.user_homepage().others, '').responseText); |
357 |
// Create an object that resembles a Directory
|
358 |
var d = new Object(); |
359 |
|
360 |
d.name = connection_obj.user_homepage().others; |
361 |
d.path = connection_obj.user_homepage().others; |
362 |
d.URI = connection_obj.user_homepage().others; |
363 |
|
364 |
d.files = new Array();
|
365 |
d.folders = new Array();
|
366 |
d.permissions = new Array();
|
367 |
|
368 |
for ( i = 0; i < others_obj.length; i++ ) { |
369 |
var dd = new Object(); |
370 |
|
371 |
dd.name = others_obj[i].username; |
372 |
dd.URI = others_obj[i].uri + '/';
|
373 |
dd.isShared = true;
|
374 |
dd.contentImage = mimes.getMime('directory', true); |
375 |
|
376 |
d.folders.push(dd); |
377 |
} |
378 |
|
379 |
return d;
|
380 |
} |
381 |
|
382 |
this.tags_list = function() { |
383 |
var data = connection_obj.rest_request_get(this.tags,''); |
384 |
if(connection_obj.success(data.status)) {
|
385 |
return ( JSON.parse(data.responseText) );
|
386 |
} else {
|
387 |
return;
|
388 |
} |
389 |
} |
390 |
|
391 |
this.reinit = function() { |
392 |
var req = connection_obj.rest_request_sync(connection_obj.rest_url + '/' + connection_obj.username + '/', ''); |
393 |
homepage_json = JSON.parse(req.responseText); |
394 |
this.fileroot = homepage_json.fileroot + '/'; |
395 |
this.trash = homepage_json.trash + '/'; |
396 |
this.shared = homepage_json.shared + '/'; |
397 |
this.others = homepage_json.others + '/'; |
398 |
this.tags = homepage_json.tags + '/'; |
399 |
this.groups = homepage_json.groups + '/'; |
400 |
this.creationdate = homepage_json.creationDate;
|
401 |
this.modificationDate = homepage_json.modificationDate;
|
402 |
this.totalFiles = homepage_json.quota.totalFiles;
|
403 |
this.totalBytes = homepage_json.quota.totalBytes;
|
404 |
this.bytesRemaining = homepage_json.quota.bytesRemaining;
|
405 |
this.announcement = homepage_json.announcement;
|
406 |
} |
407 |
|
408 |
// private methods
|
409 |
|
410 |
// run initialization
|
411 |
|
412 |
this.fileroot = homepage_json.fileroot + '/'; |
413 |
this.trash = homepage_json.trash + '/'; |
414 |
this.shared = homepage_json.shared + '/'; |
415 |
this.others = homepage_json.others + '/'; |
416 |
this.tags = homepage_json.tags + '/'; |
417 |
this.groups = homepage_json.groups + '/'; |
418 |
this.creationdate = homepage_json.creationDate;
|
419 |
this.modificationDate = homepage_json.modificationDate;
|
420 |
this.totalFiles = homepage_json.quota.totalFiles;
|
421 |
this.totalBytes = homepage_json.quota.totalBytes;
|
422 |
this.bytesRemaining = homepage_json.quota.bytesRemaining;
|
423 |
this.announcement = homepage_json.announcement;
|
424 |
|
425 |
$mb = jQuery.noConflict();
|
426 |
$mb('#label-total-files').val(this.totalFiles + ' files'); |
427 |
$mb('#label-used-quota').val(Math.round(((this.totalBytes) / 1024) / 1024) + ' MB used'); |
428 |
$mb('#label-free-quota').val(Math.round((this.bytesRemaining / 1024) / 1024) + ' MB free'); |
429 |
$mb("#label-system-message").val(this.announcement); |
430 |
} |