]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blame_incremental - src/files.js
Add ldap auth
[perso/Immae/Projets/Nodejs/Surfer.git] / src / files.js
... / ...
CommitLineData
1'use strict';
2
3var fs = require('fs'),
4 path = require('path'),
5 ejs = require('ejs'),
6 rm = require('del'),
7 debug = require('debug')('files'),
8 mkdirp = require('mkdirp'),
9 HttpError = require('connect-lastmile').HttpError,
10 HttpSuccess = require('connect-lastmile').HttpSuccess;
11
12var gBasePath;
13
14exports = module.exports = function (basePath) {
15 gBasePath = basePath;
16
17 return {
18 get: get,
19 put: put,
20 del: del
21 };
22};
23
24// http://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js
25function copyFile(source, target, cb) {
26 var cbCalled = false;
27
28 // ensure directory
29 mkdirp(path.dirname(target), function (error) {
30 if (error) return cb(error);
31
32 var rd = fs.createReadStream(source);
33 rd.on("error", function(err) {
34 done(err);
35 });
36
37 var wr = fs.createWriteStream(target);
38 wr.on("error", function(err) {
39 done(err);
40 });
41
42 wr.on("close", function(ex) {
43 done();
44 });
45
46 rd.pipe(wr);
47
48 function done(err) {
49 if (!cbCalled) {
50 cb(err);
51 cbCalled = true;
52 }
53 }
54 });
55}
56
57function render(view, options) {
58 return ejs.render(fs.readFileSync(view, 'utf8'), options);
59}
60
61function getAbsolutePath(filePath) {
62 var absoluteFilePath = path.resolve(gBasePath, filePath);
63
64 if (absoluteFilePath.indexOf(gBasePath) !== 0) return null;
65 return absoluteFilePath;
66}
67
68function get(req, res, next) {
69 var filePath = req.params[0];
70 var absoluteFilePath = getAbsolutePath(filePath);
71 if (!absoluteFilePath) return next(new HttpError(403, 'Path not allowed'));
72
73 fs.stat(absoluteFilePath, function (error, result) {
74 if (error) return next(new HttpError(404, error));
75
76 debug('get', absoluteFilePath);
77
78 if (result.isFile()) return res.sendfile(absoluteFilePath);
79 if (result.isDirectory()) return res.status(200).send({ entries: fs.readdirSync(absoluteFilePath) });
80
81 return next(new HttpError(500, 'unsupported type'));
82 });
83}
84
85function put(req, res, next) {
86 var filePath = req.params[0];
87
88 if (!req.files.file) return next(new HttpError(400, 'missing file'));
89
90 var absoluteFilePath = getAbsolutePath(filePath);
91 if (!absoluteFilePath) return next(new HttpError(403, 'Path not allowed'));
92
93 fs.stat(absoluteFilePath, function (error, result) {
94 if (error && error.code !== 'ENOENT') return next(new HttpError(500, error));
95
96 debug('put', absoluteFilePath, req.files.file);
97
98 if (result && result.isDirectory()) return next(new HttpError(409, 'cannot put on directories'));
99 if (!result || result.isFile()) {
100 return copyFile(req.files.file.path, absoluteFilePath, function (error) {
101 if (error) return next(new HttpError(500, error));
102 next(new HttpSuccess(201, {}));
103 });
104 }
105
106 return next(new HttpError(500, 'unsupported type'));
107 });
108}
109
110function del(req, res, next) {
111 var filePath = req.params[0];
112 var absoluteFilePath = getAbsolutePath(filePath);
113 if (!absoluteFilePath) return next(new HttpError(404, 'Not found'));
114 if (absoluteFilePath.slice(gBasePath.length) === '') return next(new HttpError(403, 'Forbidden'));
115
116 fs.stat(absoluteFilePath, function (error, result) {
117 if (error) return next(new HttpError(404, error));
118
119 rm(absoluteFilePath, function (error, result) {
120 if (error) return next(new HttpError(500, 'Unable to remove'));
121 next(new HttpSuccess(200, { entries: result }));
122 });
123 });
124}