Skip to content

Commit da7543a

Browse files
committed
Updated uploadFile readme section
1 parent 773e943 commit da7543a

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,52 @@ this.camera.capture()
479479
.catch(err => console.error(err));
480480
```
481481

482+
To combine the `react-native-camera` plugin with firestack, we recommend setting the `captureTarget` to the `temp` storage path, like so:
483+
484+
```javascript
485+
<Camera
486+
ref={(cam) => {
487+
this.camera = cam;
488+
}}
489+
captureTarget={Camera.constants.CaptureTarget.temp}
490+
style={styles.preview}
491+
aspect={Camera.constants.Aspect.fill}>
492+
<Text style={styles.capture} onPress={this.takePicture.bind(this)}>[CAPTURE]</Text>
493+
</Camera>
494+
```
495+
496+
Firestack also gives you the ability to listen for database events on upload. The final parameter the `uploadFile()` function accepts is a callback that will be called anytime a storage event is fired.
497+
498+
The following events are supported:
499+
500+
* upload_progress
501+
* upload_paused
502+
* upload_resumed
503+
504+
For example, the `takePicture` function from the example above might look something similar to:
505+
506+
```javascript
507+
takePicture() {
508+
this.camera.capture()
509+
.then(({path}) => {
510+
const filename = 'photo.jpg'
511+
firestack.uploadFile(`photos/${filename}`, path, {
512+
contentType: 'image/jpeg',
513+
contentEncoding: 'base64',
514+
}, (evt) => {
515+
console.log('Got an event in JS', evt);
516+
})
517+
.then((res) => {
518+
console.log('result from upload file: ', res);
519+
})
520+
.catch((err) => {
521+
console.log('error happened with uploadFile', err);
522+
})
523+
})
524+
.catch(err => console.error(err));
525+
}
526+
```
527+
482528
#### storage attribute
483529

484530
To retrieve a stored file, we can get the url to download it from using the `storage` attribute. This method allows us to call right through to the native JavaScript object provided by the Firebase library:

ios/Firestack/FirestackStorage.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,32 @@ @implementation FirestackStorage
4141
[uploadTask observeStatus:FIRStorageTaskStatusResume handler:^(FIRStorageTaskSnapshot *snapshot) {
4242
// Upload resumed, also fires when the upload starts
4343
[self sendJSEvent:STORAGE_UPLOAD_RESUMED props:@{
44-
@"ref": snapshot.reference.bucket
45-
}];
44+
@"eventName": STORAGE_UPLOAD_RESUMED,
45+
@"ref": snapshot.reference.bucket
46+
}];
4647
}];
4748

4849
[uploadTask observeStatus:FIRStorageTaskStatusPause handler:^(FIRStorageTaskSnapshot *snapshot) {
4950
// Upload paused
5051
[self sendJSEvent:STORAGE_UPLOAD_PAUSED props:@{
51-
@"ref": snapshot.reference.bucket
52-
}];
52+
@"eventName": STORAGE_UPLOAD_PAUSED,
53+
@"ref": snapshot.reference.bucket
54+
}];
5355
}];
5456
[uploadTask observeStatus:FIRStorageTaskStatusProgress handler:^(FIRStorageTaskSnapshot *snapshot) {
5557
// Upload reported progress
5658
double percentComplete = 100.0 * (snapshot.progress.completedUnitCount) / (snapshot.progress.totalUnitCount);
5759

5860
[self sendJSEvent:STORAGE_UPLOAD_PROGRESS props:@{
59-
@"progress": @(percentComplete || 0.0)
60-
}];
61+
@"eventName": STORAGE_UPLOAD_PROGRESS,
62+
@"progress": @(percentComplete || 0.0)
63+
}];
6164

6265
}];
6366

6467
[uploadTask observeStatus:FIRStorageTaskStatusSuccess handler:^(FIRStorageTaskSnapshot *snapshot) {
68+
[uploadTask removeAllObservers];
69+
6570
// Upload completed successfully
6671
FIRStorageReference *ref = snapshot.reference;
6772
NSDictionary *props = @{

lib/modules/storage.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,29 @@ export class Storage extends Base {
2626
* @param {object} metadata An object containing metadata
2727
* @return {Promise}
2828
*/
29-
uploadFile(name, filepath, metadata={}) {
29+
uploadFile(name, filepath, metadata={}, cb) {
30+
let callback = cb;
31+
if (!callback || typeof callback !== 'function') {
32+
callback = (evt) => console.log('default callback', evt);
33+
}
34+
35+
const listeners = [];
36+
listeners.push(this._addListener('upload_progress', callback));
37+
listeners.push(this._addListener('upload_paused', callback));
38+
listeners.push(this._addListener('upload_resumed', callback));
3039
return promisify('uploadFile', FirestackStorage)(this.storageUrl, name, filepath, metadata)
40+
.then((res) => {
41+
listeners.forEach(this._removeListener);
42+
return res;
43+
});
44+
}
45+
46+
_addListener(evt, cb) {
47+
return FirestackStorageEvt.addListener(evt, cb);
48+
}
49+
50+
_removeListener(evt) {
51+
return FirestackStorageEvt.removeListener(evt);
3152
}
3253

3354
setStorageUrl(url) {

0 commit comments

Comments
 (0)