Skip to content

Commit aada90d

Browse files
refactor
1 parent 9b85196 commit aada90d

File tree

3 files changed

+109
-124
lines changed

3 files changed

+109
-124
lines changed

lib/storage/acl.js

+14-39
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,10 @@ var util = require('../common/util.js');
4242
* @constructor
4343
* @alias module:storage/acl
4444
*/
45-
function Acl(module) {
46-
this.assignAccessMethods_(this, '/acl');
47-
48-
if (module.constructor.name === 'Bucket') {
49-
this.default = {};
50-
this.assignAccessMethods_(this.default, '/defaultObjectAcl');
51-
52-
this.bucket = module;
53-
} else {
54-
this.bucket = module.bucket;
55-
this.file = module;
56-
}
45+
function Acl(options) {
46+
this.bucket = options.bucket;
47+
this.makeReq = options.makeReq;
48+
this.pathPrefix = options.pathPrefix;
5749
}
5850

5951
/**
@@ -71,15 +63,15 @@ function Acl(module) {
7163
*
7264
* myBucket.acl.add(scope, role, function(err, aclObject) {});
7365
*/
74-
Acl.prototype.add_ = function(path, scope, role, callback) {
66+
Acl.prototype.add = function(scope, role, callback) {
7567
var that = this;
7668

7769
var body = {
7870
entity: scope,
7971
role: role.toUpperCase()
8072
};
8173

82-
this.makeReq_('POST', path, null, body, function(err, resp) {
74+
this.makeReq_('POST', '', null, body, function(err, resp) {
8375
if (err) {
8476
callback(err);
8577
return;
@@ -102,8 +94,8 @@ Acl.prototype.add_ = function(path, scope, role, callback) {
10294
*
10395
* myBucket.acl.delete(scope, function(err) {});
10496
*/
105-
Acl.prototype.delete_ = function(path, scope, callback) {
106-
path += '/' + encodeURIComponent(scope);
97+
Acl.prototype.delete = function(scope, callback) {
98+
var path = '/' + encodeURIComponent(scope);
10799
this.makeReq_('DELETE', path, null, null, callback);
108100
};
109101

@@ -134,11 +126,12 @@ Acl.prototype.delete_ = function(path, scope, callback) {
134126
* // ]
135127
* });
136128
*/
137-
Acl.prototype.get_ = function(path, scope, callback) {
129+
Acl.prototype.get = function(scope, callback) {
138130
var that = this;
131+
var path = '';
139132

140133
if (util.is(scope, 'string')) {
141-
path += '/' + encodeURIComponent(scope);
134+
path = '/' + encodeURIComponent(scope);
142135
} else if (util.is(scope, 'function')) {
143136
callback = scope;
144137
}
@@ -176,10 +169,9 @@ Acl.prototype.get_ = function(path, scope, callback) {
176169
*
177170
* myBucket.acl.update(scope, role, function(err) {});
178171
*/
179-
Acl.prototype.update_ = function(path, scope, role, callback) {
172+
Acl.prototype.update = function(scope, role, callback) {
180173
var that = this;
181-
182-
path += '/' + encodeURIComponent(scope);
174+
var path = '/' + encodeURIComponent(scope);
183175

184176
var body = {
185177
role: role.toUpperCase()
@@ -195,19 +187,6 @@ Acl.prototype.update_ = function(path, scope, role, callback) {
195187
});
196188
};
197189

198-
/**
199-
* Iterate over the access methods, and apply them to the specified object.
200-
*
201-
* @private
202-
*/
203-
Acl.prototype.assignAccessMethods_ = function(destObj, path) {
204-
var that = this;
205-
206-
['add', 'delete', 'get', 'update'].forEach(function(method) {
207-
destObj[method] = that[method + '_'].bind(that, path);
208-
});
209-
};
210-
211190
/**
212191
* Transform API responses to a consistent object format.
213192
*
@@ -238,11 +217,7 @@ Acl.prototype.makeAclObject_ = function(accessControlObject) {
238217
* @param {function} callback - The callback function.
239218
*/
240219
Acl.prototype.makeReq_ = function(method, path, query, body, callback) {
241-
if (this.file) {
242-
path = '/o/' + encodeURIComponent(this.file.name) + path;
243-
}
244-
245-
this.bucket.makeReq_(method, path, query, body, callback);
220+
this.makeReq(method, this.pathPrefix + path, query, body, callback);
246221
};
247222

248223
module.exports = Acl;

lib/storage/bucket.js

+71-64
Original file line numberDiff line numberDiff line change
@@ -78,83 +78,90 @@ var STORAGE_BASE_URL = 'https://www.googleapis.com/storage/v1/b';
7878
* });
7979
*/
8080
function Bucket(storage, name) {
81-
this.acl = new Acl(this);
8281
this.metadata = {};
8382
this.name = name;
8483
this.storage = storage;
8584

8685
if (!this.name) {
8786
throw Error('A bucket name is needed to use Google Cloud Storage.');
8887
}
89-
}
9088

91-
/**
92-
* Google Cloud Storage uses access control lists (ACLs) to manage object and
93-
* bucket access. ACLs are the mechanism you use to share objects with other
94-
* users and allow other users to access your buckets and objects.
95-
*
96-
* An ACL consists of one or more entries, where each entry grants permissions
97-
* to a scope. Permissions define the actions that can be performed against an
98-
* object or bucket (for example, `READ` or `WRITE`); the scope defines who the
99-
* permission applies to (for example, a specific user or group of users).
100-
*
101-
* For more detailed information, see
102-
* [About Access Control Lists](http://goo.gl/6qBBPO).
103-
*
104-
* The `acl` object on a Bucket instance provides methods to get you a list of
105-
* the ACLs defined on your bucket, as well as set, update, and delete them.
106-
*
107-
* Buckets also have
108-
* [default ACLs](https://cloud.google.com/storage/docs/accesscontrol#default)
109-
* for all created files. You can add, delete, get, and update scopes and
110-
* permissions for these as well. See {module:storage/acl#acl.default}.
111-
*
112-
* @mixes module:storage/acl
113-
*/
114-
Bucket.prototype.acl = {};
89+
/**
90+
* Google Cloud Storage uses access control lists (ACLs) to manage object and
91+
* bucket access. ACLs are the mechanism you use to share objects with other
92+
* users and allow other users to access your buckets and objects.
93+
*
94+
* An ACL consists of one or more entries, where each entry grants permissions
95+
* to a scope. Permissions define the actions that can be performed against an
96+
* object or bucket (for example, `READ` or `WRITE`); the scope defines who
97+
* the permission applies to (for example, a specific user or group of users).
98+
*
99+
* For more detailed information, see
100+
* [About Access Control Lists](http://goo.gl/6qBBPO).
101+
*
102+
* The `acl` object on a Bucket instance provides methods to get you a list of
103+
* the ACLs defined on your bucket, as well as set, update, and delete them.
104+
*
105+
* Buckets also have
106+
* [default ACLs](https://cloud.google.com/storage/docs/accesscontrol#default)
107+
* for all created files. You can add, delete, get, and update scopes and
108+
* permissions for these as well. See {module:storage/acl#acl.default}.
109+
*
110+
* @mixes module:storage/acl
111+
*/
112+
this.acl = new Acl({
113+
makeReq: this.makeReq_.bind(this),
114+
pathPrefix: '/acl'
115+
});
115116

116-
/* jshint ignore:start */
117-
/*! Developer Documentation
118-
*
119-
* Sadly, to generate the documentation properly, this comment block describes a
120-
* useless variable named `ignored` and aliases it to `acl.default`. This is
121-
* done so the doc building process picks this up, without adding cruft to the
122-
* Bucket class itself.
123-
*/
124-
/**
125-
* Google Cloud Storage Buckets have [default ACLs](http://goo.gl/YpGdyv)
126-
* for all created files. You can add, delete, get, and update scopes and
127-
* permissions for these as well. The method signatures and examples are all
128-
* the same, after only prefixing the method call with `default`.
129-
*
130-
* @alias acl.default
131-
*/
132-
var aclDefault = true;
117+
this.acl.default = new Acl({
118+
makeReq: this.makeReq_.bind(this),
119+
pathPrefix: '/defaultObjectAcl'
120+
});
133121

134-
/**
135-
* Maps to {module:storage/bucket#acl.add}.
136-
* @alias acl.default.add
137-
*/
138-
var aclDefaultAdd = true;
122+
/* jshint ignore:start */
123+
/*! Developer Documentation
124+
*
125+
* Sadly, to generate the documentation properly, this comment block describes
126+
* a useless variable named `ignored` and aliases it to `acl.default`. This is
127+
* done so the doc building process picks this up, without adding cruft to the
128+
* Bucket class itself.
129+
*/
130+
/**
131+
* Google Cloud Storage Buckets have [default ACLs](http://goo.gl/YpGdyv) for
132+
* all created files. You can add, delete, get, and update scopes and
133+
* permissions for these as well. The method signatures and examples are all
134+
* the same, after only prefixing the method call with `default`.
135+
*
136+
* @alias acl.default
137+
*/
138+
var aclDefault = true;
139139

140-
/**
141-
* Maps to {module:storage/bucket#acl.delete}.
142-
* @alias acl.default.delete
143-
*/
144-
var aclDefaultDelete = true;
140+
/**
141+
* Maps to {module:storage/bucket#acl.add}.
142+
* @alias acl.default.add
143+
*/
144+
var aclDefaultAdd = true;
145145

146-
/**
147-
* Maps to {module:storage/bucket#acl.get}.
148-
* @alias acl.default.get
149-
*/
150-
var aclDefaultGet = true;
146+
/**
147+
* Maps to {module:storage/bucket#acl.delete}.
148+
* @alias acl.default.delete
149+
*/
150+
var aclDefaultDelete = true;
151151

152-
/**
153-
* Maps to {module:storage/bucket#acl.update}.
154-
* @alias acl.default.update
155-
*/
156-
var aclDefaultUpdate = true;
157-
/* jshint ignore:end */
152+
/**
153+
* Maps to {module:storage/bucket#acl.get}.
154+
* @alias acl.default.get
155+
*/
156+
var aclDefaultGet = true;
157+
158+
/**
159+
* Maps to {module:storage/bucket#acl.update}.
160+
* @alias acl.default.update
161+
*/
162+
var aclDefaultUpdate = true;
163+
/* jshint ignore:end */
164+
}
158165

159166
/**
160167
* Delete the bucket.

lib/storage/file.js

+24-21
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,36 @@ function File(bucket, name, metadata) {
6666

6767
this.bucket = bucket;
6868
this.makeReq_ = bucket.makeReq_.bind(bucket);
69-
this.acl = new Acl(this);
7069
this.metadata = metadata || {};
70+
7171
Object.defineProperty(this, 'name', {
7272
enumerable: true,
7373
value: name
7474
});
75-
}
7675

77-
/**
78-
* Google Cloud Storage uses access control lists (ACLs) to manage object and
79-
* bucket access. ACLs are the mechanism you use to share objects with other
80-
* users and allow other users to access your buckets and objects.
81-
*
82-
* An ACL consists of one or more entries, where each entry grants permissions
83-
* to a scope. Permissions define the actions that can be performed against an
84-
* object or bucket (for example, `READ` or `WRITE`); the scope defines who the
85-
* permission applies to (for example, a specific user or group of users).
86-
*
87-
* For more detailed information, see
88-
* [About Access Control Lists](http://goo.gl/6qBBPO).
89-
*
90-
* The `acl` object on a File instance provides methods to get you a list of
91-
* the ACLs defined on your bucket, as well as set, update, and delete them.
92-
*
93-
* @mixes module:storage/acl
94-
*/
95-
File.prototype.acl = {};
76+
/**
77+
* Google Cloud Storage uses access control lists (ACLs) to manage object and
78+
* bucket access. ACLs are the mechanism you use to share objects with other
79+
* users and allow other users to access your buckets and objects.
80+
*
81+
* An ACL consists of one or more entries, where each entry grants permissions
82+
* to a scope. Permissions define the actions that can be performed against an
83+
* object or bucket (for example, `READ` or `WRITE`); the scope defines who
84+
* the permission applies to (for example, a specific user or group of users).
85+
*
86+
* For more detailed information, see
87+
* [About Access Control Lists](http://goo.gl/6qBBPO).
88+
*
89+
* The `acl` object on a File instance provides methods to get you a list of
90+
* the ACLs defined on your bucket, as well as set, update, and delete them.
91+
*
92+
* @mixes module:storage/acl
93+
*/
94+
this.acl = new Acl({
95+
makeReq: this.makeReq_,
96+
pathPrefix: '/o/' + encodeURIComponent(this.name) + '/acl'
97+
});
98+
}
9699

97100
/**
98101
* Copy this file to another file. By default, this will copy the file to the

0 commit comments

Comments
 (0)