diff options
author | Johannes Zellner <johannes@cloudron.io> | 2017-02-07 16:26:17 +0100 |
---|---|---|
committer | Johannes Zellner <johannes@cloudron.io> | 2017-02-07 16:26:19 +0100 |
commit | e628921a338684a4bc3f196c5c39beba8b8f9b68 (patch) | |
tree | a4112c316e69b4d0a302af23f5bb8f4396c15c30 /src | |
parent | 4b6cf0add4f4f89671f4553a9672a01fbb485df1 (diff) | |
download | Surfer-e628921a338684a4bc3f196c5c39beba8b8f9b68.tar.gz Surfer-e628921a338684a4bc3f196c5c39beba8b8f9b68.tar.zst Surfer-e628921a338684a4bc3f196c5c39beba8b8f9b68.zip |
Add rename functionality
This also break backwardscompat since PUT is now POST
and PUT is used for renaming
Diffstat (limited to 'src')
-rw-r--r-- | src/files.js | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/src/files.js b/src/files.js index 747acf7..876ff4e 100644 --- a/src/files.js +++ b/src/files.js | |||
@@ -17,6 +17,7 @@ exports = module.exports = function (basePath) { | |||
17 | return { | 17 | return { |
18 | get: get, | 18 | get: get, |
19 | put: put, | 19 | put: put, |
20 | post: post, | ||
20 | del: del | 21 | del: del |
21 | }; | 22 | }; |
22 | }; | 23 | }; |
@@ -111,22 +112,22 @@ function get(req, res, next) { | |||
111 | }); | 112 | }); |
112 | } | 113 | } |
113 | 114 | ||
114 | function put(req, res, next) { | 115 | function post(req, res, next) { |
115 | var filePath = decodeURIComponent(req.params[0]); | 116 | var filePath = decodeURIComponent(req.params[0]); |
116 | 117 | ||
117 | if (!(req.files && req.files.file) && !req.query.directory) return next(new HttpError(400, 'missing file or directory')); | 118 | if (!(req.files && req.files.file) && !req.query.directory) return next(new HttpError(400, 'missing file or directory')); |
118 | if ((req.files && req.files.file) && req.query.directory) return next(new HttpError(400, 'either file or directory')); | 119 | if ((req.files && req.files.file) && req.query.directory) return next(new HttpError(400, 'either file or directory')); |
119 | 120 | ||
121 | debug('post:', filePath); | ||
122 | |||
120 | var absoluteFilePath = getAbsolutePath(filePath); | 123 | var absoluteFilePath = getAbsolutePath(filePath); |
121 | if (!absoluteFilePath || isProtected(absoluteFilePath)) return next(new HttpError(403, 'Path not allowed')); | 124 | if (!absoluteFilePath || isProtected(absoluteFilePath)) return next(new HttpError(403, 'Path not allowed')); |
122 | 125 | ||
123 | fs.stat(absoluteFilePath, function (error, result) { | 126 | fs.stat(absoluteFilePath, function (error, result) { |
124 | if (error && error.code !== 'ENOENT') return next(new HttpError(500, error)); | 127 | if (error && error.code !== 'ENOENT') return next(new HttpError(500, error)); |
125 | 128 | ||
126 | debug('put', absoluteFilePath); | ||
127 | |||
128 | if (result && req.query.directory) return next(new HttpError(409, 'name already exists')); | 129 | if (result && req.query.directory) return next(new HttpError(409, 'name already exists')); |
129 | if (result && result.isDirectory()) return next(new HttpError(409, 'cannot put on directories')); | 130 | if (result && result.isDirectory()) return next(new HttpError(409, 'cannot post on directories')); |
130 | 131 | ||
131 | if (req.query.directory) { | 132 | if (req.query.directory) { |
132 | return createDirectory(absoluteFilePath, function (error) { | 133 | return createDirectory(absoluteFilePath, function (error) { |
@@ -144,6 +145,30 @@ function put(req, res, next) { | |||
144 | }); | 145 | }); |
145 | } | 146 | } |
146 | 147 | ||
148 | function put(req, res, next) { | ||
149 | var oldFilePath = decodeURIComponent(req.params[0]); | ||
150 | |||
151 | if (!req.body || !req.body.newFilePath) return next(new HttpError(400, 'missing newFilePath')); | ||
152 | |||
153 | var newFilePath = decodeURIComponent(req.body.newFilePath); | ||
154 | |||
155 | debug('put: %s -> %s', oldFilePath, newFilePath); | ||
156 | |||
157 | var absoluteOldFilePath = getAbsolutePath(oldFilePath); | ||
158 | if (!absoluteOldFilePath || isProtected(absoluteOldFilePath)) return next(new HttpError(403, 'Path not allowed')); | ||
159 | |||
160 | var absoluteNewFilePath = getAbsolutePath(newFilePath); | ||
161 | if (!absoluteNewFilePath || isProtected(absoluteNewFilePath)) return next(new HttpError(403, 'Path not allowed')); | ||
162 | |||
163 | fs.rename(absoluteOldFilePath, absoluteNewFilePath, function (error) { | ||
164 | if (error) return next (new HttpError(500, error)); | ||
165 | |||
166 | debug('put: successful'); | ||
167 | |||
168 | return next(new HttpSuccess(200, {})); | ||
169 | }); | ||
170 | } | ||
171 | |||
147 | function del(req, res, next) { | 172 | function del(req, res, next) { |
148 | var filePath = decodeURIComponent(req.params[0]); | 173 | var filePath = decodeURIComponent(req.params[0]); |
149 | var recursive = !!req.query.recursive; | 174 | var recursive = !!req.query.recursive; |