Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Nov 18, 2014
1 parent 9b85196 commit aada90d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 124 deletions.
53 changes: 14 additions & 39 deletions lib/storage/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,10 @@ var util = require('../common/util.js');
* @constructor
* @alias module:storage/acl
*/
function Acl(module) {
this.assignAccessMethods_(this, '/acl');

if (module.constructor.name === 'Bucket') {
this.default = {};
this.assignAccessMethods_(this.default, '/defaultObjectAcl');

this.bucket = module;
} else {
this.bucket = module.bucket;
this.file = module;
}
function Acl(options) {
this.bucket = options.bucket;
this.makeReq = options.makeReq;
this.pathPrefix = options.pathPrefix;
}

/**
Expand All @@ -71,15 +63,15 @@ function Acl(module) {
*
* myBucket.acl.add(scope, role, function(err, aclObject) {});
*/
Acl.prototype.add_ = function(path, scope, role, callback) {
Acl.prototype.add = function(scope, role, callback) {
var that = this;

var body = {
entity: scope,
role: role.toUpperCase()
};

this.makeReq_('POST', path, null, body, function(err, resp) {
this.makeReq_('POST', '', null, body, function(err, resp) {
if (err) {
callback(err);
return;
Expand All @@ -102,8 +94,8 @@ Acl.prototype.add_ = function(path, scope, role, callback) {
*
* myBucket.acl.delete(scope, function(err) {});
*/
Acl.prototype.delete_ = function(path, scope, callback) {
path += '/' + encodeURIComponent(scope);
Acl.prototype.delete = function(scope, callback) {
var path = '/' + encodeURIComponent(scope);
this.makeReq_('DELETE', path, null, null, callback);
};

Expand Down Expand Up @@ -134,11 +126,12 @@ Acl.prototype.delete_ = function(path, scope, callback) {
* // ]
* });
*/
Acl.prototype.get_ = function(path, scope, callback) {
Acl.prototype.get = function(scope, callback) {
var that = this;
var path = '';

if (util.is(scope, 'string')) {
path += '/' + encodeURIComponent(scope);
path = '/' + encodeURIComponent(scope);
} else if (util.is(scope, 'function')) {
callback = scope;
}
Expand Down Expand Up @@ -176,10 +169,9 @@ Acl.prototype.get_ = function(path, scope, callback) {
*
* myBucket.acl.update(scope, role, function(err) {});
*/
Acl.prototype.update_ = function(path, scope, role, callback) {
Acl.prototype.update = function(scope, role, callback) {
var that = this;

path += '/' + encodeURIComponent(scope);
var path = '/' + encodeURIComponent(scope);

var body = {
role: role.toUpperCase()
Expand All @@ -195,19 +187,6 @@ Acl.prototype.update_ = function(path, scope, role, callback) {
});
};

/**
* Iterate over the access methods, and apply them to the specified object.
*
* @private
*/
Acl.prototype.assignAccessMethods_ = function(destObj, path) {
var that = this;

['add', 'delete', 'get', 'update'].forEach(function(method) {
destObj[method] = that[method + '_'].bind(that, path);
});
};

/**
* Transform API responses to a consistent object format.
*
Expand Down Expand Up @@ -238,11 +217,7 @@ Acl.prototype.makeAclObject_ = function(accessControlObject) {
* @param {function} callback - The callback function.
*/
Acl.prototype.makeReq_ = function(method, path, query, body, callback) {
if (this.file) {
path = '/o/' + encodeURIComponent(this.file.name) + path;
}

this.bucket.makeReq_(method, path, query, body, callback);
this.makeReq(method, this.pathPrefix + path, query, body, callback);
};

module.exports = Acl;
135 changes: 71 additions & 64 deletions lib/storage/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,83 +78,90 @@ var STORAGE_BASE_URL = 'https://www.googleapis.com/storage/v1/b';
* });
*/
function Bucket(storage, name) {
this.acl = new Acl(this);
this.metadata = {};
this.name = name;
this.storage = storage;

if (!this.name) {
throw Error('A bucket name is needed to use Google Cloud Storage.');
}
}

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

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

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

/**
* Maps to {module:storage/bucket#acl.delete}.
* @alias acl.default.delete
*/
var aclDefaultDelete = true;
/**
* Maps to {module:storage/bucket#acl.add}.
* @alias acl.default.add
*/
var aclDefaultAdd = true;

/**
* Maps to {module:storage/bucket#acl.get}.
* @alias acl.default.get
*/
var aclDefaultGet = true;
/**
* Maps to {module:storage/bucket#acl.delete}.
* @alias acl.default.delete
*/
var aclDefaultDelete = true;

/**
* Maps to {module:storage/bucket#acl.update}.
* @alias acl.default.update
*/
var aclDefaultUpdate = true;
/* jshint ignore:end */
/**
* Maps to {module:storage/bucket#acl.get}.
* @alias acl.default.get
*/
var aclDefaultGet = true;

/**
* Maps to {module:storage/bucket#acl.update}.
* @alias acl.default.update
*/
var aclDefaultUpdate = true;
/* jshint ignore:end */
}

/**
* Delete the bucket.
Expand Down
45 changes: 24 additions & 21 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,36 @@ function File(bucket, name, metadata) {

this.bucket = bucket;
this.makeReq_ = bucket.makeReq_.bind(bucket);
this.acl = new Acl(this);
this.metadata = metadata || {};

Object.defineProperty(this, 'name', {
enumerable: true,
value: name
});
}

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

/**
* Copy this file to another file. By default, this will copy the file to the
Expand Down

0 comments on commit aada90d

Please sign in to comment.