Skip to content

Commit a4ac849

Browse files
elseeeElse ten Broeke
and
Else ten Broeke
authored
[API-844] Add support to return progress of upload (#116)
* Add support to return progress of upload * Undo rename * Add progress update to stream as well Co-authored-by: Else ten Broeke <[email protected]>
1 parent e27dcd7 commit a4ac849

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

samples/upload.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ bynder.getBrands()
1616
brandId: brand.id,
1717
name: 'test asset'
1818
}
19-
});
19+
}, console.log);
2020
})
2121
.then(console.log)
2222
.catch((error) => {

src/bynder-js-sdk.js

+53-3
Original file line numberDiff line numberDiff line change
@@ -874,9 +874,10 @@ class Bynder {
874874
* @param {Number} file.length - The length of the file to be uploaded
875875
* @param {string} endpoint - S3 endpoint url
876876
* @param {Object} init - Result from init upload
877+
* @param {progressCallback} [progressCallback] - Function which is called anytime there is a progress update
877878
* @return {Promise}
878879
*/
879-
uploadFileInChunks(file, endpoint, init) {
880+
uploadFileInChunks(file, endpoint, init, progressCallback) {
880881
const { body } = file;
881882
const bodyType = bodyTypes.get(body);
882883
const length = getLength(file);
@@ -918,6 +919,14 @@ class Bynder {
918919
});
919920
}
920921

922+
progressCallback({
923+
action: 'Uploading file',
924+
completed: 'Initializing',
925+
chunksUploaded: 0,
926+
chunks,
927+
});
928+
929+
921930
// sequentially upload chunks to AWS, then register them
922931
function nextChunk(chunkNumber) {
923932
if (chunkNumber >= chunks) {
@@ -931,6 +940,12 @@ class Bynder {
931940
// our read stream is not done yet reading
932941
// let's wait for a while...
933942
return delay(50).then(() => {
943+
progressCallback({
944+
action: 'Uploading file',
945+
completed: 'Initializing',
946+
chunksUploaded: chunkNumber,
947+
chunks,
948+
});
934949
return nextChunk(chunkNumber);
935950
});
936951
}
@@ -946,12 +961,28 @@ class Bynder {
946961
return registerChunk(init, newChunkNumber);
947962
})
948963
.then(() => {
964+
progressCallback({
965+
action: 'Uploading file',
966+
completed: 'Initializing',
967+
chunksUploaded: chunkNumber,
968+
chunks,
969+
});
949970
return nextChunk(newChunkNumber);
950971
});
951972
}
952973
return nextChunk(0);
953974
}
954975

976+
/**
977+
* Callback for adding two numbers.
978+
*
979+
* @callback progressCallback
980+
* @param {Object} state={} - An object containing the progress state
981+
* @param {String} state.action - The next action
982+
* @param {String} [state.completed] - The last completed action
983+
* @param {Number} [state.chunks] - Total amount of chunks
984+
* @param {Number} state.chunksUploaded - Amount of chunks already uploaded
985+
*/
955986
/**
956987
* Uploads an arbitrarily sized buffer or stream file and returns the uploaded asset information
957988
* @see {@link https://bynder.docs.apiary.io/#reference/upload-assets}
@@ -961,9 +992,10 @@ class Bynder {
961992
* @param {Number} file.length - The length of the file to be uploaded
962993
* @param {Object} file.data={} - An object containing the assets' attributes
963994
* @param {Boolean} file.additional - Boolean that signals if the asset should be added as additional to an existing asset
995+
* @param {progressCallback} [progressCallback] - Function which is called anytime there is a progress update
964996
* @return {Promise} The information of the uploaded file, including IDs and all final file urls.
965997
*/
966-
uploadFile(file) {
998+
uploadFile(file, progressCallback = () => {}) {
967999
const { body, filename, data, additional } = file;
9681000
const { brandId } = data;
9691001
const bodyType = bodyTypes.get(body);
@@ -991,14 +1023,26 @@ class Bynder {
9911023
const finaliseUpload = this.finaliseUpload.bind(this);
9921024
const saveAsset = this.saveAsset.bind(this);
9931025
const waitForUploadDone = this.waitForUploadDone.bind(this);
1026+
let totalChunks;
9941027

1028+
progressCallback({
1029+
action: 'Initializing',
1030+
chunksUploaded: 0,
1031+
});
9951032
return Promise.all([getClosestUploadEndpoint(), initUpload(filename)])
9961033
.then(res => {
9971034
const [endpoint, init] = res;
998-
return uploadFileInChunks(file, endpoint, init);
1035+
return uploadFileInChunks(file, endpoint, init, progressCallback);
9991036
})
10001037
.then(uploadResponse => {
10011038
const { init, chunkNumber } = uploadResponse;
1039+
totalChunks = chunkNumber;
1040+
progressCallback({
1041+
action: 'Finalizing upload',
1042+
completed: 'Uploading file',
1043+
chunksUploaded: chunkNumber,
1044+
chunks: chunkNumber,
1045+
});
10021046
return finaliseUpload(init, filename, chunkNumber, additional ? data.id : null);
10031047
}).then(finalizeResponse => {
10041048
if (additional) {
@@ -1013,6 +1057,12 @@ class Bynder {
10131057
}
10141058
const { itemsDone } = doneResponse;
10151059
const importId = itemsDone[0];
1060+
progressCallback({
1061+
action: 'Saving asset',
1062+
completed: 'Finalizing upload',
1063+
chunksUploaded: totalChunks,
1064+
chunks: totalChunks,
1065+
});
10161066
return saveAsset(Object.assign(data, { importId }));
10171067
});
10181068
}

0 commit comments

Comments
 (0)