Skip to content

Commit

Permalink
storage: use default method options for File class
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Nov 24, 2015
1 parent 8b6c450 commit c466911
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 336 deletions.
234 changes: 95 additions & 139 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,35 @@ var STORAGE_UPLOAD_BASE_URL = 'https://www.googleapis.com/upload/storage/v1/b';
* var file = myBucket.file('my-file');
*/
function File(bucket, name, options) {
options = options || {};

var requestQueryObject = {};
var generation = parseInt(options.generation, 10);

if (generation) {
requestQueryObject.generation = generation;
}

var methods = {
/**
* Delete the file.
*
* @resource [Objects: delete API Documentation]{@link https://cloud.google.com/storage/docs/json_api/v1/objects/delete}
*
* @param {function=} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* file.delete(function(err, apiResponse) {});
*/
delete: {
reqOpts: {
qs: requestQueryObject
}
},

/**
* Check if the file exists.
*
Expand All @@ -126,7 +154,72 @@ function File(bucket, name, options) {
* // file.metadata` has been populated.
* });
*/
get: true
get: true,

/**
* Get the file's metadata.
*
* @resource [Objects: get API Documentation]{@link https://cloud.google.com/storage/docs/json_api/v1/objects/get}
*
* @param {function=} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {object} callback.metadata - The File's metadata.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* file.getMetadata(function(err, metadata, apiResponse) {});
*/
getMetadata: {
reqOpts: {
qs: requestQueryObject
}
},

/**
* Merge the given metadata with the current remote file's metadata. This
* will set metadata if it was previously unset or update previously set
* metadata. To unset previously set metadata, set its value to null.
*
* You can set custom key/value pairs in the metadata key of the given
* object, however the other properties outside of this object must adhere
* to the [official API documentation](https://goo.gl/BOnnCK).
*
* See the examples below for more information.
*
* @resource [Objects: patch API Documentation]{@link https://cloud.google.com/storage/docs/json_api/v1/objects/patch}
*
* @param {object} metadata - The metadata you wish to update.
* @param {function=} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* file.setMetadata({
* contentType: 'application/x-font-ttf',
* metadata: {
* my: 'custom',
* properties: 'go here'
* }
* }, function(err, apiResponse) {});
*
* // Assuming current metadata = { hello: 'world', unsetMe: 'will do' }
* file.setMetadata({
* metadata: {
* abc: '123', // will be set.
* unsetMe: null, // will be unset (deleted).
* hello: 'goodbye' // will be updated from 'hello' to 'goodbye'.
* }
* }, function(err, apiResponse) {
* // metadata should now be { abc: '123', hello: 'goodbye' }
* });
*/
setMetadata: {
reqOpts: {
qs: requestQueryObject
}
}
};

ServiceObject.call(this, {
Expand All @@ -139,7 +232,7 @@ function File(bucket, name, options) {
options = options || {};

this.bucket = bucket;
this.generation = parseInt(options.generation, 10);
this.generation = generation;
this.storage = bucket.parent;

Object.defineProperty(this, 'name', {
Expand Down Expand Up @@ -772,41 +865,6 @@ File.prototype.createWriteStream = function(options) {
return stream;
};

/**
* Delete the file.
*
* @resource [Objects: delete API Documentation]{@link https://cloud.google.com/storage/docs/json_api/v1/objects/delete}
*
* @param {function=} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request
* @param {object} callback.apiResponse - The full API response.
*
* @example
* file.delete(function(err, apiResponse) {});
*/
File.prototype.delete = function(callback) {
callback = callback || util.noop;

var query = {};

if (this.generation) {
query.generation = this.generation;
}

this.request({
method: 'DELETE',
uri: '',
qs: query
}, function(err, resp) {
if (err) {
callback(err, resp);
return;
}

callback(null, resp);
});
};

/**
* Convenience method to download a file into memory or to a local destination.
*
Expand Down Expand Up @@ -858,43 +916,6 @@ File.prototype.download = function(options, callback) {
}
};

/**
* Get the file's metadata.
*
* @resource [Objects: get API Documentation]{@link https://cloud.google.com/storage/docs/json_api/v1/objects/get}
*
* @param {function=} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request
* @param {object} callback.metadata - The File's metadata.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* file.getMetadata(function(err, metadata, apiResponse) {});
*/
File.prototype.getMetadata = function(callback) {
var self = this;

callback = callback || util.noop;

var query = {};
if (this.generation) {
query.generation = this.generation;
}

this.request({
uri: '',
qs: query
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

self.metadata = resp;
callback(null, self.metadata, resp);
});
};

/**
* Get a signed policy document to allow a user to upload data with a POST
* request.
Expand Down Expand Up @@ -1389,71 +1410,6 @@ File.prototype.move = function(destination, callback) {
});
};

/**
* Merge the given metadata with the current remote file's metadata. This will
* set metadata if it was previously unset or update previously set metadata. To
* unset previously set metadata, set its value to null.
*
* You can set custom key/value pairs in the metadata key of the given object,
* however the other properties outside of this object must adhere to the
* [official API documentation](https://goo.gl/BOnnCK).
*
* See the examples below for more information.
*
* @resource [Objects: patch API Documentation]{@link https://cloud.google.com/storage/docs/json_api/v1/objects/patch}
*
* @param {object} metadata - The metadata you wish to update.
* @param {function=} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request
* @param {object} callback.apiResponse - The full API response.
*
* @example
* file.setMetadata({
* contentType: 'application/x-font-ttf',
* metadata: {
* my: 'custom',
* properties: 'go here'
* }
* }, function(err, apiResponse) {});
*
* // Assuming current metadata = { hello: 'world', unsetMe: 'will do' }
* file.setMetadata({
* metadata: {
* abc: '123', // will be set.
* unsetMe: null, // will be unset (deleted).
* hello: 'goodbye' // will be updated from 'hello' to 'goodbye'.
* }
* }, function(err, apiResponse) {
* // metadata should now be { abc: '123', hello: 'goodbye' }
* });
*/
File.prototype.setMetadata = function(metadata, callback) {
var self = this;

callback = callback || util.noop;

var query = {};
if (this.generation) {
query.generation = this.generation;
}

this.request({
method: 'PATCH',
uri: '',
qs: query,
json: metadata
}, function(err, resp) {
if (err) {
callback(err, resp);
return;
}

self.metadata = resp;

callback(null, resp);
});
};

/**
* This creates a gcs-resumable-upload upload stream.
*
Expand Down
Loading

0 comments on commit c466911

Please sign in to comment.