-
Notifications
You must be signed in to change notification settings - Fork 16
/
fileserver.js
367 lines (327 loc) · 8.93 KB
/
fileserver.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// fileserver
var bodyParser = require('body-parser');
var fileDriver = require('./fsDriver.js');
var url = require('url');
var mime = require('mime');
var path = require('path');
var mw = require('dat-middleware');
var flow = require('middleware-flow');
var morgan = require('morgan');
var error = require('debug')('rest-fs:fileserver');
var fileserver = function(app) {
if (!app) {
throw new Error('express app required');
}
app.set('etag', 'strong');
app.use(require('express-domain-middleware'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(morgan('combined', {
skip: function () { return process.env.LOG !== 'true'; }
}));
app.get(/^\/(.+\/)?$/, getDir);
app.get(/^\/.+[^\/]$/, getFile);
app.post("/*", postFileOrDir);
app.put("/*", putFileOrDir);
app.delete(/^\/.+\/$/, delDir);
app.delete(/^\/.+[^\/]$/, delFile);
app.use(function (err, req, res, next) {
error('uncaught error', err.stack);
var outErr = {
message: err.message,
stack: err.stack
};
res.status(500).send(outErr);
});
return app;
};
/* GET
/path/to/dir/
list contents of directory
*optional*
?recursive = list recursively default false
return: list of files/dirs
res.body = [
{
"fullFilePath"
}, ...
]
*/
var getDir = function (req, res, next) {
if (req.query.stat) {
return statFile(req, res, next);
}
var dirPath = decodeURI(url.parse(req.url).pathname);
var isRecursive = req.query.recursive || "false";
var opts = req.body.opts;
var handList = function (err, files) {
if (err && err.code === 'ENOTDIR') {
// this this is a file, redirect to file path
var originalUrl = url.parse(req.originalUrl);
originalUrl.pathname = originalUrl.pathname.substr(0, originalUrl.pathname.length - 1);
var target = url.format(originalUrl);
res.statusCode = 303;
res.setHeader('Location', target);
return res.end('Redirecting to ' + target);
}
if (files) {
for (var i = files.length - 1; i >= 0; i--) {
files[i] = formatOutData(req, files[i]);
}
}
sendCode(200, req, res, next, files)(err);
};
if (isRecursive === "true") {
return fileDriver.listAll({
dirPath: dirPath,
opts: opts
}, handList);
} else {
return fileDriver.list({
dirPath: dirPath,
opts: opts
}, handList);
}
};
/* GET
/path/to/file
return contents of file
if dir, redirect to dir path
*optional*
?encoding = default utf8
return: data of file
res.body = {"content of specified file"}
*/
var getFile = function (req, res, next) {
if (req.query.stat) {
return statFile(req, res, next);
}
var filePath = decodeURI(url.parse(req.url).pathname);
var encoding = req.query.encoding || 'utf8';
var opts = req.body.opts;
fileDriver.readFile({
filePath: filePath,
encoding: encoding,
opts: opts
}, function(err, data) {
if (err && err.code === 'EISDIR') {
// this this is a dir, redirect to dir path
var originalUrl = url.parse(req.originalUrl);
originalUrl.pathname += '/';
var target = url.format(originalUrl);
res.statusCode = 303;
res.setHeader('Location', target);
return res.end('Redirecting to ' + target);
}
res.set('content-type', mime.lookup(filePath));
sendCode(200, req, res, next, data)(err);
});
};
/* POST
/path/to/file/or/dir
creates or overwrites file
creates dir if it does not exisit.
renames or moves file if newPath exists
*optional*
body.newPath = if exist, move/rename file to this location.
body.clobber = if true will overwrite dest files (default false)
body.mkdirp = if true will create path to new location (default false)
body.mode = permissons of file (defaults: file 438(0666) dir 511(0777))
body.encoding = default utf8
returns: modified resource
res.body = {
"fullFilePath" or dir
}
*/
var postFileOrDir = function (req, res, next) {
var dirPath = decodeURI(url.parse(req.url).pathname);
var isDir = dirPath.substr(-1) === '/';
var options = {};
var isJson = false;
var opts = req.body.opts;
if (typeof req.headers['content-type'] === 'string') {
isJson = ~req.headers['content-type'].indexOf('application/json') === -1 ? true : false;
}
// move/rename if newPath exists
if (req.body.newPath) {
options.clobber = req.body.clobber || false;
options.mkdirp = req.body.mkdirp || false;
var newPath = req.body.newPath;
if (isDir && newPath.substr(-1) !== '/') {
newPath = newPath + '/';
}
return fileDriver.move({
dirPath: dirPath,
newPath: newPath,
options: options,
opts: opts
}, sendCode(200, req, res, next, formatOutData(req, newPath)));
}
if (isDir) {
var mode = req.body.mode || 511;
return fileDriver.mkdir({
dirPath: dirPath,
mode: mode,
opts: opts
}, sendCode(201, req, res, next, formatOutData(req, dirPath)));
}
if (!isJson) {
// default is to not clobber
options.encoding = req.query.encoding || 'utf8';
options.mode = req.query.mode || 438;
options.flags = req.query.clobber === 'true' ? 'w' : 'wx';
return fileDriver.writeFileStream({
dirPath: dirPath,
stream: req,
options: options,
opts: opts
}, sendCode(201, req, res, next, formatOutData(req, dirPath)));
}
options.encoding = req.body.encoding || 'utf8';
options.mode = req.body.mode || 438;
var data = req.body.content || '';
fileDriver.writeFile({
dirPath: dirPath,
data: data,
options: options,
opts: opts
}, sendCode(201, req, res, next, formatOutData(req, dirPath)));
};
/* PUT
/path/to/file/or/dir
make file or dir
*optional*
body.mode = permissons of file (438 default 0666 octal)
body.encoding = default utf8
returns: modified resource
res.body = {
"fullFilePath"
}
*/
var putFileOrDir = function (req, res, next) {
var dirPath = decodeURI(url.parse(req.url).pathname);
var isDir = dirPath.substr(-1) === '/';
var options = {};
var opts = req.body.opts;
if (isDir) {
var mode = req.body.mode || 511;
fileDriver.mkdir({
dirPath: dirPath,
mode: mode,
opts: opts
}, sendCode(201, req, res, next, formatOutData(req, dirPath)));
} else {
options.encoding = req.body.encoding || 'utf8';
options.mode = req.body.mode || 438;
var data = req.body.content || '';
fileDriver.writeFile({
dirPath: dirPath,
data: data,
options: options,
opts: opts
}, sendCode(201, req, res, next, formatOutData(req, dirPath)));
}
};
/* DEL
/path/to/dir/
deletes dir
*optional*
body.clobber = will remove non-empty dir (defaut: false)
return:
res.body = {}
*/
var delDir = function (req, res, next) {
var dirPath = decodeURI(url.parse(req.url).pathname);
var clobber = req.body.clobber || false;
var opts = req.body.opts;
fileDriver.rmdir({
dirPath: dirPath,
clobber: clobber,
opts: opts
}, sendCode(200, req, res, next, {}));
};
/* DEL
/path/to/file
deletes file
return:
res.body = {}
*/
var delFile = function (req, res, next) {
var dirPath = decodeURI(url.parse(req.url).pathname);
var opts = req.body.opts;
fileDriver.unlink({
dirPath: dirPath,
opts: opts
}, sendCode(200, req, res, next, {}));
};
/* GET
/path/to/dir/or/file
Returns stats for a file
return: stats for the file as determined by node
res.body = {
dev: 16777220,
mode: 16877,
nlink: 31,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 604862,
size: 1054,
blocks: 0,
atime: Thu Mar 05 2015 11:38:47 GMT-0800 (PST),
mtime: Thu Mar 05 2015 10:52:41 GMT-0800 (PST),
ctime: Thu Mar 05 2015 10:52:41 GMT-0800 (PST),
birthtime: Mon Mar 02 2015 10:55:37 GMT-0800 (PST)
}
*/
var statFile = function (req, res, next) {
var filePath = decodeURI(url.parse(req.url).pathname);
var opts = req.body.opts;
fileDriver.stat({
filePath: filePath,
opts: opts
}, function(err, stats) {
sendCode(200, req, res, next, stats)(err);
});
};
// Helpers
// formats out data based on client spec.
var formatOutData = function (req, filepath) {
var out = filepath;
if (typeof req.modifyOut === 'function') {
out = req.modifyOut(out);
}
return out;
};
var sendCode = function(code, req, res, next, out) {
return function (err) {
if (err) {
error('ERROR', req.url, err);
code = 500;
out = {
errno: err.errno,
code: err.code,
path: err.path,
message: err.message,
stack: err.stack
};
if (err.code === 'ENOENT') {
code = 404;
} if (err.code === 'EPERM') {
code = 403;
} if (err.code === 'ENOTDIR' ||
err.code === 'EISDIR') {
code = 400;
} if (err.code === 'ENOTEMPTY' ||
err.code === 'EEXIST' ||
err.code === 'EINVAL') {
code = 409;
}
}
res.status(code).send(out);
};
};
module.exports = fileserver;