]>
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 createDirectory(targetPath
, callback
) {
58 mkdirp(targetPath
, function (error
) {
59 if (error
) return callback(error
);
64 function getAbsolutePath(filePath
) {
65 var absoluteFilePath
= path
.resolve(path
.join(gBasePath
, filePath
));
67 if (absoluteFilePath
.indexOf(gBasePath
) !== 0) return null;
68 return absoluteFilePath
;
71 function removeBasePath(filePath
) {
72 return filePath
.slice(gBasePath
.length
);
75 function get(req
, res
, next
) {
76 var filePath
= req
.params
[0];
77 var absoluteFilePath
= getAbsolutePath(filePath
);
78 if (!absoluteFilePath
) return next(new HttpError(403, 'Path not allowed'));
80 fs
.stat(absoluteFilePath
, function (error
, result
) {
81 if (error
) return next(new HttpError(404, error
));
83 debug('get', absoluteFilePath
);
85 if (!result
.isDirectory() && !result
.isFile()) return next(new HttpError(500, 'unsupported type'));
86 if (result
.isFile()) return res
.sendFile(absoluteFilePath
);
88 async
.map(fs
.readdirSync(absoluteFilePath
), function (filePath
, callback
) {
89 fs
.stat(path
.join(absoluteFilePath
, filePath
), function (error
, result
) {
90 if (error
) return callback(error
);
93 isDirectory: result
.isDirectory(),
94 isFile: result
.isFile(),
98 birthtime: result
.birthtime
,
103 }, function (error
, results
) {
104 if (error
) return next(new HttpError(500, error
));
105 res
.status(222).send({ entries: results
});
110 function put(req
, res
, next
) {
111 var filePath
= req
.params
[0];
113 if (!(req
.files
&& req
.files
.file
) && !req
.query
.directory
) return next(new HttpError(400, 'missing file or directory'));
114 if ((req
.files
&& req
.files
.file
) && req
.query
.directory
) return next(new HttpError(400, 'either file or directory'));
116 var absoluteFilePath
= getAbsolutePath(filePath
);
117 if (!absoluteFilePath
) return next(new HttpError(403, 'Path not allowed'));
119 fs
.stat(absoluteFilePath
, function (error
, result
) {
120 if (error
&& error
.code
!== 'ENOENT') return next(new HttpError(500, error
));
122 debug('put', absoluteFilePath
);
124 if (result
&& req
.query
.directory
) return next(new HttpError(409, 'name already exists'));
125 if (result
&& result
.isDirectory()) return next(new HttpError(409, 'cannot put on directories'));
127 if (req
.query
.directory
) {
128 return createDirectory(absoluteFilePath
, function (error
) {
129 if (error
) return next(new HttpError(500, error
));
130 next(new HttpSuccess(201, {}));
132 } else if (!result
|| result
.isFile()) {
133 return copyFile(req
.files
.file
.path
, absoluteFilePath
, function (error
) {
134 if (error
) return next(new HttpError(500, error
));
135 next(new HttpSuccess(201, {}));
139 return next(new HttpError(500, 'unsupported type'));
143 function del(req
, res
, next
) {
144 var filePath
= req
.params
[0];
145 var recursive
= !!req
.query
.recursive
;
146 var dryRun
= !!req
.query
.dryRun
;
148 var absoluteFilePath
= getAbsolutePath(filePath
);
149 if (!absoluteFilePath
) return next(new HttpError(404, 'Not found'));
151 // absoltueFilePath has to have the base path prepended
152 if (absoluteFilePath
.length
<= gBasePath
.length
) return next(new HttpError(404, 'Not found'));
154 fs
.stat(absoluteFilePath
, function (error
, result
) {
155 if (error
) return next(new HttpError(404, error
));
157 if (result
.isDirectory() && !recursive
) return next(new HttpError(403, 'Is directory'));
159 // add globs to get file listing
160 if (result
.isDirectory()) absoluteFilePath
+= '/**';
162 rm(absoluteFilePath
, { dryRun: dryRun
}).then(function (result
) {
163 result
= result
.map(removeBasePath
);
164 next(new HttpSuccess(200, { entries: result
}));
165 }, function (error
) {
166 console
.error(error
);
167 next(new HttpError(500, 'Unable to remove'));