]>
Commit | Line | Data |
---|---|---|
ca2d3b4d JZ |
1 | 'use strict'; |
2 | ||
13080980 JZ |
3 | var async = require('async'), |
4 | fs = require('fs'), | |
ca2d3b4d | 5 | path = require('path'), |
eb83e4da | 6 | rm = require('del'), |
8c3ae071 | 7 | debug = require('debug')('files'), |
eaa62184 | 8 | mkdirp = require('mkdirp'), |
ca2d3b4d JZ |
9 | HttpError = require('connect-lastmile').HttpError, |
10 | HttpSuccess = require('connect-lastmile').HttpSuccess; | |
11 | ||
8c3ae071 JZ |
12 | var gBasePath; |
13 | ||
14 | exports = module.exports = function (basePath) { | |
15 | gBasePath = basePath; | |
ca2d3b4d | 16 | |
8c3ae071 JZ |
17 | return { |
18 | get: get, | |
19 | put: put, | |
20 | del: del | |
21 | }; | |
22 | }; | |
ca2d3b4d JZ |
23 | |
24 | // http://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js | |
25 | function copyFile(source, target, cb) { | |
26 | var cbCalled = false; | |
27 | ||
eaa62184 JZ |
28 | // ensure directory |
29 | mkdirp(path.dirname(target), function (error) { | |
30 | if (error) return cb(error); | |
ca2d3b4d | 31 | |
eaa62184 JZ |
32 | var rd = fs.createReadStream(source); |
33 | rd.on("error", function(err) { | |
34 | done(err); | |
35 | }); | |
ca2d3b4d | 36 | |
eaa62184 JZ |
37 | var wr = fs.createWriteStream(target); |
38 | wr.on("error", function(err) { | |
39 | done(err); | |
40 | }); | |
ca2d3b4d | 41 | |
eaa62184 JZ |
42 | wr.on("close", function(ex) { |
43 | done(); | |
44 | }); | |
45 | ||
46 | rd.pipe(wr); | |
ca2d3b4d | 47 | |
eaa62184 JZ |
48 | function done(err) { |
49 | if (!cbCalled) { | |
50 | cb(err); | |
51 | cbCalled = true; | |
52 | } | |
ca2d3b4d | 53 | } |
eaa62184 | 54 | }); |
ca2d3b4d JZ |
55 | } |
56 | ||
ca2d3b4d | 57 | function getAbsolutePath(filePath) { |
7bb99aff | 58 | var absoluteFilePath = path.resolve(path.join(gBasePath, filePath)); |
ca2d3b4d | 59 | |
8c3ae071 | 60 | if (absoluteFilePath.indexOf(gBasePath) !== 0) return null; |
ca2d3b4d JZ |
61 | return absoluteFilePath; |
62 | } | |
63 | ||
898fe7c8 JZ |
64 | function removeBasePath(filePath) { |
65 | return filePath.slice(gBasePath.length); | |
66 | } | |
67 | ||
ca2d3b4d JZ |
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')); | |
72 | ||
73 | fs.stat(absoluteFilePath, function (error, result) { | |
74 | if (error) return next(new HttpError(404, error)); | |
75 | ||
8c3ae071 | 76 | debug('get', absoluteFilePath); |
ca2d3b4d | 77 | |
13080980 | 78 | if (!result.isDirectory() && !result.isFile()) return next(new HttpError(500, 'unsupported type')); |
cfe24a27 | 79 | if (result.isFile()) return res.sendFile(absoluteFilePath); |
ca2d3b4d | 80 | |
13080980 JZ |
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); | |
84 | ||
85 | callback(null, { | |
86 | isDirectory: result.isDirectory(), | |
87 | isFile: result.isFile(), | |
88 | atime: result.atime, | |
89 | mtime: result.mtime, | |
90 | ctime: result.ctime, | |
91 | birthtime: result.birthtime, | |
92 | size: result.size, | |
93 | filePath: filePath | |
94 | }); | |
95 | }); | |
96 | }, function (error, results) { | |
97 | if (error) return next(new HttpError(500, error)); | |
98 | res.status(222).send({ entries: results }); | |
99 | }); | |
ca2d3b4d JZ |
100 | }); |
101 | } | |
102 | ||
103 | function put(req, res, next) { | |
104 | var filePath = req.params[0]; | |
105 | ||
106 | if (!req.files.file) return next(new HttpError(400, 'missing file')); | |
107 | ||
108 | var absoluteFilePath = getAbsolutePath(filePath); | |
109 | if (!absoluteFilePath) return next(new HttpError(403, 'Path not allowed')); | |
110 | ||
111 | fs.stat(absoluteFilePath, function (error, result) { | |
112 | if (error && error.code !== 'ENOENT') return next(new HttpError(500, error)); | |
113 | ||
8c3ae071 | 114 | debug('put', absoluteFilePath, req.files.file); |
ca2d3b4d JZ |
115 | |
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, {})); | |
121 | }); | |
122 | } | |
123 | ||
124 | return next(new HttpError(500, 'unsupported type')); | |
125 | }); | |
126 | } | |
127 | ||
128 | function del(req, res, next) { | |
129 | var filePath = req.params[0]; | |
898fe7c8 JZ |
130 | var recursive = !!req.query.recursive; |
131 | var dryRun = !!req.query.dryRun; | |
132 | ||
ca2d3b4d | 133 | var absoluteFilePath = getAbsolutePath(filePath); |
eaa62184 | 134 | if (!absoluteFilePath) return next(new HttpError(404, 'Not found')); |
d755925f JZ |
135 | |
136 | // absoltueFilePath has to have the base path prepended | |
898fe7c8 | 137 | if (absoluteFilePath.length <= gBasePath.length) return next(new HttpError(404, 'Not found')); |
ca2d3b4d JZ |
138 | |
139 | fs.stat(absoluteFilePath, function (error, result) { | |
140 | if (error) return next(new HttpError(404, error)); | |
141 | ||
898fe7c8 JZ |
142 | if (result.isDirectory() && !recursive) return next(new HttpError(403, 'Is directory')); |
143 | ||
144 | // add globs to get file listing | |
145 | if (result.isDirectory()) absoluteFilePath += '/**'; | |
146 | ||
147 | rm(absoluteFilePath, { dryRun: dryRun }).then(function (result) { | |
148 | result = result.map(removeBasePath); | |
eb83e4da | 149 | next(new HttpSuccess(200, { entries: result })); |
898fe7c8 JZ |
150 | }, function (error) { |
151 | console.error(error); | |
152 | next(new HttpError(500, 'Unable to remove')); | |
ca2d3b4d JZ |
153 | }); |
154 | }); | |
155 | } |