From a47c95acd35f16c6d0283e76781ef3db1de65883 Mon Sep 17 00:00:00 2001 From: Armin Krauss Date: Wed, 3 Apr 2013 13:14:45 -0400 Subject: [PATCH 1/3] Allowing files to be uploaded to a folder in data/projects in order to make separation and backup of data easier --- data/.gitignore | 1 - pikachu.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) delete mode 100644 data/.gitignore diff --git a/data/.gitignore b/data/.gitignore deleted file mode 100644 index f59ec20..0000000 --- a/data/.gitignore +++ /dev/null @@ -1 +0,0 @@ -* \ No newline at end of file diff --git a/pikachu.js b/pikachu.js index 6c0968e..aee3c23 100644 --- a/pikachu.js +++ b/pikachu.js @@ -34,6 +34,32 @@ app.get('/upload.html', function (req, res) { res.sendfile(path.join(__dirname, 'upload.html')); }); +app.get('/projects', function (req, res) { + fs.readdir(path.join(DATA_DIR, 'projects'), function (err, files) { + res.type('json'); + + if (err) { + res.send(500, JSON.stringify(err)); + } else { + res.send(JSON.stringify(files)); + } + }); +}); + +app.get('/projects/:name', function (req, res) { + var folder = req.params.name; + fs.readdir(path.join(DATA_DIR, 'projects', folder), function (err, files) { + res.type('json'); + + if (err) { + res.send(500, JSON.stringify(err)); + } else { + res.send(JSON.stringify(files)); + } + }); + +}); + app.get('/', function (req, res) { fs.readdir(DATA_DIR, function (err, files) { res.type('json'); @@ -66,6 +92,31 @@ app.post('/', function (req, res) { }); }); +app.post('/projects/:name', function (req, res) { + var folder = req.params.name; + var storage_folder = path.join(DATA_DIR, 'projects', folder); + if (!fs.existsSync(storage_folder)) { + fs.mkdirSync(storage_folder); + } + fs.readdir(storage_folder, function (err, files) { + var newId = Math.random() * 9007199254740992; // max int value, see http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-t + var newFilename = newId.toString(36) + path.extname(req.files.file.filename); + fs.rename(req.files.file.path, path.join(storage_folder, newFilename), function (err) { + res.type('json'); + if (err) { + res.send(500, JSON.stringify(err)); + } else { + var f = { + url: newFilename, + size: req.files.file.size, + type: req.files.file.type + } + res.send(JSON.stringify(f)); + } + }); + }); +}); + http.createServer(app).listen(app.get('port'), function(){ console.log("Express server listening on port " + app.get('port')); }); From 8bf866825db4245ddd598fa0c8ad110bfb2c4709 Mon Sep 17 00:00:00 2001 From: Armin Krauss Date: Wed, 3 Apr 2013 13:18:48 -0400 Subject: [PATCH 2/3] Adding needed folders --- data/.gitignore | 1 + data/projects/.gitignore | 1 + 2 files changed, 2 insertions(+) create mode 100644 data/.gitignore create mode 100644 data/projects/.gitignore diff --git a/data/.gitignore b/data/.gitignore new file mode 100644 index 0000000..f2c4ee5 --- /dev/null +++ b/data/.gitignore @@ -0,0 +1 @@ +*.* diff --git a/data/projects/.gitignore b/data/projects/.gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/data/projects/.gitignore @@ -0,0 +1 @@ +* From d60db9b32b1afccb41ae6438a18a2286ec135441 Mon Sep 17 00:00:00 2001 From: Armin Krauss Date: Wed, 26 Mar 2014 16:19:28 -0400 Subject: [PATCH 3/3] I think this tree is not necessary ... --- package.json | 3 ++- pikachu.js | 69 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 2a454ae..1694774 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "start": "node app" }, "dependencies": { - "express": "3.1.0" + "express": "3.1.0", + "lodash": "*" } } \ No newline at end of file diff --git a/pikachu.js b/pikachu.js index aee3c23..a5fcee2 100644 --- a/pikachu.js +++ b/pikachu.js @@ -6,7 +6,8 @@ var express = require('express'), http = require('http'), path = require('path'), - fs = require('fs'); + fs = require('fs'), + _ = require('lodash'); var app = express(); @@ -34,32 +35,6 @@ app.get('/upload.html', function (req, res) { res.sendfile(path.join(__dirname, 'upload.html')); }); -app.get('/projects', function (req, res) { - fs.readdir(path.join(DATA_DIR, 'projects'), function (err, files) { - res.type('json'); - - if (err) { - res.send(500, JSON.stringify(err)); - } else { - res.send(JSON.stringify(files)); - } - }); -}); - -app.get('/projects/:name', function (req, res) { - var folder = req.params.name; - fs.readdir(path.join(DATA_DIR, 'projects', folder), function (err, files) { - res.type('json'); - - if (err) { - res.send(500, JSON.stringify(err)); - } else { - res.send(JSON.stringify(files)); - } - }); - -}); - app.get('/', function (req, res) { fs.readdir(DATA_DIR, function (err, files) { res.type('json'); @@ -67,7 +42,8 @@ app.get('/', function (req, res) { if (err) { res.send(500, JSON.stringify(err)); } else { - res.send(JSON.stringify(files)); + var nonHiddenFiles = _.filter(files, function(file) { return !isUnixHiddenPath(file); }); + res.send(JSON.stringify(nonHiddenFiles)); } }); }); @@ -92,6 +68,33 @@ app.post('/', function (req, res) { }); }); +app.get('/projects', function (req, res) { + fs.readdir(path.join(DATA_DIR, 'projects'), function (err, files) { + res.type('json'); + + if (err) { + res.send(500, JSON.stringify(err)); + } else { + var nonHiddenFiles = _.filter(files, function(file) { return !isUnixHiddenPath(file); }); + res.send(JSON.stringify(nonHiddenFiles)); + } + }); +}); + +app.get('/projects/:name', function (req, res) { + var folder = req.params.name; + fs.readdir(path.join(DATA_DIR, 'projects', folder), function (err, files) { + res.type('json'); + + if (err) { + res.send(500, JSON.stringify(err)); + } else { + res.send(JSON.stringify(files)); + } + }); + +}); + app.post('/projects/:name', function (req, res) { var folder = req.params.name; var storage_folder = path.join(DATA_DIR, 'projects', folder); @@ -147,3 +150,13 @@ function cors(req, res, next) { next(); } } + +/** + * Checks whether a path starts with or contains a hidden file or a folder. + * @param {string} source - The path of the file that needs to be validated. + * returns {boolean} - `true` if the source is blacklisted and otherwise `false`. + * via http://stackoverflow.com/questions/8905680/nodejs-check-for-hidden-files + */ +var isUnixHiddenPath = function (path) { + return (/(^|.\/)\.+[^\/\.]/g).test(path); +};