diff options
Diffstat (limited to 'src/files.js')
-rw-r--r-- | src/files.js | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/src/files.js b/src/files.js index 3812d21..48f91a8 100644 --- a/src/files.js +++ b/src/files.js | |||
@@ -4,17 +4,22 @@ var fs = require('fs'), | |||
4 | path = require('path'), | 4 | path = require('path'), |
5 | ejs = require('ejs'), | 5 | ejs = require('ejs'), |
6 | rimraf = require('rimraf'), | 6 | rimraf = require('rimraf'), |
7 | debug = require('debug')('files'), | ||
7 | mkdirp = require('mkdirp'), | 8 | mkdirp = require('mkdirp'), |
8 | HttpError = require('connect-lastmile').HttpError, | 9 | HttpError = require('connect-lastmile').HttpError, |
9 | HttpSuccess = require('connect-lastmile').HttpSuccess; | 10 | HttpSuccess = require('connect-lastmile').HttpSuccess; |
10 | 11 | ||
11 | exports = module.exports = { | 12 | var gBasePath; |
12 | get: get, | 13 | |
13 | put: put, | 14 | exports = module.exports = function (basePath) { |
14 | del: del | 15 | gBasePath = basePath; |
15 | }; | ||
16 | 16 | ||
17 | var FILE_BASE = path.resolve(__dirname, '../files'); | 17 | return { |
18 | get: get, | ||
19 | put: put, | ||
20 | del: del | ||
21 | }; | ||
22 | }; | ||
18 | 23 | ||
19 | // http://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js | 24 | // http://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js |
20 | function copyFile(source, target, cb) { | 25 | function copyFile(source, target, cb) { |
@@ -54,9 +59,9 @@ function render(view, options) { | |||
54 | } | 59 | } |
55 | 60 | ||
56 | function getAbsolutePath(filePath) { | 61 | function getAbsolutePath(filePath) { |
57 | var absoluteFilePath = path.resolve(FILE_BASE, filePath); | 62 | var absoluteFilePath = path.resolve(gBasePath, filePath); |
58 | 63 | ||
59 | if (absoluteFilePath.indexOf(FILE_BASE) !== 0) return null; | 64 | if (absoluteFilePath.indexOf(gBasePath) !== 0) return null; |
60 | return absoluteFilePath; | 65 | return absoluteFilePath; |
61 | } | 66 | } |
62 | 67 | ||
@@ -68,7 +73,7 @@ function get(req, res, next) { | |||
68 | fs.stat(absoluteFilePath, function (error, result) { | 73 | fs.stat(absoluteFilePath, function (error, result) { |
69 | if (error) return next(new HttpError(404, error)); | 74 | if (error) return next(new HttpError(404, error)); |
70 | 75 | ||
71 | console.log('get', absoluteFilePath); | 76 | debug('get', absoluteFilePath); |
72 | 77 | ||
73 | if (result.isFile()) return res.sendfile(absoluteFilePath); | 78 | if (result.isFile()) return res.sendfile(absoluteFilePath); |
74 | if (result.isDirectory()) return res.status(200).send({ entries: fs.readdirSync(absoluteFilePath) }); | 79 | if (result.isDirectory()) return res.status(200).send({ entries: fs.readdirSync(absoluteFilePath) }); |
@@ -88,7 +93,7 @@ function put(req, res, next) { | |||
88 | fs.stat(absoluteFilePath, function (error, result) { | 93 | fs.stat(absoluteFilePath, function (error, result) { |
89 | if (error && error.code !== 'ENOENT') return next(new HttpError(500, error)); | 94 | if (error && error.code !== 'ENOENT') return next(new HttpError(500, error)); |
90 | 95 | ||
91 | console.log('put', absoluteFilePath, req.files.file); | 96 | debug('put', absoluteFilePath, req.files.file); |
92 | 97 | ||
93 | if (result && result.isDirectory()) return next(new HttpError(409, 'cannot put on directories')); | 98 | if (result && result.isDirectory()) return next(new HttpError(409, 'cannot put on directories')); |
94 | if (!result || result.isFile()) { | 99 | if (!result || result.isFile()) { |
@@ -106,7 +111,7 @@ function del(req, res, next) { | |||
106 | var filePath = req.params[0]; | 111 | var filePath = req.params[0]; |
107 | var absoluteFilePath = getAbsolutePath(filePath); | 112 | var absoluteFilePath = getAbsolutePath(filePath); |
108 | if (!absoluteFilePath) return next(new HttpError(404, 'Not found')); | 113 | if (!absoluteFilePath) return next(new HttpError(404, 'Not found')); |
109 | if (absoluteFilePath.slice(FILE_BASE.length) === '') return next(new HttpError(403, 'Forbidden')); | 114 | if (absoluteFilePath.slice(gBasePath.length) === '') return next(new HttpError(403, 'Forbidden')); |
110 | 115 | ||
111 | fs.stat(absoluteFilePath, function (error, result) { | 116 | fs.stat(absoluteFilePath, function (error, result) { |
112 | if (error) return next(new HttpError(404, error)); | 117 | if (error) return next(new HttpError(404, error)); |