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

storage: Don't provide multiple methods for file writing #70

Merged
merged 4 commits into from
Jul 31, 2014
Merged
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
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,21 @@ 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);
// Uploads file.pdf.
bucket.write(name, {
filename: '/path/to/file.pdf',
metadata: { /* metadata properties */ }
}, callback);

// Reads the stream and uploads it as file contents
bucket.writeStream(
filename, fs.createReadStream('/path/to/file.pdf'), metadata, callback);
// Uploads the readable stream.
bucket.write(name, {
data: anyReadableStream,
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
Expand Down
47 changes: 16 additions & 31 deletions lib/storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,25 @@ 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 {string=} opts.filename Path of the source file.
* @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 (opts.filename) {
stream = fs.createReadStream(opts.filename);
} else if (opts.data && (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({
Expand Down Expand Up @@ -298,34 +306,11 @@ Bucket.prototype.writeStream = function(name, stream, metadata, 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);
});
};

/**
* 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
Expand Down