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

Commit 372c66f

Browse files
Extract storage-related functions into a new separate module #761
1 parent 933c709 commit 372c66f

File tree

14 files changed

+573
-549
lines changed

14 files changed

+573
-549
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ src/platforms/android/include.gradle
1616
!src/index.d.ts
1717
!src/references.d.ts
1818
!src/analytics/analytics.d.ts
19+
!src/storage/storage.d.ts
1920
!src/mlkit/*/index.d.ts
2021
!src/mlkit/mlkit-cameraview.d.ts
2122
!src/platforms/web/typings/firebase-webapi.d.ts

demo/app/main-view-model.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import { Observable } from "tns-core-modules/data/observable";
22
import { alert, prompt } from "tns-core-modules/ui/dialogs";
33
import { ios as iosUtils } from "tns-core-modules/utils/utils";
44
import { isIOS } from "tns-core-modules/platform";
5-
import { AddEventListenerResult, User } from "nativescript-plugin-firebase";
6-
import * as fs from "tns-core-modules/file-system";
7-
85
import * as firebase from "nativescript-plugin-firebase";
6+
import { AddEventListenerResult, storage as firebaseStorage, User } from "nativescript-plugin-firebase";
7+
import * as fs from "tns-core-modules/file-system";
98

109
const firebaseWebApi = require("nativescript-plugin-firebase/app");
1110

@@ -356,7 +355,7 @@ export class HelloWorldModel extends Observable {
356355
childRef.getDownloadURL()
357356
.then(theUrl => {
358357
console.log("Download url: " + theUrl);
359-
this.set("storageFeedback", "Download URL: " + theUrl);
358+
this.set("storageFeedback", "Download URL logged to the console");
360359
})
361360
.catch(error => {
362361
console.log("Download error: " + error);
@@ -1374,7 +1373,7 @@ export class HelloWorldModel extends Observable {
13741373
const appPath = fs.knownFolders.currentApp().path;
13751374
const logoPath = appPath + "/images/telerik-logo.png";
13761375

1377-
firebase.uploadFile({
1376+
firebaseStorage.uploadFile({
13781377
remoteFullPath: 'uploads/images/telerik-logo-uploaded.png',
13791378
localFile: fs.File.fromPath(logoPath), // use this (a file-system module File object)
13801379
// localFullPath: logoPath, // or this, a full file path
@@ -1403,7 +1402,7 @@ export class HelloWorldModel extends Observable {
14031402
// this will create or overwrite a local file in the app's documents folder
14041403
const localLogoFile = documents.getFile("telerik-logo-downloaded.png");
14051404

1406-
firebase.downloadFile({
1405+
firebaseStorage.downloadFile({
14071406
remoteFullPath: 'uploads/images/telerik-logo-uploaded.png',
14081407
// localFile: localLogoFile // use this (a file-system module File object)
14091408
localFullPath: logoPath // or this, a full file path
@@ -1426,7 +1425,7 @@ export class HelloWorldModel extends Observable {
14261425
}
14271426

14281427
public doGetDownloadUrl(): void {
1429-
firebase.getDownloadUrl({
1428+
firebaseStorage.getDownloadUrl({
14301429
remoteFullPath: 'uploads/images/telerik-logo-uploaded.png'
14311430
}).then(
14321431
theUrl => {
@@ -1448,7 +1447,7 @@ export class HelloWorldModel extends Observable {
14481447
}
14491448

14501449
public doDeleteFile(): void {
1451-
firebase.deleteFile({
1450+
firebaseStorage.deleteFile({
14521451
remoteFullPath: 'uploads/images/telerik-logo-uploaded.png'
14531452
}).then(
14541453
theUrl => {

docs/STORAGE.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-plugin-firebase/master/docs/images/features/storage.png" height="85px" alt="Storage"/>
22

3+
> NOTE: since plugin version 6.2.0 you have to use `firebase.storage.xxx` instead of `firebase.xxx`, because I've extracted storage-related functions into a separate module.
34
45
## Enabling Storage
56
Since plugin version 3.4.0 you can use Firebase _Storage_ features.
@@ -62,7 +63,7 @@ You can either pass in a full local path to a file, or (as a convenience) use th
6263
var logoPath = appPath + "/res/telerik-logo.png";
6364

6465
// now upload the file with either of the options below:
65-
firebase.uploadFile({
66+
firebase.storage.uploadFile({
6667
// optional, can also be passed during init() as 'storageBucket' param so we can cache it (find it in the Firebase console)
6768
bucket: 'gs://n-plugin-test.appspot.com',
6869
// the full path of the file in your Firebase storage (folders will be created)
@@ -129,7 +130,7 @@ In this example we'll download the previously uploaded file to a certain path on
129130
var localLogoFile = documents.getFile("telerik-logo-downloaded.png");
130131

131132
// now download the file with either of the options below:
132-
firebase.downloadFile({
133+
firebase.storage.downloadFile({
133134
// optional, can also be passed during init() as 'storageBucket' param so we can cache it
134135
bucket: 'gs://n-plugin-test.appspot.com',
135136
// the full path of an existing file in your Firebase storage
@@ -179,7 +180,7 @@ In this example we'll determine the remote URL of the previously uploaded file.
179180
<summary>Native API</summary>
180181

181182
```js
182-
firebase.getDownloadUrl({
183+
firebase.storage.getDownloadUrl({
183184
// optional, can also be passed during init() as 'storageBucket' param so we can cache it
184185
bucket: 'gs://n-plugin-test.appspot.com',
185186
// the full path of an existing file in your Firebase storage
@@ -217,7 +218,7 @@ You can pass in remote file path to delete it.
217218
<summary>Native API</summary>
218219

219220
```js
220-
firebase.deleteFile({
221+
firebase.storage.deleteFile({
221222
// optional, can also be passed during init() as 'storageBucket' param so we can cache it
222223
bucket: 'gs://n-plugin-test.appspot.com',
223224
// the full path of an existing file in your Firebase storage

src/app/storage/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as firebase from "../../firebase";
2-
import { UploadFileResult } from "../../firebase";
31
import { File } from "tns-core-modules/file-system"
2+
import * as firebaseStorage from "../../storage/storage";
3+
import { UploadFileResult } from "../../storage/storage";
44

55
export module storage {
66

@@ -32,20 +32,20 @@ export module storage {
3232
}
3333

3434
delete(): Promise<void> {
35-
return firebase.deleteFile({
35+
return firebaseStorage.deleteFile({
3636
remoteFullPath: this.path
3737
});
3838
}
3939

4040
getDownloadURL(): Promise<string> {
41-
return firebase.getDownloadUrl({
41+
return firebaseStorage.getDownloadUrl({
4242
remoteFullPath: this.path
4343
});
4444
}
4545

4646
public put(data: File | string /* path */, metadata?: any /* ignored */): Promise<UploadTaskSnapshot> {
4747
return new Promise((resolve, reject) => {
48-
firebase.uploadFile({
48+
firebaseStorage.uploadFile({
4949
localFile: data instanceof File ? data : undefined,
5050
localFullPath: !(data instanceof File) ? data : undefined,
5151
remoteFullPath: this.path,
@@ -64,7 +64,7 @@ export module storage {
6464

6565
// Note that this is not part of the web API, but prolly super convenient ;)
6666
public download(downloadToPath: string): Promise<any> {
67-
return firebase.downloadFile({
67+
return firebaseStorage.downloadFile({
6868
localFullPath: downloadToPath,
6969
remoteFullPath: this.path
7070
});

src/firebase-common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { prompt } from "tns-core-modules/ui/dialogs";
22
import { getString, setString } from "tns-core-modules/application-settings";
33
import { firestore } from "./firebase";
44
import * as analytics from "./analytics/analytics";
5+
import * as storage from "./storage/storage";
56
import * as mlkit from "./mlkit";
67

78
// note that this implementation is overridden for iOS
@@ -12,12 +13,12 @@ export class FieldValue {
1213
export const firebase: any = {
1314
initialized: false,
1415
instance: null,
15-
storage: null,
1616
firebaseRemoteConfig: null,
1717
authStateListeners: [],
1818
_receivedNotificationCallback: null,
1919
_dynamicLinkCallback: null,
2020
analytics,
21+
storage,
2122
mlkit,
2223
firestore: {
2324
FieldValue

0 commit comments

Comments
 (0)