Skip to content

Commit

Permalink
Merge pull request #70 from rakyll/writeinterfaces
Browse files Browse the repository at this point in the history
storage: Don't provide multiple methods for file writing
  • Loading branch information
silvolu committed Jul 31, 2014
2 parents 5e16d23 + c3400f2 commit 6cd5692
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 39 deletions.
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

0 comments on commit 6cd5692

Please sign in to comment.