diff options
Diffstat (limited to 'src/files.js')
-rw-r--r-- | src/files.js | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/files.js b/src/files.js index 520127d..68dfea3 100644 --- a/src/files.js +++ b/src/files.js | |||
@@ -54,6 +54,13 @@ function copyFile(source, target, cb) { | |||
54 | }); | 54 | }); |
55 | } | 55 | } |
56 | 56 | ||
57 | function createDirectory(targetPath, callback) { | ||
58 | mkdirp(targetPath, function (error) { | ||
59 | if (error) return callback(error); | ||
60 | callback(null); | ||
61 | }); | ||
62 | } | ||
63 | |||
57 | function getAbsolutePath(filePath) { | 64 | function getAbsolutePath(filePath) { |
58 | var absoluteFilePath = path.resolve(path.join(gBasePath, filePath)); | 65 | var absoluteFilePath = path.resolve(path.join(gBasePath, filePath)); |
59 | 66 | ||
@@ -103,7 +110,8 @@ function get(req, res, next) { | |||
103 | function put(req, res, next) { | 110 | function put(req, res, next) { |
104 | var filePath = req.params[0]; | 111 | var filePath = req.params[0]; |
105 | 112 | ||
106 | if (!req.files.file) return next(new HttpError(400, 'missing file')); | 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')); | ||
107 | 115 | ||
108 | var absoluteFilePath = getAbsolutePath(filePath); | 116 | var absoluteFilePath = getAbsolutePath(filePath); |
109 | if (!absoluteFilePath) return next(new HttpError(403, 'Path not allowed')); | 117 | if (!absoluteFilePath) return next(new HttpError(403, 'Path not allowed')); |
@@ -111,10 +119,17 @@ function put(req, res, next) { | |||
111 | fs.stat(absoluteFilePath, function (error, result) { | 119 | fs.stat(absoluteFilePath, function (error, result) { |
112 | if (error && error.code !== 'ENOENT') return next(new HttpError(500, error)); | 120 | if (error && error.code !== 'ENOENT') return next(new HttpError(500, error)); |
113 | 121 | ||
114 | debug('put', absoluteFilePath, req.files.file); | 122 | debug('put', absoluteFilePath); |
115 | 123 | ||
124 | if (result && req.query.directory) return next(new HttpError(409, 'name already exists')); | ||
116 | if (result && result.isDirectory()) return next(new HttpError(409, 'cannot put on directories')); | 125 | if (result && result.isDirectory()) return next(new HttpError(409, 'cannot put on directories')); |
117 | if (!result || result.isFile()) { | 126 | |
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, {})); | ||
131 | }); | ||
132 | } else if (!result || result.isFile()) { | ||
118 | return copyFile(req.files.file.path, absoluteFilePath, function (error) { | 133 | return copyFile(req.files.file.path, absoluteFilePath, function (error) { |
119 | if (error) return next(new HttpError(500, error)); | 134 | if (error) return next(new HttpError(500, error)); |
120 | next(new HttpSuccess(201, {})); | 135 | next(new HttpSuccess(201, {})); |