]>
git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - src/files.js
3 var async
= require('async'),
5 path
= require('path'),
7 debug
= require('debug')('files'),
8 mkdirp
= require('mkdirp'),
9 HttpError
= require('connect-lastmile').HttpError
,
10 HttpSuccess
= require('connect-lastmile').HttpSuccess
;
14 exports
= module
.exports = function (basePath
) {
24 // http://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js
25 function copyFile(source
, target
, cb
) {
29 mkdirp(path
.dirname(target
), function (error
) {
30 if (error
) return cb(error
);
32 var rd
= fs
.createReadStream(source
);
33 rd
.on("error", function(err
) {
37 var wr
= fs
.createWriteStream(target
);
38 wr
.on("error", function(err
) {
42 wr
.on("close", function(ex
) {
57 function getAbsolutePath(filePath
) {
58 var absoluteFilePath
= path
.resolve(path
.join(gBasePath
, filePath
));
60 if (absoluteFilePath
.indexOf(gBasePath
) !== 0) return null;
61 return absoluteFilePath
;
64 function removeBasePath(filePath
) {
65 return filePath
.slice(gBasePath
.length
);
68 function 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'));
73 fs
.stat(absoluteFilePath
, function (error
, result
) {
74 if (error
) return next(new HttpError(404, error
));
76 debug('get', absoluteFilePath
);
78 if (!result
.isDirectory() && !result
.isFile()) return next(new HttpError(500, 'unsupported type'));
79 if (result
.isFile()) return res
.sendFile(absoluteFilePath
);
81 async
.map(fs
.readdirSync(absoluteFilePath
), function (filePath
, callback
) {
82 fs
.stat(path
.join(absoluteFilePath
, filePath
), function (error
, result
) {
83 if (error
) return callback(error
);
86 isDirectory: result
.isDirectory(),
87 isFile: result
.isFile(),
91 birthtime: result
.birthtime
,
96 }, function (error
, results
) {
97 if (error
) return next(new HttpError(500, error
));
98 res
.status(222).send({ entries: results
});
103 function put(req
, res
, next
) {
104 var filePath
= req
.params
[0];
106 if (!req
.files
.file
) return next(new HttpError(400, 'missing file'));
108 var absoluteFilePath
= getAbsolutePath(filePath
);
109 if (!absoluteFilePath
) return next(new HttpError(403, 'Path not allowed'));
111 fs
.stat(absoluteFilePath
, function (error
, result
) {
112 if (error
&& error
.code
!== 'ENOENT') return next(new HttpError(500, error
));
114 debug('put', absoluteFilePath
, req
.files
.file
);
116 if (result
&& result
.isDirectory()) return next(new HttpError(409, 'cannot put on directories'));
117 if (!result
|| result
.isFile()) {
118 return copyFile(req
.files
.file
.path
, absoluteFilePath
, function (error
) {
119 if (error
) return next(new HttpError(500, error
));
120 next(new HttpSuccess(201, {}));
124 return next(new HttpError(500, 'unsupported type'));
128 function del(req
, res
, next
) {
129 var filePath
= req
.params
[0];
130 var recursive
= !!req
.query
.recursive
;
131 var dryRun
= !!req
.query
.dryRun
;
133 var absoluteFilePath
= getAbsolutePath(filePath
);
134 if (!absoluteFilePath
) return next(new HttpError(404, 'Not found'));
136 // absoltueFilePath has to have the base path prepended
137 if (absoluteFilePath
.length
<= gBasePath
.length
) return next(new HttpError(404, 'Not found'));
139 fs
.stat(absoluteFilePath
, function (error
, result
) {
140 if (error
) return next(new HttpError(404, error
));
142 if (result
.isDirectory() && !recursive
) return next(new HttpError(403, 'Is directory'));
144 // add globs to get file listing
145 if (result
.isDirectory()) absoluteFilePath
+= '/**';
147 rm(absoluteFilePath
, { dryRun: dryRun
}).then(function (result
) {
148 result
= result
.map(removeBasePath
);
149 next(new HttpSuccess(200, { entries: result
}));
150 }, function (error
) {
151 console
.error(error
);
152 next(new HttpError(500, 'Unable to remove'));