Skip to content

Commit

Permalink
Adding mkdirp to POST, to make interemediary directories.
Browse files Browse the repository at this point in the history
  • Loading branch information
teknopaul committed Sep 2, 2015
1 parent 8ea672a commit 97ab2b4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions .npmignore
@@ -0,0 +1 @@
node_nodules
3 changes: 2 additions & 1 deletion server/http_mods/rest.js
Expand Up @@ -4,6 +4,7 @@ var fs = require('fs');
var defaults = require('./default');
var resolve = require('../persistence/file-resolve');
var config = require('../util/config.js').configData;
var util = require('../util/util.js');


/**
Expand Down Expand Up @@ -96,7 +97,7 @@ doPost = function(request, response, url) {

request.on('end', function() {

fs.mkdir(dirname, 0755, function(err) {
util.mkdirp(dirname, 0755, function(err) {
var safeBean = null;
try {
safeBean = JSON.parse(buffer); // check it is valid JSON
Expand Down
41 changes: 41 additions & 0 deletions server/util/util.js
@@ -0,0 +1,41 @@
var fs = require('fs');
var path = require('path');

/**
* mkdir -p for nodejs
* @author substack
*/
var mkdirp = function(pathname, mode, cb, made) {
if (!made) made = null;

pathname = path.resolve(pathname);

fs.mkdir(pathname, mode, function (er) {
if (!er) {
made = made || pathname;
return cb(null, made);
}
switch (er.code) {
case 'ENOENT':
mkdirp(path.dirname(pathname), mode, function (er, made) {
if (er) cb(er, made);
else mkdirp(pathname, mode, cb, made);
});
break;

// In the case of any other error, just see if there's a dir
// there already. If so, then hooray! If not, then something
// is borked.
default:
fs.stat(pathname, function (er2, stat) {
// if the stat fails, then that's super weird.
// let the original error be the failure reason.
if (er2 || !stat.isDirectory()) cb(er, made)
else cb(null, made);
});
break;
}
});
};

exports.mkdirp = mkdirp;

0 comments on commit 97ab2b4

Please sign in to comment.