From 3a625e0496ac19c992e65fc562f2034bca4cc86c Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Fri, 31 Jul 2015 10:02:52 -0400 Subject: [PATCH] storage: update code that now uses streamrouter --- lib/storage/bucket.js | 121 ++++++++++++++--------------------------- system-test/storage.js | 13 +++-- test/storage/bucket.js | 48 +++------------- 3 files changed, 57 insertions(+), 125 deletions(-) diff --git a/lib/storage/bucket.js b/lib/storage/bucket.js index a09caa72afc..72fcb489ce7 100644 --- a/lib/storage/bucket.js +++ b/lib/storage/bucket.js @@ -382,61 +382,43 @@ Bucket.prototype.deleteFiles = function(query, callback) { query = query || {}; - var self = this; - var MAX_PARALLEL_LIMIT = 10; var errors = []; - // Start deleting files, iteratively fetching more as necessary. - deleteFiles(query, function(err) { - if (err || errors.length > 0) { - callback(err || errors); + this.getFiles(query, function(err, files) { + if (err) { + callback(err); return; } - callback(null); - }); - - function deleteFiles(query, callback) { - self.getFiles(query, function(err, files, nextQuery) { - if (err) { - callback(err); - return; - } - - // Iterate through each file and attempt to delete it. - async.eachLimit(files, MAX_PARALLEL_LIMIT, deleteFile, function(err) { + function deleteFile(file, callback) { + file.delete(function(err) { if (err) { - callback(err); - return; - } + if (query.force) { + errors.push(err); + callback(); + return; + } - if (nextQuery) { - deleteFiles(nextQuery, callback); + callback(err); return; } callback(); }); - }); - } - - function deleteFile(file, callback) { - file.delete(function(err) { - if (err) { - if (query.force) { - errors.push(err); - callback(); - return; - } + } - callback(err); + // Iterate through each file and attempt to delete it. + async.eachLimit(files, MAX_PARALLEL_LIMIT, deleteFile, function(err) { + if (err || errors.length > 0) { + callback(err || errors); return; } callback(); }); - } + }); + }; /** @@ -1033,69 +1015,50 @@ Bucket.prototype.upload = function(localPath, options, callback) { * @param {function} callback - The callback function. */ Bucket.prototype.makeAllFilesPublicPrivate_ = function(options, callback) { - var self = this; - var MAX_PARALLEL_LIMIT = 10; var errors = []; var updatedFiles = []; - // Start processing files, iteratively fetching more as necessary. - processFiles({}, function(err) { - if (err || errors.length > 0) { - callback(err || errors, updatedFiles); + this.getFiles(function(err, files) { + if (err) { + callback(err); return; } - callback(null, updatedFiles); - }); - - function processFiles(query, callback) { - self.getFiles(query, function(err, files, nextQuery) { - if (err) { - callback(err); - return; + function processFile(file, callback) { + if (options.public) { + file.makePublic(processedCallback); + } else if (options.private) { + file.makePrivate(processedCallback); } - // Iterate through each file and make it public or private. - async.eachLimit(files, MAX_PARALLEL_LIMIT, processFile, function(err) { + function processedCallback(err) { if (err) { - callback(err); - return; - } + if (options.force) { + errors.push(err); + callback(); + return; + } - if (nextQuery) { - processFiles(nextQuery, callback); + callback(err); return; } + updatedFiles.push(file); callback(); - }); - }); - } - - function processFile(file, callback) { - if (options.public) { - file.makePublic(processedCallback); - } else if (options.private) { - file.makePrivate(processedCallback); + } } - function processedCallback(err) { - if (err) { - if (options.force) { - errors.push(err); - callback(); - return; - } - - callback(err); + // Iterate through each file and make it public or private. + async.eachLimit(files, MAX_PARALLEL_LIMIT, processFile, function(err) { + if (err || errors.length > 0) { + callback(err || errors, updatedFiles); return; } - updatedFiles.push(file); - callback(); - } - } + callback(null, updatedFiles); + }); + }); }; /** diff --git a/system-test/storage.js b/system-test/storage.js index 46d793a7512..9ef6b3e82f3 100644 --- a/system-test/storage.js +++ b/system-test/storage.js @@ -818,12 +818,15 @@ describe('storage', function() { }); }); - afterEach(function(done) { - versionedBucket.deleteFiles({ versions: true }, done); - }); - after(function(done) { - versionedBucket.delete(done); + versionedBucket.deleteFiles({ versions: true }, function(err) { + if (err) { + done(err); + return; + } + + versionedBucket.delete(done); + }); }); it('should overwrite file, then get older version', function(done) { diff --git a/test/storage/bucket.js b/test/storage/bucket.js index e8fae1e0cae..759fbd56a84 100644 --- a/test/storage/bucket.js +++ b/test/storage/bucket.js @@ -406,23 +406,6 @@ describe('Bucket', function() { }); }); - it('should get more files if more exist', function(done) { - var fakeNextQuery = { a: 'b', c: 'd' }; - - bucket.getFiles = function(query, callback) { - if (Object.keys(query).length === 0) { - // First time through, return a `nextQuery` value. - callback(null, [], fakeNextQuery); - } else { - // Second time through. - assert.deepEqual(query, fakeNextQuery); - done(); - } - }; - - bucket.deleteFiles({}, assert.ifError); - }); - it('should execute callback with error from getting files', function(done) { var error = new Error('Error.'); @@ -1024,7 +1007,7 @@ describe('Bucket', function() { done(); }; - bucket.getFiles = function(query, callback) { + bucket.getFiles = function(callback) { callback(null, []); }; @@ -1042,7 +1025,7 @@ describe('Bucket', function() { callback(); })); - bucket.getFiles = function(query, callback) { + bucket.getFiles = function(callback) { callback(null, files); }; @@ -1064,7 +1047,7 @@ describe('Bucket', function() { callback(); })); - bucket.getFiles = function(query, callback) { + bucket.getFiles = function(callback) { callback(null, files); }; @@ -1075,27 +1058,10 @@ describe('Bucket', function() { }); }); - it('should get more files if more exist', function(done) { - var fakeNextQuery = { a: 'b', c: 'd' }; - - bucket.getFiles = function(query, callback) { - if (Object.keys(query).length === 0) { - // First time through, return a `nextQuery` value. - callback(null, [], fakeNextQuery); - } else { - // Second time through. - assert.deepEqual(query, fakeNextQuery); - done(); - } - }; - - bucket.makeAllFilesPublicPrivate_({}, assert.ifError); - }); - it('should execute callback with error from getting files', function(done) { var error = new Error('Error.'); - bucket.getFiles = function(query, callback) { + bucket.getFiles = function(callback) { callback(error); }; @@ -1115,7 +1081,7 @@ describe('Bucket', function() { callback(error); })); - bucket.getFiles = function(query, callback) { + bucket.getFiles = function(callback) { callback(null, files); }; @@ -1135,7 +1101,7 @@ describe('Bucket', function() { callback(error); })); - bucket.getFiles = function(query, callback) { + bucket.getFiles = function(callback) { callback(null, files); }; @@ -1165,7 +1131,7 @@ describe('Bucket', function() { callback(error); })); - bucket.getFiles = function(query, callback) { + bucket.getFiles = function(callback) { callback(null, successFiles.concat(errorFiles)); };