-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
74 lines (62 loc) · 1.94 KB
/
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
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var config = require('./config');
var fs = require('fs');
var path = require('path');
var requestHelper = {};
var homeDirectory = config.homeDirectory || ".";
var requestHelper = require('./app/requestHelper')
app.use('/designer/', express.static(__dirname + '/public'));
io.set('transports', ['websocket', 'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling', 'polling']);
io.set('origins', '*:*');
io.sockets.on('connection', function(socket) {
requestHelper.listen(socket);
requestHelper.on('directory', function (request) {
var deferred = this;
fs.readdir(path.resolve(homeDirectory, '.' + request.path), function (err, files) {
var result = {
path: request.path,
name: '',
type: 'folder',
children: []
};
if (err) {
deferred.reject(err);
} else {
files.forEach(function (file) {
result.children.push({
path: path.resolve(request.path, file),
name: file,
type: 'file'
});
});
deferred.resolve(result);
}
});
});
requestHelper.on('load', function (request) {
var deferred = this;
fs.readFile(path.resolve(homeDirectory, '.' + request.path), function (err, data) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(new Buffer(data, 'binary').toString('base64'));
}
});
});
requestHelper.on('save', function (file) {
var deferred = this;
fs.writeFile(file.name, new Buffer(file.content, 'base64').toString('binary'), function (err) {
if (err) {
console.log('File could not be saved.');
deferred.reject(err);
} else {
console.log('File saved.');
deferred.resolve({ message: 'File saved.' });
};
});
});
});
server.listen(config.port);