@@ -4,15 +4,6 @@ const CloudControllerBase = require("./CloudControllerBase");
4
4
const rest = require ( "restler" ) ; //TODO: Analyze a way to remove this dependency
5
5
const fs = require ( "fs" ) ;
6
6
7
- /**
8
- * Is it a ReadableStream?
9
- * @param {Object } [source] the source of information
10
- * @returns {Boolean } true is the argument is a ReadableStream; otherwise, false
11
- */
12
- function isReadableStream ( source ) {
13
- return source && typeof source . pipe === 'function' ;
14
- }
15
-
16
7
/**
17
8
* This public class manages the operations related with Applications on Cloud Controller.
18
9
*/
@@ -301,12 +292,12 @@ class Apps extends CloudControllerBase {
301
292
* function File(path, filename, fileSize, encoding, contentType)
302
293
* 'application': rest.file(path, null, fileSizeInBytes, null, 'application/zip')
303
294
*
304
- * @param {String } appGuid [App guid]
305
- * @param {String|stream.ReadableStream } source [file path or readable stream to upload]
306
- * @param {Boolean } async [Sync/Async]
307
- * @return {JSON/String } [{}/Job information]
295
+ * @param {String } appGuid [App guid]
296
+ * @param {String } filePath [file path to upload]
297
+ * @param {Boolean } async [Sync/Async]
298
+ * @return {JSON/String } [{}/Job information]
308
299
*/
309
- upload ( appGuid , source , async ) {
300
+ upload ( appGuid , filePath , async ) {
310
301
311
302
const url = `${ this . API_URL } /v2/apps/${ appGuid } /bits` ;
312
303
const zipResources = [ ] ;
@@ -318,36 +309,60 @@ class Apps extends CloudControllerBase {
318
309
asyncFlag = true ;
319
310
}
320
311
}
312
+ var options = {
313
+ multipart : true ,
314
+ accessToken : this . UAA_TOKEN . access_token ,
315
+ query : {
316
+ guid : appGuid ,
317
+ async : asyncFlag
318
+ } ,
319
+ data : {
320
+ resources : JSON . stringify ( zipResources )
321
+ }
322
+ } ;
321
323
322
- let promise ;
323
- if ( typeof source === "string" || isReadableStream ( source ) ) {
324
- let options = {
325
- headers : {
326
- Authorization : `${ this . UAA_TOKEN . token_type } ${ this . UAA_TOKEN . access_token } `
327
- } ,
328
- qs : {
329
- async : asyncFlag
330
- }
331
- } ;
332
- promise = this . REST . uploadStream ( url , options , typeof source === "string" ? fs . createReadStream ( source ) : source , this . HttpStatus . CREATED , false ) ;
324
+ if ( typeof filePath === "string" ) {
325
+ stats = fs . statSync ( filePath ) ;
326
+ options . data . application = rest . file ( filePath , null , stats . size , null , "application/zip" ) ;
333
327
} else {
334
- let options = {
335
- multipart : true ,
336
- data : {
337
- resources : JSON . stringify ( zipResources )
338
- } ,
339
- application : rest . data ( null , "application/zip" , source . toBuffer ( ) ) ,
340
- query : {
341
- guid : appGuid ,
342
- async : asyncFlag
343
- }
344
- }
345
- promise = this . REST . upload ( url , options , this . HttpStatus . CREATED , false ) ;
328
+ options . data . application = rest . data ( null , "application/zip" , filePath . toBuffer ( ) ) ;
346
329
}
347
330
348
- return promise ;
331
+ return this . REST . upload ( url , options , this . HttpStatus . CREATED , false ) ;
349
332
}
350
333
334
+ /**
335
+ * Upload source code from a ReadableStream
336
+ * {@link http://apidocs.cloudfoundry.org/214/apps/uploads_the_bits_for_an_app.html}
337
+ *
338
+ *
339
+ * @param {String } appGuid [App guid]
340
+ * @param {stream.ReadableStream } stream [stream to upload]
341
+ * @param {Boolean } async [Sync/Async]
342
+ * @return {JSON/String } [{}/Job information]
343
+ */
344
+ uploadFromStream ( appGuid , stream , async ) {
345
+
346
+ const url = `${ this . API_URL } /v2/apps/${ appGuid } /bits` ;
347
+ const zipResources = [ ] ;
348
+ let asyncFlag = false ;
349
+ var stats = null ;
350
+
351
+ if ( typeof async === "boolean" ) {
352
+ if ( async ) {
353
+ asyncFlag = true ;
354
+ }
355
+ }
356
+ let options = {
357
+ headers : {
358
+ Authorization : `${ this . UAA_TOKEN . token_type } ${ this . UAA_TOKEN . access_token } `
359
+ } ,
360
+ qs : {
361
+ async : asyncFlag
362
+ }
363
+ } ;
364
+ return this . REST . uploadStream ( url , options , stream , this . HttpStatus . CREATED , false ) ;
365
+ }
351
366
352
367
/**
353
368
* Get Instances
0 commit comments