@@ -874,9 +874,10 @@ class Bynder {
874
874
* @param {Number } file.length - The length of the file to be uploaded
875
875
* @param {string } endpoint - S3 endpoint url
876
876
* @param {Object } init - Result from init upload
877
+ * @param {progressCallback } [progressCallback] - Function which is called anytime there is a progress update
877
878
* @return {Promise }
878
879
*/
879
- uploadFileInChunks ( file , endpoint , init ) {
880
+ uploadFileInChunks ( file , endpoint , init , progressCallback ) {
880
881
const { body } = file ;
881
882
const bodyType = bodyTypes . get ( body ) ;
882
883
const length = getLength ( file ) ;
@@ -918,6 +919,14 @@ class Bynder {
918
919
} ) ;
919
920
}
920
921
922
+ progressCallback ( {
923
+ action : 'Uploading file' ,
924
+ completed : 'Initializing' ,
925
+ chunksUploaded : 0 ,
926
+ chunks,
927
+ } ) ;
928
+
929
+
921
930
// sequentially upload chunks to AWS, then register them
922
931
function nextChunk ( chunkNumber ) {
923
932
if ( chunkNumber >= chunks ) {
@@ -931,6 +940,12 @@ class Bynder {
931
940
// our read stream is not done yet reading
932
941
// let's wait for a while...
933
942
return delay ( 50 ) . then ( ( ) => {
943
+ progressCallback ( {
944
+ action : 'Uploading file' ,
945
+ completed : 'Initializing' ,
946
+ chunksUploaded : chunkNumber ,
947
+ chunks,
948
+ } ) ;
934
949
return nextChunk ( chunkNumber ) ;
935
950
} ) ;
936
951
}
@@ -946,12 +961,28 @@ class Bynder {
946
961
return registerChunk ( init , newChunkNumber ) ;
947
962
} )
948
963
. then ( ( ) => {
964
+ progressCallback ( {
965
+ action : 'Uploading file' ,
966
+ completed : 'Initializing' ,
967
+ chunksUploaded : chunkNumber ,
968
+ chunks,
969
+ } ) ;
949
970
return nextChunk ( newChunkNumber ) ;
950
971
} ) ;
951
972
}
952
973
return nextChunk ( 0 ) ;
953
974
}
954
975
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
+ */
955
986
/**
956
987
* Uploads an arbitrarily sized buffer or stream file and returns the uploaded asset information
957
988
* @see {@link https://bynder.docs.apiary.io/#reference/upload-assets }
@@ -961,9 +992,10 @@ class Bynder {
961
992
* @param {Number } file.length - The length of the file to be uploaded
962
993
* @param {Object } file.data={} - An object containing the assets' attributes
963
994
* @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
964
996
* @return {Promise } The information of the uploaded file, including IDs and all final file urls.
965
997
*/
966
- uploadFile ( file ) {
998
+ uploadFile ( file , progressCallback = ( ) => { } ) {
967
999
const { body, filename, data, additional } = file ;
968
1000
const { brandId } = data ;
969
1001
const bodyType = bodyTypes . get ( body ) ;
@@ -991,14 +1023,26 @@ class Bynder {
991
1023
const finaliseUpload = this . finaliseUpload . bind ( this ) ;
992
1024
const saveAsset = this . saveAsset . bind ( this ) ;
993
1025
const waitForUploadDone = this . waitForUploadDone . bind ( this ) ;
1026
+ let totalChunks ;
994
1027
1028
+ progressCallback ( {
1029
+ action : 'Initializing' ,
1030
+ chunksUploaded : 0 ,
1031
+ } ) ;
995
1032
return Promise . all ( [ getClosestUploadEndpoint ( ) , initUpload ( filename ) ] )
996
1033
. then ( res => {
997
1034
const [ endpoint , init ] = res ;
998
- return uploadFileInChunks ( file , endpoint , init ) ;
1035
+ return uploadFileInChunks ( file , endpoint , init , progressCallback ) ;
999
1036
} )
1000
1037
. then ( uploadResponse => {
1001
1038
const { init, chunkNumber } = uploadResponse ;
1039
+ totalChunks = chunkNumber ;
1040
+ progressCallback ( {
1041
+ action : 'Finalizing upload' ,
1042
+ completed : 'Uploading file' ,
1043
+ chunksUploaded : chunkNumber ,
1044
+ chunks : chunkNumber ,
1045
+ } ) ;
1002
1046
return finaliseUpload ( init , filename , chunkNumber , additional ? data . id : null ) ;
1003
1047
} ) . then ( finalizeResponse => {
1004
1048
if ( additional ) {
@@ -1013,6 +1057,12 @@ class Bynder {
1013
1057
}
1014
1058
const { itemsDone } = doneResponse ;
1015
1059
const importId = itemsDone [ 0 ] ;
1060
+ progressCallback ( {
1061
+ action : 'Saving asset' ,
1062
+ completed : 'Finalizing upload' ,
1063
+ chunksUploaded : totalChunks ,
1064
+ chunks : totalChunks ,
1065
+ } ) ;
1016
1066
return saveAsset ( Object . assign ( data , { importId } ) ) ;
1017
1067
} ) ;
1018
1068
}
0 commit comments