aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/files.js
diff options
context:
space:
mode:
authorJohannes Zellner <johannes@nebulon.de>2016-03-01 16:37:05 +0100
committerJohannes Zellner <johannes@nebulon.de>2016-03-01 16:37:05 +0100
commit403359cf6986b9c0f8c69f144cd36974d61a2370 (patch)
tree89ba9b26ff89afc617c89603b40f7b2cbf552c61 /src/files.js
parent61e0b8e4910051d0dfb4d454817b9f0511ba96ad (diff)
downloadSurfer-403359cf6986b9c0f8c69f144cd36974d61a2370.tar.gz
Surfer-403359cf6986b9c0f8c69f144cd36974d61a2370.tar.zst
Surfer-403359cf6986b9c0f8c69f144cd36974d61a2370.zip
Add ui to create directories
Diffstat (limited to 'src/files.js')
-rw-r--r--src/files.js21
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
57function createDirectory(targetPath, callback) {
58 mkdirp(targetPath, function (error) {
59 if (error) return callback(error);
60 callback(null);
61 });
62}
63
57function getAbsolutePath(filePath) { 64function 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) {
103function put(req, res, next) { 110function 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, {}));