From: Johannes Zellner Date: Tue, 1 Mar 2016 15:37:05 +0000 (+0100) Subject: Add ui to create directories X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=403359cf6986b9c0f8c69f144cd36974d61a2370;p=perso%2FImmae%2FProjets%2FNodejs%2FSurfer.git Add ui to create directories --- diff --git a/app/index.html b/app/index.html index cf1f399..ea14bc1 100644 --- a/app/index.html +++ b/app/index.html @@ -49,6 +49,29 @@ + +
@@ -126,7 +149,7 @@
- +
diff --git a/app/js/app.js b/app/js/app.js index 823e4f7..c0ed616 100644 --- a/app/js/app.js +++ b/app/js/app.js @@ -79,9 +79,12 @@ function up() { } function upload() { - $(app.$els.upload).change(function () { + $(app.$els.upload).on('change', function () { app.busy = true; + // detach event handler + $(app.$els.upload).off('change'); + var file = app.$els.upload.files[0]; var path = encode(sanitize(app.path + '/' + file.name)); @@ -123,6 +126,29 @@ function del(entry) { }); } +function createDirectoryAsk() { + $('#modalcreateDirectory').modal('show'); + app.createDirectoryData = ''; +} + +function createDirectory(name) { + app.busy = true; + + var path = encode(sanitize(app.path + '/' + name)); + + superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password, directory: true }).end(function (error, result) { + app.busy = false; + + if (error) return console.error(error); + if (result.statusCode !== 201) return console.error('Error creating directory: ', result.statusCode); + + app.createDirectoryData = ''; + refresh(); + + $('#modalcreateDirectory').modal('hide'); + }); +} + var app = new Vue({ el: '#app', data: { @@ -134,6 +160,7 @@ var app = new Vue({ }, loginData: {}, deleteData: {}, + createDirectoryData: '', entries: [] }, methods: { @@ -144,7 +171,9 @@ var app = new Vue({ up: up, upload: upload, delAsk: delAsk, - del: del + del: del, + createDirectoryAsk: createDirectoryAsk, + createDirectory: createDirectory } }); 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) { }); } +function createDirectory(targetPath, callback) { + mkdirp(targetPath, function (error) { + if (error) return callback(error); + callback(null); + }); +} + function getAbsolutePath(filePath) { var absoluteFilePath = path.resolve(path.join(gBasePath, filePath)); @@ -103,7 +110,8 @@ function get(req, res, next) { function put(req, res, next) { var filePath = req.params[0]; - if (!req.files.file) return next(new HttpError(400, 'missing file')); + if (!(req.files && req.files.file) && !req.query.directory) return next(new HttpError(400, 'missing file or directory')); + if ((req.files && req.files.file) && req.query.directory) return next(new HttpError(400, 'either file or directory')); var absoluteFilePath = getAbsolutePath(filePath); if (!absoluteFilePath) return next(new HttpError(403, 'Path not allowed')); @@ -111,10 +119,17 @@ function put(req, res, next) { fs.stat(absoluteFilePath, function (error, result) { if (error && error.code !== 'ENOENT') return next(new HttpError(500, error)); - debug('put', absoluteFilePath, req.files.file); + debug('put', absoluteFilePath); + if (result && req.query.directory) return next(new HttpError(409, 'name already exists')); if (result && result.isDirectory()) return next(new HttpError(409, 'cannot put on directories')); - if (!result || result.isFile()) { + + if (req.query.directory) { + return createDirectory(absoluteFilePath, function (error) { + if (error) return next(new HttpError(500, error)); + next(new HttpSuccess(201, {})); + }); + } else if (!result || result.isFile()) { return copyFile(req.files.file.path, absoluteFilePath, function (error) { if (error) return next(new HttpError(500, error)); 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) { module.exports = function multipart(options) { return function (req, res, next) { - if (_mime(req) !== 'multipart/form-data') return res.status(400).send('Invalid content-type. Expecting multipart'); + if (_mime(req) !== 'multipart/form-data') return next(null); var form = new multiparty.Form({ uploadDir: '/tmp',