@@ -254,17 +254,25 @@ Bucket.prototype.createReadStream = function(name) {
254
254
/**
255
255
* Writes the provided stream to the destination
256
256
* 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.
260
261
* @param {Function } callback Callback function.
261
262
*/
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 ) ;
265
274
}
266
275
267
- var that = this ;
268
276
var boundary = uuid . v4 ( ) ;
269
277
metadata . contentType = metadata . contentType || 'text/plain'
270
278
this . conn . createAuthorizedReq ( {
@@ -298,34 +306,11 @@ Bucket.prototype.writeStream = function(name, stream, metadata, callback) {
298
306
remoteStream . write ( 'Content-Type: ' + metadata . contentType + '\n\n' ) ;
299
307
stream . pipe ( remoteStream ) ;
300
308
// TODO(jbd): High potential of multiple callback invokes.
301
- reqStreamToCallback ( stream , callback ) ;
309
+ stream . on ( 'error' , callback ) ;
302
310
reqStreamToCallback ( remoteStream , callback ) ;
303
311
} ) ;
304
312
} ;
305
313
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
-
329
314
/**
330
315
* Makes a new request object from the provided
331
316
* arguments, and wraps the callback to intercept
0 commit comments