Skip to content
This repository was archived by the owner on May 21, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions mox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test-noxmox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down