Skip to content

Commit 7c5e0d9

Browse files
committed
Add a unique function for handling uploading from a stream
1 parent a2e3860 commit 7c5e0d9

File tree

1 file changed

+53
-38
lines changed

1 file changed

+53
-38
lines changed

lib/model/cloudcontroller/Apps.js

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ const CloudControllerBase = require("./CloudControllerBase");
44
const rest = require("restler");//TODO: Analyze a way to remove this dependency
55
const fs = require("fs");
66

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-
167
/**
178
* This public class manages the operations related with Applications on Cloud Controller.
189
*/
@@ -301,12 +292,12 @@ class Apps extends CloudControllerBase {
301292
* function File(path, filename, fileSize, encoding, contentType)
302293
* 'application': rest.file(path, null, fileSizeInBytes, null, 'application/zip')
303294
*
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]
308299
*/
309-
upload (appGuid, source, async) {
300+
upload (appGuid, filePath, async) {
310301

311302
const url = `${this.API_URL}/v2/apps/${appGuid}/bits`;
312303
const zipResources = [];
@@ -318,36 +309,60 @@ class Apps extends CloudControllerBase {
318309
asyncFlag = true;
319310
}
320311
}
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+
};
321323

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");
333327
} 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());
346329
}
347330

348-
return promise;
331+
return this.REST.upload(url, options, this.HttpStatus.CREATED, false);
349332
}
350333

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+
}
351366

352367
/**
353368
* Get Instances

0 commit comments

Comments
 (0)