Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefixes to results from bucket.getFiles() #342

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
3. Add your (or your organization's) name to the AUTHORS and CONTRIBUTORS files.
4. Send a pull request.

## Code quality

To check code use:

``` sh
$ npm run lint
```

This comment was marked as spam.


## Testing

### Unit Tests
Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

Burcu Dogan <jbd@google.com>
Johan Euphrosine <proppy@google.com>
Maciej Chmielarski <m.chmielarski@gmail.com>
Patrick Costello <pcostell@google.com>
Silvano Luciani <silvano@google.com>
Stephen Sawchuk <sawchuk@gmail.com>

26 changes: 25 additions & 1 deletion lib/storage/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,28 @@ Bucket.prototype.file = function(name) {
* bucket.getFiles({
* maxResults: 5
* }, function(err, files, nextQuery) {});
*
* //-
* // Using delimiter and prefix parameter for results in
* // directory-like mode. In conjunction with the prefix filter,
* // the use of the delimiter parameter allows the list method to
* // operate like a directory listing, despite the object namespace
* // being flat. For example, if delimiter were set to "/",
* // then listing objects from a bucket that contains the objects
* // "a/b", "a/c", "d", "e", "e/f" would return objects "d" and "e",
* // and prefixes "a/" and "e/"
* //-
* var query = { delimiter: "/" };
* bucket.getFiles(query, function(err, files, nextQuery, prefixes) {
* // files = ["d", "e"]
* // prefixes = ["a/", "e/"]
*
* query.prefix = "a/";
* bucket.getFiles(query, function(err, files, nextQuery, prefixes) {
* // files = ["b", "c"]
* // prefixes = null;
* });
* });
*/
Bucket.prototype.getFiles = function(query, callback) {
var that = this;
Expand All @@ -252,16 +274,18 @@ Bucket.prototype.getFiles = function(query, callback) {
callback(err);
return;
}

var files = (resp.items || []).map(function(item) {
var file = that.file(item.name);
file.metadata = item;
return file;
});
var prefixes = resp.prefixes || null;
var nextQuery = null;
if (resp.nextPageToken) {
nextQuery = extend({}, query, { pageToken: resp.nextPageToken });
}
callback(null, files, nextQuery);
callback(null, files, nextQuery, prefixes);

This comment was marked as spam.

This comment was marked as spam.

});
};

Expand Down
59 changes: 59 additions & 0 deletions regression/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,65 @@ describe('storage', function() {
});
});

describe('prefixes', function() {
var filenames = ['a/b', 'a/c', 'd', 'e', 'e/f', 'e/f/g'];

before(function(done) {
async.parallel(
filenames.map(function(filename) {
return function(callback) {
var file = bucket.file(filename);
var contents = 'test';

var writeStream = file.createWriteStream({ resumable: false });
writeStream.on('error', function() {
callback();
});
writeStream.on('complete', function() {
callback();
});
writeStream.write(contents);
writeStream.end();
};
}), done);
});

after(function(done) {
async.parallel(
filenames.map(function(filename) {
return function(callback) {
bucket.file(filename).delete(callback);
};
}), done);
});

it('should use delimiter', function(done) {
var query = { delimiter: '/' };
bucket.getFiles(query, function(err, files, nextQuery, prefixes) {
assert.ifError(err);
assert.equal(files.length, 2);
['a/', 'e/'].forEach(function(val) {
assert.equal((prefixes.indexOf(val) > -1), true);
});
assert.equal(nextQuery, null);
done();
});
});

it('should use prefix & delimiter', function(done) {
var query = { delimiter: '/', prefix: 'e/' };
bucket.getFiles(query, function(err, files, nextQuery, prefixes) {
assert.ifError(err);
assert.equal(files.length, 1);
['e/f/'].forEach(function(val) {
assert.equal((prefixes.indexOf(val) > -1), true);
});
assert.equal(nextQuery, null);
done();
});
});
});

describe('sign urls', function() {
var localFile = fs.readFileSync(files.logo.path);
var file;
Expand Down
24 changes: 24 additions & 0 deletions test/storage/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,30 @@ describe('Bucket', function() {
done();
});
});

it('should return prefixes', function(done) {
var fakePrefixes = ['fake-prefix-1'];
bucket.makeReq_ = function(method, path, query, body, callback) {
callback(null, { prefixes: fakePrefixes });
};
bucket.getFiles(function(err, files, nextQuery, prefixes) {
assert.ifError(err);
assert(prefixes, fakePrefixes);
done();
});
});

it('should return null if no prefixes', function(done) {
bucket.makeReq_ = function(method, path, query, body, callback) {
callback(null, {});
};
bucket.getFiles(function(err, files, nextQuery, prefixes) {
console.log(prefixes);
assert.ifError(err);
assert.strictEqual(nextQuery, null);
done();
});
});
});

describe('getMetadata', function() {
Expand Down