Statistics
| Branch: | Revision:

root / src / pithos / content / vfs.js @ 68:0ec0b09ea126

History | View | Annotate | Download (4.7 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
vfs = null;
34

    
35
function VFS() {
36
        // public properties
37
        
38
        // private properties
39
        var special_files = {};
40
        
41
        // public methods
42

    
43
        // Always returns a json object
44
        this.getDirectory = function(uri) {
45
                if ( uri.indexOf(connection_obj.rest_url + '/search/') >= 0 ) {
46
                        return getSearchResult(uri, JSON.parse(connection_obj.rest_request_get(uri, '').responseText));
47
                }
48
        
49
                var r = special_files[uri];
50
                // is not a special file
51
                if ( r == undefined ) {
52
                        return load_directory_sync(uri);
53
                }
54
                
55
                return r(); // return the special case
56
        }
57

    
58
    this.getResourceAsBinary = function(uri) {
59
        if ( this.isRemoteResource(uri) ) {
60
            var req = connection_obj.rest_request_get_inline(uri);
61
            if ( connection_obj.success(req.status) ) {
62
                return req.responseText;
63
            } else {
64
                return null;
65
            }
66
        } else if ( this.isLocalResource(uri) ) {
67
            return loadBinaryFile(uri);
68
        }
69

    
70
        return null;
71
    }
72

    
73

    
74
        // Returns the metadata as a json object
75
        this.getResourceMetadata = function(uri) {
76
                var fm_obj = null;
77
                
78
                if ( this.isRemoteResource(uri) ) {
79
                        if ( uri.charAt(uri.length - 1) == '/' ) {
80
                                // is directory
81
                                fm_obj = this.getDirectory(uri);
82
                        } else {
83
                                // is file
84
                                var req = connection_obj.rest_request_head(uri, '?' + Math.random());
85
                                fm_obj = JSON.parse(req.getResponseHeader('X-GSS-Metadata'));                                
86
                        }
87
                } else if ( this.isLocalResource(uri) ) {
88
                        if ( this.isDirectory(uri) ) {
89
                                fm_obj = load_local_directory(uri);
90
                        } else {
91
                                fm_obj = loadLocalFile(uri);
92
                        }
93
                }
94
                
95
                return fm_obj;
96
        }
97
        
98
        this.isRemoteResource = function(uri) {
99
                return (uri.indexOf('http') != -1);
100
        }
101
        
102
        this.isLocalResource = function(uri) {
103
                return !this.isRemoteResource(uri);
104
        }
105
        
106
        this.isDirectory = function(uri) {
107
                if ( this.isRemoteResource(uri)) {
108
                        return (uri.charAt(uri.length - 1) == '/');
109
                } else if ( this.isLocalResource(uri)) {
110
                        try {
111
                                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalFileRead UniversalBrowserRead");
112
                                var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
113
                                file.initWithPath(uri);
114
                
115
                                return file.isDirectory();
116
                        } catch (error) {
117
                                return false;
118
                        }
119
                }
120
        }
121
        
122
        this.isFile = function(uri) {
123
                return !this.isDirectory(uri);                
124
        }
125
        
126
        // private methods
127

    
128
        // 
129
        function load_directory_sync(uri) {
130
                var d = cache.get(uri);
131
        
132
                if (d == undefined) {
133
                        if ( vfs.isRemoteResource(uri) ) {
134
                                // remote dir, do a json call
135
                                var req = connection_obj.rest_request_sync(uri, '');
136
                                var rt = req.responseText;
137
                                if (rt.length == 0) { rt = "{}"; }
138
                                return (new Directory(uri, JSON.parse(rt)));
139
                        } else if ( vfs.isLocalResource(uri) ) {
140
                                // local dir
141
                                return (new Directory(uri, load_local_directory(uri)));
142
                        }
143
                }
144
                
145
                return d;
146
        }
147
        
148
        // init special files
149
        // Handle cases where we do not have normal directory structure
150
        special_files[connection_obj.user_homepage().others] = connection_obj.user_homepage().others_object;
151
}