forked from rejetto/hfs-aborted-see-new-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin-server.js
315 lines (285 loc) · 11.3 KB
/
admin-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* @fileOverview This serves the back-end GUI and all it needs to perform server-side actions.
* @author Massimo Melina <a@rejetto.com>
*/
var http = require('http');
var socket_io = require('socket.io');
var serving = require('./lib/serving');
exports.start = function(listenOn) {
srv.listenOn = listenOn;
srv.listen(listenOn.port, listenOn.ip, function(){
dbg('listening on port '+listenOn.port);
});
}
var srv = http.createServer(function(httpReq,httpRes){
if (!serving.parseUrl(httpReq)) return;
/*
var peer = httpReq.socket.address(); // bug: currently peer.port is our listening port, while we want to show the port of the socket serving the connection
dbg('serving '+peer.address+':'+peer.port+' '+httpReq.url);
*/
if (serving.serveStatic(httpReq, httpRes)) return; // access to the special 'static' folder
if (httpReq.uri == '/') {
serving.serveFile('static/backend/index.html', httpReq, httpRes);
return;
}
serving.serve(httpRes, 404); // we serve nothing else
});
srv.on('error', function(err){
switch (err.code) {
case 'EADDRINUSE':
return dbg('port '+srv.listenOn.port+' busy');
}
});
/* converts FileNode to an object that's ready to be streamed to the back-end.
@param {number} depth specifies how many levels of children should be included in the structure.
@param {function} cb is necessary if you specify a depth that's more than zero
@return the object representing the FileNode if no depth is specified. Otherwise the returning turns into
an async fashion and you need to specify a callback to retrieve the result.
*/
function nodeToObjectForStreaming(fnode, depth, cb, isRecurring) {
assert(!cb || isFunction(cb), 'cb'); // this is necessarily an async procedure: require a callback
if (!fnode) {
if (cb) cb(false);
return false;
}
assert(fnode instanceof vfsLib.FileNode, 'fnode');
var res = fnode._clone()._expand({ name:fnode.name }); // make a copy of the whole object without recurring, and overwriting the getter 'name'
var s = fnode.stats; // cannot use _clone on this one
if (s) {
res.ctime = s.ctime.toJSON();
res.mtime = s.mtime.toJSON();
if (res.mtime===res.ctime)
delete res.mtime;
if (fnode.isFile())
res.size = s.size;
}
delete res._parent; // this makes a circular reference
delete res.children; // in case we want the true listing, not just the children
if (!res.customName) {
delete res.name;
}
delete res.customName;
// save bandwidth by not sending some empty properties
if (res.deletedItems
&& !res.deletedItems.length) {
delete res.deletedItems;
}
if (isRecurring && res.resource) {
res.resource = fnode.resourceRelativeTo(fnode.parent);
}
if (!res.resource) {
delete res.resource;
}
// depth is not required, or not possible, our job ends here
if (!depth
|| !fnode.isFolder()) {
if (cb) cb(res);
return res;
}
// recur on children
fnode.dir(function(items,bads){
res.children = [];
async.forEach(items._values(), function(e, doneThis){
nodeToObjectForStreaming(e, depth-1, function(obj){
res.children.push(obj);
doneThis();
}, true);
}, function(err) {
assert(!err, 'illegal');
if (!res.children.length)
delete res.children;
cb(res, bads);
});
});
} // nodeToObjectForStreaming
/*
SET UP SOCKET.IO
*/
var sockets = serving.sockets(srv, {
'vfs.get': function onGet(data, cb) {
if (serving.ioError(cb, !data ? 'data'
: !isString(data.uri) ? 'uri'
: null)) return;
vfs.fromUrl(data.uri, function(fnode) {
if (!fnode)
return serving.ioError(cb, 'not found');
nodeToObjectForStreaming(fnode, Math.min(2,data.depth), function(res){
serving.ioOk(cb, { item:res });
});
});
},
// set properties of a vfs item
'vfs.set': function onSet(data, cb){
var socket = this;
// assertions
if (serving.ioError(cb, !data ? 'data'
: !isString(data.uri) ? 'uri'
: null)) return;
vfs.fromUrl(data.uri, function(fnode) {
if (serving.ioError(cb, !fnode ? 'not found'
: 'name' in data && !isString(data.name) ? 'name'
: 'resource' in data && !isString(data.resource) ? 'resource'
: null)) return;
if ('name' in data)
fnode.name = data.name;
async.series([
function(pathSet){
if ('resource' in data)
return fnode.setPath(data.resource, function(err){
serving.ioError(cb, err && choose(err.code,{ ENOENT:'resource not found' },err.code))
|| pathSet();
});
pathSet();
},
function(overlapRefreshed){
fnode.checkOverlapping(overlapRefreshed);
},
function(){
serving.ioOk(cb, { item:nodeToObjectForStreaming(fnode) });
notifyVfsChange(socket, fnode.getURI(true));
}
]);
});
},
// add an item to the vfs
'vfs.add': function onAdd(data, cb){
var socket = this;
// assertions
if (serving.ioError(cb, !data ? 'data'
: !isString(data.uri) ? 'uri'
: !isString(data.resource) ? 'resource'
: null)) return;
vfs.fromUrl(data.uri, function(fnode) {
if (!fnode) {
serving.ioError(cb, 'uri not found');
return;
}
var already = fnode.getChildByName(path.basename(data.resource)); // check if it already exists
if (already) {
serving.ioError(cb, 'already exists');
return;
}
fnode.add(data.resource, function(err, newNode){
serving.ioOk(cb, {item:nodeToObjectForStreaming(newNode)});
notifyVfsChange(socket, newNode.getURI(true));
});
});
},
// delete item, make it non-existent in the VFS
'vfs.delete': function onRemove(data, cb){
var socket = this;
// assertions
if (serving.ioError(cb, !data ? 'data'
: !isString(data.uri) ? 'uri'
: null)) return;
vfs.fromUrl(data.uri, function(fnode){
if (!fnode) {
serving.ioError(cb, 'uri not found')
return;
}
fnode.delete(function(folder){
// if we just deleted a dynamic item, the GUI may need an extra refresh
serving.ioOk(cb, {
dynamicItem: fnode.nodeKind!==NK.FIXED && path.basename(fnode.resource)+(fnode.isFolder() ? '/' : ''), // a trailing slash denotes folders
folderDeletedCount: folder.deletedItems ? folder.deletedItems.length : 0
});
notifyVfsChange(socket, folder.getURI());
});
});
},
// move an item within the vfs
'vfs.move': function onMove(data, cb){
var socket = this;
// assertions
if (serving.ioError(cb, !data ? 'data'
: !isString(data.from) ? 'from'
: !isString(data.to) ? 'to'
: null)) return;
vfs.fromUrl(data.to, function(destination){
if (serving.ioError(cb, !destination && 'destination not found')) return;
vfs.fromUrl(data.from, function(source){
if (serving.ioError(cb, !source ? 'source not found'
: destination.getChildByName(source.name) ? 'already exists'
: null)) return;
var originalSourceFolder = source.getURI(true).split('/').slice(0,-1).join('/')+'/'; // one level above
if (source.isFixed()
&& path.dirname(source.resource) !== destination.resource) { // we must exclude from this kind of treatment the case where source is a previously moved child of destination
source.parent = destination;
return done(source);
}
source.delete(function(){
destination.add(source.resource, function(err,addition){
serving.ioError(cb, err)
|| done(addition);
});
});
function done(item){
item.checkOverlapping(function(){
vfs.fromUrl(data.from, function(updatedFrom){
serving.ioOk(cb, {
wasOverlapped:nodeToObjectForStreaming(updatedFrom)||null, // sometimes moving a file reveals the overlapped one
to:nodeToObjectForStreaming(item)
});
});
notifyVfsChange(socket, originalSourceFolder);
notifyVfsChange(socket, data.from);
});
}
});
});
},
// restore a temp item that was deleted
'vfs.restore': function onRestore(data, cb){
var socket = this;
// assertions
if (serving.ioError(cb, !data ? 'data'
: !isString(data.uri) ? 'uri'
: !isString(data.resource) ? 'resource'
: null)) return;
vfs.fromUrl(data.uri, function(fnode){
if (!fnode) {
serving.ioError(cb, 'uri not found');
return;
}
if (data.resource === '*') { // special case, restore all
fnode.deletedItems = [];
serving.ioOk(cb);
notifyVfsChange(socket, fnode.getURI());
return;
}
if (!fnode.restoreDeleted(data.resource)) {
serving.ioError(cb, 'failed');
return;
}
fnode.createChildRelatively(data.resource, function(err, child){
if (serving.ioError(cb, err)) return;
serving.ioOk(cb, {item:child});
notifyVfsChange(socket, fnode.getURI());
});
});
},
'vfs.export': function onExport(data, cb){
serving.ioOk(cb, { data: vfs.toString() });
},
'vfs.import': function onImport(data, cb){
if (serving.ioError(cb, !data.root ? 'root' : null)) return;
var socket = this;
vfs.fromString(data.root, function(err){
if (serving.ioError(cb, err)) return;
serving.ioOk(cb);
notifyVfsChange(socket, vfs.root.getURI());
});
},
'info.get': function onInfo(data, cb){
serving.ioOk(cb, {
caseSensitiveFileNames: caseSensitiveFileNames,
frontEnd: log(GLOBAL.fileServer.listeningOn)
});
}
});
notifyVfsChange = function(socket, uri) {
dbg('vfs.changed '+uri);
var evt = 'vfs.changed', data={uri:uri||'/'};
require('./file-server').sockets.broadcast(evt, data);
socket.broadcast.emit(evt, data);
}; // notifyVfsChange