From 99f464c5e6d2f0dbd96a74443242a7758caab5a3 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Wed, 30 Jul 2014 17:59:46 -0700 Subject: [PATCH 1/3] storage: Don't provide multiple methods for file writing. --- README.md | 17 ++++++++--------- lib/storage/index.js | 41 +++++++++++------------------------------ 2 files changed, 19 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index a835e2eecc3..3288052a48a 100644 --- a/README.md +++ b/README.md @@ -341,16 +341,15 @@ A bucket object allows you to write a readable stream, a file and a buffer as file contents. ~~~~ js -// Uploads file.pdf -bucket.writeFile( - filename, '/path/to/file.pdf', { contentType: 'application/pdf' }, callback); - -// Reads the stream and uploads it as file contents -bucket.writeStream( - filename, fs.createReadStream('/path/to/file.pdf'), metadata, callback); +// Uploads file.pdf, data could be any readable stream. +bucket.write(name, { + data: fs.createStream('/path/to/file.pdf'), + metadata: { /* metadata properties */ } +}, callback); -// Uploads 'Hello World' as file contents -bucket.writeBuffer(filename, 'Hello World', callback); +// Uploads 'Hello World' as file contents. +// data could be any string or buffer. +bucket.write(name, { data: 'Hello World' }, callback); ~~~~ #### Copy files diff --git a/lib/storage/index.js b/lib/storage/index.js index 819fa24c5b6..b66db445acb 100644 --- a/lib/storage/index.js +++ b/lib/storage/index.js @@ -254,17 +254,21 @@ Bucket.prototype.createReadStream = function(name) { /** * Writes the provided stream to the destination * with optional metadata. - * @param {String} name Name of the remote file. - * @param {Stream} stream A readable stream. - * @param {Object?} metadata Optional metadata. + * @param {String} name Name of the remote file. + * @param {Object} opts.data A string, buffer or readable stream. + * @param {Object=} opts.metadata Optional metadata. * @param {Function} callback Callback function. */ -Bucket.prototype.writeStream = function(name, stream, metadata, callback) { - if (!callback) { - callback = metadata, metadata = {}; +Bucket.prototype.write = function(name, opts, callback) { + // TODO(jbd): Support metadata only requests. + var that = this; + + var metadata = opts.metadata || {}; + var stream = opts.data; + if (typeof opts.data === 'string' || opts.data instanceof Buffer) { + stream = new BufferStream(opts.data); } - var that = this; var boundary = uuid.v4(); metadata.contentType = metadata.contentType || 'text/plain' this.conn.createAuthorizedReq({ @@ -303,29 +307,6 @@ Bucket.prototype.writeStream = function(name, stream, metadata, callback) { }); }; -/** - * Writes the source file to the destination with - * optional metadata. - * @param {String} name Name of the remote file. - * @param {String} filename Path to the source file. - * @param {object?} metadata Optional metadata. - * @param {Function} callback Callback function. - */ -Bucket.prototype.writeFile = function(name, filename, metadata, callback) { - this.writeStream(name, fs.createReadStream(filename), metadata, callback); -}; - -/** - * Writes the provided buffer to the destination file. - * @param {String} name Name of the remote file resource. - * @param {Buffer} buffer Buffer contents to be written. - * @param {Object?} metadata Optional metadata. - * @param {Function} callback Callback function. - */ -Bucket.prototype.writeBuffer = function(name, buffer, metadata, callback) { - this.writeStream(name, new BufferStream(buffer), metadata, callback); -}; - /** * Makes a new request object from the provided * arguments, and wraps the callback to intercept From d161ccff9e9bdb9f999115b00aa10dd8cd4c3a38 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Wed, 30 Jul 2014 18:19:26 -0700 Subject: [PATCH 2/3] storage: Stream from filename if it's provided. --- lib/storage/index.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/storage/index.js b/lib/storage/index.js index b66db445acb..a9e4fb4e213 100644 --- a/lib/storage/index.js +++ b/lib/storage/index.js @@ -254,9 +254,10 @@ Bucket.prototype.createReadStream = function(name) { /** * Writes the provided stream to the destination * with optional metadata. - * @param {String} name Name of the remote file. - * @param {Object} opts.data A string, buffer or readable stream. - * @param {Object=} opts.metadata Optional metadata. + * @param {String} name Name of the remote file. + * @param {Object=} opts.data A string, buffer or readable stream. + * @param {string=} opts.filename Path of the source file. + * @param {Object=} opts.metadata Optional metadata. * @param {Function} callback Callback function. */ Bucket.prototype.write = function(name, opts, callback) { @@ -265,7 +266,10 @@ Bucket.prototype.write = function(name, opts, callback) { var metadata = opts.metadata || {}; var stream = opts.data; - if (typeof opts.data === 'string' || opts.data instanceof Buffer) { + + if (opts.filename) { + stream = fs.createReadStream(opts.filename); + } else if (opts.data && (typeof opts.data === 'string' || opts.data instanceof Buffer)) { stream = new BufferStream(opts.data); } @@ -302,7 +306,7 @@ Bucket.prototype.write = function(name, opts, callback) { remoteStream.write('Content-Type: ' + metadata.contentType + '\n\n'); stream.pipe(remoteStream); // TODO(jbd): High potential of multiple callback invokes. - reqStreamToCallback(stream, callback); + stream.on('error', callback); reqStreamToCallback(remoteStream, callback); }); }; From c3400f27dc2570c21a6a469fc5af43a48f984185 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Wed, 30 Jul 2014 18:21:15 -0700 Subject: [PATCH 3/3] Update README with writing with filename option. --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 84eaf1fa9c4..5cd5472dd5e 100644 --- a/README.md +++ b/README.md @@ -341,9 +341,15 @@ A bucket object allows you to write a readable stream, a file and a buffer as file contents. ~~~~ js -// Uploads file.pdf, data could be any readable stream. +// Uploads file.pdf. bucket.write(name, { - data: fs.createStream('/path/to/file.pdf'), + filename: '/path/to/file.pdf', + metadata: { /* metadata properties */ } +}, callback); + +// Uploads the readable stream. +bucket.write(name, { + data: anyReadableStream, metadata: { /* metadata properties */ } }, callback);