Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 08cf591

Browse files
#119 Feature Request: Upload Progress
1 parent 35b5c91 commit 08cf591

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ If version numbers __changed__, clean your platform folders to avoid build error
1111
- Android: 9.4.0
1212

1313
### New
14+
- [#119](#119) Upload Progress
15+
- [#120](#120) Chaining range types for `.query`
1416
- [#125](#125) Analytics API
1517

1618

docs/STORAGE.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ You can either pass in a full local path to a file, or (as a convenience) use th
5050
// option 1: a file-system module File object
5151
localFile: fs.File.fromPath(logoPath),
5252
// option 2: a full file path (ignored if 'localFile' is set)
53-
localFullPath: logoPath
53+
localFullPath: logoPath,
54+
// get notified of file upload progress
55+
onProgress: function(status) {
56+
console.log("Uploaded fraction: " + status.fractionCompleted);
57+
console.log("Percentage complete: " + status.percentageCompleted);
58+
}
5459
}).then(
5560
function (uploadedFile)
5661
console.log("File uploaded: " + JSON.stringify(uploadedFile));

firebase.android.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,18 @@ firebase.uploadFile = function (arg) {
10671067
}
10681068
});
10691069

1070+
var onProgressListener = new com.google.firebase.storage.OnProgressListener({
1071+
onProgress: function (snapshot) {
1072+
if (typeof(arg.onProgress) === "function") {
1073+
var fractionCompleted = snapshot.getBytesTransferred() / snapshot.getTotalByteCount();
1074+
arg.onProgress({
1075+
fractionCompleted: fractionCompleted,
1076+
percentageCompleted: Math.round(fractionCompleted * 100)
1077+
});
1078+
}
1079+
}
1080+
});
1081+
10701082
if (arg.localFile) {
10711083
if (typeof(arg.localFile) != "object") {
10721084
reject("localFile argument must be a File object; use file-system module to create one");
@@ -1083,7 +1095,8 @@ firebase.uploadFile = function (arg) {
10831095

10841096
var uploadDataTask = storageReference.putBytes(contents)
10851097
.addOnFailureListener(onFailureListener)
1086-
.addOnSuccessListener(onSuccessListener);
1098+
.addOnSuccessListener(onSuccessListener)
1099+
.addOnProgressListener(onProgressListener);
10871100

10881101
} else if (arg.localFullPath) {
10891102

@@ -1096,11 +1109,11 @@ firebase.uploadFile = function (arg) {
10961109
var localFileUrl = android.net.Uri.fromFile(new java.io.File(arg.localFullPath));
10971110
var uploadFileTask = storageReference.putFile(localFileUrl)
10981111
.addOnFailureListener(onFailureListener)
1099-
.addOnSuccessListener(onSuccessListener);
1112+
.addOnSuccessListener(onSuccessListener)
1113+
.addOnProgressListener(onProgressListener);
11001114

11011115
} else {
11021116
reject("One of localFile or localFullPath is required");
1103-
return;
11041117
}
11051118

11061119
} catch (ex) {

firebase.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,17 @@ declare module "nativescript-plugin-firebase" {
276276
/**
277277
* Option 2: a full file path (ignored if 'localFile' is set)
278278
*/
279-
localFullPath?: string
279+
localFullPath?: string;
280+
281+
/**
282+
* You can get updates during upload by passing a function, example:
283+
*
284+
* onProgress: function(status) {
285+
* console.log("Uploaded fraction: " + status.fractionCompleted);
286+
* console.log("Percentage complete: " + status.percentageCompleted);
287+
* }
288+
*/
289+
onProgress?: Function;
280290
}
281291

282292
export interface UploadFileResult {

firebase.ios.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,7 @@ firebase.uploadFile = function (arg) {
10951095
}
10961096

10971097
var fIRStorageReference = storageRef.child(arg.remoteFullPath);
1098+
var fIRStorageUploadTask = null;
10981099

10991100
if (arg.localFile) {
11001101
if (typeof(arg.localFile) != "object") {
@@ -1110,17 +1111,29 @@ firebase.uploadFile = function (arg) {
11101111
return;
11111112
}
11121113

1113-
var fIRStorageUploadDataTask = fIRStorageReference.putDataMetadataCompletion(contents, null, onCompletion);
1114+
fIRStorageUploadTask = fIRStorageReference.putDataMetadataCompletion(contents, null, onCompletion);
11141115

11151116
} else if (arg.localFullPath) {
11161117
var localFileUrl = NSURL.fileURLWithPath(arg.localFullPath);
1117-
var fIRStorageUploadFileTask = fIRStorageReference.putFileMetadataCompletion(localFileUrl, null, onCompletion);
1118+
fIRStorageUploadTask = fIRStorageReference.putFileMetadataCompletion(localFileUrl, null, onCompletion);
11181119

11191120
} else {
11201121
reject("One of localFile or localFullPath is required");
11211122
return;
11221123
}
11231124

1125+
if (fIRStorageUploadTask !== null) {
1126+
// Add a progress observer to an upload task
1127+
var fIRStorageHandle = fIRStorageUploadTask.observeStatusHandler(FIRStorageTaskStatusProgress, function(snapshot) {
1128+
if (!snapshot.error && typeof(arg.onProgress) === "function") {
1129+
arg.onProgress({
1130+
fractionCompleted: snapshot.progress.fractionCompleted,
1131+
percentageCompleted: Math.round(snapshot.progress.fractionCompleted * 100)
1132+
});
1133+
}
1134+
});
1135+
}
1136+
11241137
} catch (ex) {
11251138
console.log("Error in firebase.uploadFile: " + ex);
11261139
reject(ex);

0 commit comments

Comments
 (0)