-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
89 lines (74 loc) · 2.18 KB
/
app.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
const express = require('express'),
fs = require('fs'),
app = express.createServer(),
CONFIG = require('config'),
storage = CONFIG.server.storageDirectory,
crossorigin = CONFIG.server.crossorigin;
app.use(express.logger(CONFIG.logger))
.use(express.bodyParser())
.use(express.cookieParser())
.use(express.session(CONFIG.session))
.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", crossorigin);
res.header("Access-Control-Allow-Credentials", "true");
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT");
next();
});
require('express-browserid').plugAll(app);
app.get('/files', function(req, res) {
var email = req.session.email;
if (!email) {
res.json({ error: 'unauthorized' }, 403);
return;
}
fs.readdir(storage + email, function(err, files) {
if (!err) {
res.json({ error: 'okay', filenames: files });
return;
}
res.json({ error: 'okay', filenames: [] });
fs.mkdir(storage + email, function(err) {
if (err) {
fs.mkdir(storage, function(err) {
if (err) {
console.error('Something went horribly wrong!', err);
}
});
}
});
});
});
app.get('/files/:name', function(req, res) {
var email = req.session.email,
name = req.params.name;
if (!email) {
res.json({ error: 'unauthorized' }, 403);
return;
}
res.sendfile(storage + email + '/' + name, function(err) {
if (err) {
res.json({ error: 'file not found' }, 404);
}
});
});
app.put('/files/:name', function(req, res) {
var email = req.session.email,
name = req.params.name;
if (!email) {
res.json({ error: 'unauthorized' }, 403);
return;
}
fs.writeFile(storage + email + '/' + name, req.body.data, function(err) {
if (err) {
console.error('Something went horribly wrong!', err);
res.json({ error: err });
return;
}
res.json({ error: 'okay' });
});
});
app.listen(CONFIG.server.bindPort, CONFIG.server.bindIP, function() {
var addy = app.address();
console.log('Server started on http://' + addy.address + ':' + addy.port);
console.log('Press Ctrl+C to stop');
});