From 71e0a322ece079edc24b30607406065051a0c859 Mon Sep 17 00:00:00 2001 From: Kieran Sedgwick Date: Wed, 8 Jul 2015 12:58:22 -0400 Subject: [PATCH 1/2] Made mox create and read from a directory structure if a filename contains a path --- mox.js | 45 +++++++++++++++++++++++++++++++++++++-------- test-noxmox.js | 2 +- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/mox.js b/mox.js index e9eb5c7..327f302 100644 --- a/mox.js +++ b/mox.js @@ -101,20 +101,49 @@ exports.createClient = function createClient(options) { } // map S3 keyname to filename - function getFilePath(keyname, newFile) { + function getFilePath(filePath, newFile) { // Backward incompatible (version <= 0.2.3) check for existing file if (!newFile) { - var oldFilePath = path.join(bucketPath, keyname); + var oldFilePath = path.join(bucketPath, filePath); if (fs.existsSync(oldFilePath)) { return oldFilePath; } } - // The new naming scheme: - // encode keyname using the fixedEncodeURIComponent from - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent - var filename = encodeURIComponent(keyname).replace( - /[!'()]/g, escape).replace(/\*/g, "%2A"); - return path.join(bucketPath, filename); + + // Isolate each part of the file path + filePath = filePath.split('/'); + if (filePath[0].length === 0) { + filePath.shift(); + } + + // Encode each part + filePath = filePath.map(function(section) { + // The new naming scheme: + // encode filePath using the fixedEncodeURIComponent from + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent + return encodeURIComponent(section) + .replace(/[!'()]/g, escape) + .replace(/\*/g, "%2A"); + }); + + var fileName = filePath.pop(); + + // Ensure the path exists, then recombine + var tmp = '/'; + filePath.forEach(function(el) { + tmp = path.join(tmp, el); + + try { + fs.mkdirSync(path.join(bucketPath, tmp)); + } catch (e) { + if (e.code !== 'EEXIST') { + throw e; + } + } + }); + filePath = filePath.join('/'); + + return path.join(bucketPath, filePath, fileName); } function emitResponse(request, opts) { diff --git a/test-noxmox.js b/test-noxmox.js index a944011..d634450 100644 --- a/test-noxmox.js +++ b/test-noxmox.js @@ -82,7 +82,7 @@ function runTests(data) { function test(client, callback) { - var name = 'test/noxmox.txt'; + var name = 'test/foo/blaz/noxmox.txt'; t1(); function t1() { var buf = new Buffer('Testing the noxmox lib.'); From 11be57eba433ef6a8cdf6cdcf6a1db736565d0b2 Mon Sep 17 00:00:00 2001 From: Kieran Sedgwick Date: Tue, 14 Jul 2015 14:13:56 -0400 Subject: [PATCH 2/2] Fix for folder uri encoding --- mox.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/mox.js b/mox.js index 327f302..181215d 100644 --- a/mox.js +++ b/mox.js @@ -116,17 +116,10 @@ exports.createClient = function createClient(options) { filePath.shift(); } - // Encode each part - filePath = filePath.map(function(section) { - // The new naming scheme: - // encode filePath using the fixedEncodeURIComponent from - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent - return encodeURIComponent(section) + var fileName = filePath.pop(); + fileName = encodeURIComponent(fileName) .replace(/[!'()]/g, escape) .replace(/\*/g, "%2A"); - }); - - var fileName = filePath.pop(); // Ensure the path exists, then recombine var tmp = '/';