-
Notifications
You must be signed in to change notification settings - Fork 31
/
simple-api-server.js
230 lines (205 loc) · 6.73 KB
/
simple-api-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
'use strict';
const Path = require('path');
const PassThrough = require('stream').PassThrough;
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const Unifile = require('../lib/');
const app = express();
const unifile = new Unifile();
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({
secret: 'unifile',
resave: false,
saveUninitialized: false
}));
// Configure connectors
const ghconnector = new Unifile.GitHubConnector({
clientId: 'b4e46028bf36d871f68d',
clientSecret: 'c39806c4d0906cfeaac932012996a1919475cc78'
});
const dbxconnector = new Unifile.DropboxConnector({
clientId: '37mo489tld3rdi2',
clientSecret: 'kqfzd11vamre6xr',
redirectUri: 'http://localhost:6805/dropbox/oauth-callback'
});
const ftpconnector = new Unifile.FtpConnector({redirectUri: 'http://localhost:6805/ftp/signin'});
//const wdconnector = new Unifile.WebDavConnector({redirectUri: 'http://localhost:6805/webdav/signin'});
const rsconnector = new Unifile.RemoteStorageConnector({redirectUri: 'http://localhost:6805/remotestorage/callback'});
const fsconnector = new Unifile.FsConnector({showHiddenFile: true});
const sftpconnector = new Unifile.SftpConnector({redirectUri: 'http://localhost:6805/sftp/signin'});
// Register connectors
unifile.use(ghconnector);
unifile.use(dbxconnector);
unifile.use(ftpconnector);
unifile.use(rsconnector);
//unifile.use(wdconnector);
unifile.use(fsconnector);
unifile.use(sftpconnector);
// Expose connector methods
app.post('/:connector/authorize', function(req, res) {
if(req.body != null) {
if(req.session.unifile.remotestorage)
req.session.unifile.remotestorage.userAddress = req.body.userAddress;
else
req.session.unifile.remotestorage = req.body;
}
unifile.getAuthorizeURL(req.session.unifile, req.params.connector)
.catch((err) => {
console.error('Error while authorizing Unifile', err);
res.statusCode = 400;
res.end();
})
.then((result) => res.end(result));
});
// Search for a old session token in the cookies
app.get('/', function(req, res) {
// Init unifile session in Express
req.session.unifile = req.session.unifile || {};
let response;
if(req.cookies.unifile_github)
response = unifile.setAccessToken(req.session.unifile, 'github', req.cookies.unifile_github);
if(req.cookies.unifile_dropbox)
response = unifile.setAccessToken(req.session.unifile, 'dropbox', req.cookies.unifile_dropbox);
if(response)
response.then(() => res.sendFile(Path.join(__dirname, 'public', 'index.html')));
else res.sendFile(Path.join(__dirname, 'public', 'index.html'));
});
// List files and folders
app.get(/\/(.*)\/ls\/(.*)/, function(req, res) {
unifile.readdir(req.session.unifile, req.params[0], req.params[1])
.then(function(result) {
res.send(result);
})
.catch(function(err) {
console.error(err);
res.status(400).send(err);
});
});
app.put(/\/(.*)\/mkdir\/(.*)/, function(req, res) {
unifile.mkdir(req.session.unifile, req.params[0], req.params[1])
.then(function(result) {
res.send(result);
})
.catch(function(err) {
console.error(err);
res.status(400).send(err);
});
});
app.put(/\/(.*)\/put\/(.*)/, function(req, res) {
unifile.writeFile(req.session.unifile, req.params[0], req.params[1], req.body.content)
.then(function(result) {
res.send(result);
})
.catch(function(err) {
console.error(err);
res.status(400).send(err);
});
});
app.get(/\/(.*)\/get\/(.*)/, function(req, res) {
unifile.readFile(req.session.unifile, req.params[0], req.params[1])
.then(function(result) {
res.send(result);
})
.catch(function(err) {
console.error(err);
res.status(400).send(err);
});
});
app.patch(/\/(.*)\/mv\/(.*)/, function(req, res) {
unifile.rename(req.session.unifile, req.params[0], req.params[1], req.body.destination)
.then(function(result) {
res.send(result);
})
.catch(function(err) {
console.error(err);
res.status(400).send(err);
});
});
app.delete(/\/(.*)\/rm\/(.*)/, function(req, res) {
unifile.unlink(req.session.unifile, req.params[0], req.params[1])
.then(function(result) {
res.send(result);
})
.catch(function(err) {
console.error(err);
res.status(400).send(err);
});
});
app.delete(/\/(.*)\/rmdir\/(.*)/, function(req, res) {
unifile.rmdir(req.session.unifile, req.params[0], req.params[1])
.then(function(result) {
res.send(result);
})
.catch(function(err) {
console.error(err);
res.status(400).send(err);
});
});
app.post(/\/(.*)\/cp\/(.*)/, function(req, res) {
let stream = unifile.createReadStream(req.session.unifile, req.params[0], req.params[1]);
// Use PassThrough to prevent request from copying headers between requests
if(req.params[0] !== 'webdav' && req.params[0] !== 'fs') stream = stream.pipe(new PassThrough());
stream.pipe(unifile.createWriteStream(req.session.unifile, req.params[0], req.body.destination))
.pipe(res);
});
app.post(/\/(.*)\/batch\/(.*)/, function(req, res) {
const path = req.params[1];
const batch = [
{name: 'mkdir', path: path},
{name: 'writeFile', path: path + '/test.txt', content: 'Hello world'},
{name: 'writeFile', path: path + '/test2.txt', content: 'Hello world too'},
{name: 'rename', path: path + '/test.txt', destination: path + '/test_old.txt'},
{name: 'unlink', path: path + '/test2.txt'},
{name: 'rmdir', path: path}
];
unifile.batch(req.session.unifile, req.params[0], batch)
.then((result) => {
res.send(result);
})
.catch((err) => {
console.error(err);
res.status(400).send(err);
});
});
app.get(/\/(.*)\/stat\/(.*)/, function(req, res) {
unifile.stat(req.session.unifile, req.params[0], req.params[1])
.then((result) => {
res.send(result);
})
.catch((err) => {
res.status(400).send(err.message);
});
});
// register callback url
app.get('/:connector/oauth-callback', function(req, res) {
if('error' in req.query) {
res.status(500).send(req.query);
} else {
unifile.login(req.session.unifile, req.params.connector, req.query)
.then(function(result) {
res.cookie('unifile_' + req.params.connector, result);
res.end('<script>window.close();</script>');
})
.catch(function(err) {
console.error('ERROR', err);
res.status(500).send(err);
});
}
});
app.get('/remotestorage/callback', function(req, res) {
// Return a script that get the hash and redirect to oauth-callback
res.end('<script>' +
'var token = location.hash.substr(1).split("=")[1];location="/remotestorage/oauth-callback?token="+token' +
'</script>');
});
app.get('/:connector/signin', function(req, res) {
res.sendFile(Path.join(__dirname, 'public', req.params.connector + '_login.html'));
});
// server 'loop'
const port = process.env.PORT || 6805; // 6805 is the date of sexual revolution started in paris france 8-)
app.listen(port, function() {
console.log('Listening on ' + port);
});