From e3a3c50fb4db10ee717c3cf2fd53bff18c5180a1 Mon Sep 17 00:00:00 2001 From: agolybev Date: Thu, 3 Sep 2015 19:15:46 +0300 Subject: [PATCH] fix: in windows getObjects return absolute path to file including drive and dir instead of relative --- lib/file-store.js | 14 ++++++++++++-- lib/utils.js | 4 ++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/file-store.js b/lib/file-store.js index ad7ab5dc..343fa8a4 100644 --- a/lib/file-store.js +++ b/lib/file-store.js @@ -93,11 +93,17 @@ var FileStore = function (rootDirectory) { var getObjects = function (bucket, options, done) { var bucketPath = getBucketPath(bucket.name); + var bucketPathWithSep = path.join(bucketPath, '/'); + var replaceSeparator = '/' !== path.sep; var matches = []; var keys = utils.walk(bucketPath); var filteredKeys = _.filter(keys, function (file) { if (options.prefix) { - var key = file.replace(bucketPath + '/', ''); + var key = file.replace(bucketPathWithSep, ''); + if(replaceSeparator){ + key = key.replace(new RegExp(utils.escapeRegExp(path.sep), 'g'), '/'); + } + //expect options.prefix use '/' separator var match = (key.substring(0, options.prefix.length) === options.prefix); return match; } @@ -106,7 +112,11 @@ var FileStore = function (rootDirectory) { async.eachSeries(filteredKeys, function (key, callback) { fs.readFile(path.join(key, METADATA_FILE), function (err, data) { if (data) { - matches.push(buildS3ObjectFromMetaDataFile(key.replace(bucketPath + '/', ''), data)); + var outputKey = key.replace(bucketPathWithSep, ''); + if(replaceSeparator){ + outputKey = outputKey.replace(new RegExp(utils.escapeRegExp(path.sep), 'g'), '/'); + } + matches.push(buildS3ObjectFromMetaDataFile(outputKey, data)); } callback(null); }); diff --git a/lib/utils.js b/lib/utils.js index f6dbfdc6..687187bd 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -17,3 +17,7 @@ exports.walk = function (dir) { return results; }; +// Gathered from http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript +exports.escapeRegExp = function (string) { + return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); +}; \ No newline at end of file