Skip to content

Commit 6cd5692

Browse files
committed
Merge pull request #70 from rakyll/writeinterfaces
storage: Don't provide multiple methods for file writing
2 parents 5e16d23 + c3400f2 commit 6cd5692

File tree

2 files changed

+29
-39
lines changed

2 files changed

+29
-39
lines changed

README.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,21 @@ A bucket object allows you to write a readable stream, a file and a buffer
341341
as file contents.
342342

343343
~~~~ js
344-
// Uploads file.pdf
345-
bucket.writeFile(
346-
filename, '/path/to/file.pdf', { contentType: 'application/pdf' }, callback);
344+
// Uploads file.pdf.
345+
bucket.write(name, {
346+
filename: '/path/to/file.pdf',
347+
metadata: { /* metadata properties */ }
348+
}, callback);
347349

348-
// Reads the stream and uploads it as file contents
349-
bucket.writeStream(
350-
filename, fs.createReadStream('/path/to/file.pdf'), metadata, callback);
350+
// Uploads the readable stream.
351+
bucket.write(name, {
352+
data: anyReadableStream,
353+
metadata: { /* metadata properties */ }
354+
}, callback);
351355

352-
// Uploads 'Hello World' as file contents
353-
bucket.writeBuffer(filename, 'Hello World', callback);
356+
// Uploads 'Hello World' as file contents.
357+
// data could be any string or buffer.
358+
bucket.write(name, { data: 'Hello World' }, callback);
354359
~~~~
355360

356361
#### Copy files

lib/storage/index.js

+16-31
Original file line numberDiff line numberDiff line change
@@ -254,17 +254,25 @@ Bucket.prototype.createReadStream = function(name) {
254254
/**
255255
* Writes the provided stream to the destination
256256
* with optional metadata.
257-
* @param {String} name Name of the remote file.
258-
* @param {Stream} stream A readable stream.
259-
* @param {Object?} metadata Optional metadata.
257+
* @param {String} name Name of the remote file.
258+
* @param {Object=} opts.data A string, buffer or readable stream.
259+
* @param {string=} opts.filename Path of the source file.
260+
* @param {Object=} opts.metadata Optional metadata.
260261
* @param {Function} callback Callback function.
261262
*/
262-
Bucket.prototype.writeStream = function(name, stream, metadata, callback) {
263-
if (!callback) {
264-
callback = metadata, metadata = {};
263+
Bucket.prototype.write = function(name, opts, callback) {
264+
// TODO(jbd): Support metadata only requests.
265+
var that = this;
266+
267+
var metadata = opts.metadata || {};
268+
var stream = opts.data;
269+
270+
if (opts.filename) {
271+
stream = fs.createReadStream(opts.filename);
272+
} else if (opts.data && (typeof opts.data === 'string' || opts.data instanceof Buffer)) {
273+
stream = new BufferStream(opts.data);
265274
}
266275

267-
var that = this;
268276
var boundary = uuid.v4();
269277
metadata.contentType = metadata.contentType || 'text/plain'
270278
this.conn.createAuthorizedReq({
@@ -298,34 +306,11 @@ Bucket.prototype.writeStream = function(name, stream, metadata, callback) {
298306
remoteStream.write('Content-Type: ' + metadata.contentType + '\n\n');
299307
stream.pipe(remoteStream);
300308
// TODO(jbd): High potential of multiple callback invokes.
301-
reqStreamToCallback(stream, callback);
309+
stream.on('error', callback);
302310
reqStreamToCallback(remoteStream, callback);
303311
});
304312
};
305313

306-
/**
307-
* Writes the source file to the destination with
308-
* optional metadata.
309-
* @param {String} name Name of the remote file.
310-
* @param {String} filename Path to the source file.
311-
* @param {object?} metadata Optional metadata.
312-
* @param {Function} callback Callback function.
313-
*/
314-
Bucket.prototype.writeFile = function(name, filename, metadata, callback) {
315-
this.writeStream(name, fs.createReadStream(filename), metadata, callback);
316-
};
317-
318-
/**
319-
* Writes the provided buffer to the destination file.
320-
* @param {String} name Name of the remote file resource.
321-
* @param {Buffer} buffer Buffer contents to be written.
322-
* @param {Object?} metadata Optional metadata.
323-
* @param {Function} callback Callback function.
324-
*/
325-
Bucket.prototype.writeBuffer = function(name, buffer, metadata, callback) {
326-
this.writeStream(name, new BufferStream(buffer), metadata, callback);
327-
};
328-
329314
/**
330315
* Makes a new request object from the provided
331316
* arguments, and wraps the callback to intercept

0 commit comments

Comments
 (0)