diff options
author | Johannes Zellner <johannes@nebulon.de> | 2016-03-01 16:37:05 +0100 |
---|---|---|
committer | Johannes Zellner <johannes@nebulon.de> | 2016-03-01 16:37:05 +0100 |
commit | 403359cf6986b9c0f8c69f144cd36974d61a2370 (patch) | |
tree | 89ba9b26ff89afc617c89603b40f7b2cbf552c61 /src | |
parent | 61e0b8e4910051d0dfb4d454817b9f0511ba96ad (diff) | |
download | Surfer-403359cf6986b9c0f8c69f144cd36974d61a2370.tar.gz Surfer-403359cf6986b9c0f8c69f144cd36974d61a2370.tar.zst Surfer-403359cf6986b9c0f8c69f144cd36974d61a2370.zip |
Add ui to create directories
Diffstat (limited to 'src')
-rw-r--r-- | src/files.js | 21 | ||||
-rw-r--r-- | src/multipart.js | 2 |
2 files changed, 19 insertions, 4 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, {})); |
diff --git a/src/multipart.js b/src/multipart.js index 7b994cc..ec72506 100644 --- a/src/multipart.js +++ b/src/multipart.js | |||
@@ -12,7 +12,7 @@ function _mime(req) { | |||
12 | 12 | ||
13 | module.exports = function multipart(options) { | 13 | module.exports = function multipart(options) { |
14 | return function (req, res, next) { | 14 | return function (req, res, next) { |
15 | if (_mime(req) !== 'multipart/form-data') return res.status(400).send('Invalid content-type. Expecting multipart'); | 15 | if (_mime(req) !== 'multipart/form-data') return next(null); |
16 | 16 | ||
17 | var form = new multiparty.Form({ | 17 | var form = new multiparty.Form({ |
18 | uploadDir: '/tmp', | 18 | uploadDir: '/tmp', |