diff --git a/mox.js b/mox.js index e9eb5c7..181215d 100644 --- a/mox.js +++ b/mox.js @@ -101,20 +101,42 @@ 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(); + } + + var fileName = filePath.pop(); + fileName = encodeURIComponent(fileName) + .replace(/[!'()]/g, escape) + .replace(/\*/g, "%2A"); + + // 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.');