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

Commit d68b16a

Browse files
how can I delete a folder? #757
1 parent 4669bf5 commit d68b16a

8 files changed

Lines changed: 313 additions & 38 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# NativeScript Firebase plugin
22

3-
[![Build Status][build-status]][build-url]
43
[![NPM version][npm-image]][npm-url]
54
[![Downloads][downloads-image]][npm-url]
65
[![Twitter Follow][twitter-image]][twitter-url]

demo/app/main-page.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<TabViewItem.view>
1919
<ScrollView>
2020
<GridLayout columns="*, *"
21-
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
21+
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
2222
horizontalAlignment="stretch"
2323
class="tab-content">
2424
<Button row="0" colSpan="2" text="init firebase - do this first" tap="{{ doWebInit }}" class="button button-positive"/>
@@ -62,6 +62,16 @@
6262
<Label row="15" col="0" text="Value:" class="message"/>
6363
<Label row="15" col="1" text="{{ value }}" class="message" textWrap="true"/>
6464

65+
<Label row="16" colSpan="2" text="Google Cloud Storage" class="subtitle"/>
66+
67+
<Button row="17" col="0" text="upload file" tap="{{ doWebUploadFile }}" class="button button-storage"/>
68+
<Button row="17" col="1" text="download file" tap="{{ doWebDownloadFile }}" class="button button-storage"/>
69+
70+
<Button row="18" col="0" text="get download url" tap="{{ doWebGetDownloadUrl }}" class="button button-storage"/>
71+
<Button row="18" col="1" text="delete remote file" tap="{{ doWebDeleteFile }}" class="button button-storage"/>
72+
73+
<Label row="19" colSpan="2" text="{{ storageFeedback }}" class="message" textWrap="true"/>
74+
6575
</GridLayout>
6676
</ScrollView>
6777
</TabViewItem.view>

demo/app/main-view-model.ts

Lines changed: 96 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { isIOS } from "tns-core-modules/platform";
55
import { AddEventListenerResult, User } from "nativescript-plugin-firebase";
66
import * as fs from "tns-core-modules/file-system";
77

8-
import * as firebase from"nativescript-plugin-firebase";
8+
import * as firebase from "nativescript-plugin-firebase";
9+
910
const firebaseWebApi = require("nativescript-plugin-firebase/app");
1011

1112
declare const Crashlytics: any;
@@ -58,7 +59,9 @@ export class HelloWorldModel extends Observable {
5859
}
5960

6061
public doWebInit(): void {
61-
firebaseWebApi.initializeApp();
62+
firebaseWebApi.initializeApp({
63+
storageBucket: 'gs://n-plugin-test.appspot.com',
64+
});
6265
}
6366

6467
public doWebLoginAnonymously(): void {
@@ -307,6 +310,76 @@ export class HelloWorldModel extends Observable {
307310
});
308311
}
309312

313+
public doWebUploadFile(): void {
314+
// let's first create a File object using the tns file module
315+
const appPath = fs.knownFolders.currentApp().path;
316+
const logoPath = appPath + "/images/telerik-logo.png";
317+
318+
const storageRef = firebaseWebApi.storage().ref();
319+
const childRef = storageRef.child("uploads/images/telerik-logo-uploaded.png");
320+
321+
childRef.put(fs.File.fromPath(logoPath)).then(
322+
uploadedFile => {
323+
console.log("Uploaded! " + JSON.stringify(uploadedFile));
324+
this.set("storageFeedback", "Uploaded!");
325+
},
326+
error => {
327+
console.log("firebase.doWebUploadFile error: " + error);
328+
this.set("storageFeedback", "Error: " + error);
329+
}
330+
);
331+
}
332+
333+
public doWebDownloadFile(): void {
334+
const storageRef = firebaseWebApi.storage().ref();
335+
const childRef = storageRef.child("uploads/images/telerik-logo-uploaded.png");
336+
337+
// let's first determine where we'll create the file using the 'file-system' module
338+
const documents = fs.knownFolders.documents();
339+
const logoPath = documents.path + "/telerik-logo-downloaded.png";
340+
341+
childRef.download(logoPath)
342+
.then(() => {
343+
console.log("The file has been downloaded");
344+
this.set("storageFeedback", "The file has been downloaded");
345+
})
346+
.catch(error => {
347+
console.log("Download error: " + error);
348+
this.set("storageFeedback", "Error: " + error);
349+
});
350+
}
351+
352+
public doWebGetDownloadUrl(): void {
353+
const storageRef = firebaseWebApi.storage().ref();
354+
const childRef = storageRef.child("uploads/images/telerik-logo-uploaded.png");
355+
356+
childRef.getDownloadURL()
357+
.then(theUrl => {
358+
console.log("Download url: " + theUrl);
359+
this.set("storageFeedback", "Download URL: " + theUrl);
360+
})
361+
.catch(error => {
362+
console.log("Download error: " + error);
363+
this.set("storageFeedback", "Error: " + error);
364+
});
365+
}
366+
367+
public doWebDeleteFile(): void {
368+
const storageRef = firebaseWebApi.storage().ref();
369+
const childRef = storageRef.child("uploads/images/telerik-logo-uploaded.png");
370+
371+
childRef.delete().then(
372+
() => {
373+
console.log("Deleted file");
374+
this.set("storageFeedback", "File deleted");
375+
},
376+
error => {
377+
console.log("Error deleting file: " + error);
378+
this.set("storageFeedback", "Error deleting file: " + error);
379+
}
380+
);
381+
}
382+
310383

311384
/***********************************************
312385
* Native API usage examples
@@ -1376,6 +1449,27 @@ export class HelloWorldModel extends Observable {
13761449
);
13771450
}
13781451

1452+
public doDeleteFile(): void {
1453+
firebase.deleteFile({
1454+
remoteFullPath: 'uploads/images/telerik-logo-uploaded.png'
1455+
}).then(
1456+
theUrl => {
1457+
alert({
1458+
title: "File deleted",
1459+
message: "Enjoy your day!",
1460+
okButtonText: "Thanks ;)"
1461+
});
1462+
},
1463+
error => {
1464+
alert({
1465+
title: "File deletion error",
1466+
message: error,
1467+
okButtonText: "OK"
1468+
});
1469+
}
1470+
);
1471+
}
1472+
13791473
public doReauthenticatePwdUser(): void {
13801474
firebase.reauthenticate({
13811475
type: firebase.LoginType.PASSWORD,
@@ -1440,27 +1534,6 @@ export class HelloWorldModel extends Observable {
14401534
);
14411535
}
14421536

1443-
public doDeleteFile(): void {
1444-
firebase.deleteFile({
1445-
remoteFullPath: 'uploads/images/telerik-logo-uploaded.png'
1446-
}).then(
1447-
theUrl => {
1448-
alert({
1449-
title: "File deleted",
1450-
message: "Enjoy your day!",
1451-
okButtonText: "Thanks ;)"
1452-
});
1453-
},
1454-
error => {
1455-
alert({
1456-
title: "File deletion error",
1457-
message: error,
1458-
okButtonText: "OK"
1459-
});
1460-
}
1461-
);
1462-
}
1463-
14641537
public doSubscribeToTopic(): void {
14651538
firebase.subscribeToTopic("demo").then(
14661539
() => {

docs/STORAGE.md

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,40 @@ You can either pass it to the `init()` function as shown below,
2020
or pass it as a property any time you're using a storage feature.
2121
In theory the former is a little more efficient because it's cached by the plugin.
2222

23+
> Should I use the *Native API* or the *Web API*? Whichever syntax you like most - the underlying implementation is the same.
24+
25+
<details>
26+
<summary>Native API</summary>
27+
2328
```js
2429
firebase.init({
2530
storageBucket: 'gs://n-plugin-test.appspot.com'
2631
// any other options
2732
});
2833
```
34+
</details>
35+
36+
<details>
37+
<summary>Web API</summary>
38+
39+
```js
40+
firebaseWebApi.initializeApp({
41+
storageBucket: 'gs://n-plugin-test.appspot.com'
42+
});
43+
```
44+
</details>
2945

3046
## Functions
3147

3248
### uploadFile
3349
You can either pass in a full local path to a file, or (as a convenience) use the `file-system` module that comes shipped with {N} as standard.
3450

51+
<details>
52+
<summary>Native API</summary>
53+
3554
```js
3655
// init the file-system module
37-
var fs = require("file-system");
56+
var fs = require("tns-core-modules/file-system");
3857

3958
// grab a reference to the app folder
4059
var appPath = fs.knownFolders.currentApp().path;
@@ -66,15 +85,41 @@ You can either pass in a full local path to a file, or (as a convenience) use th
6685
}
6786
);
6887
```
88+
</details>
89+
90+
<details>
91+
<summary>Web API</summary>
92+
93+
#### TypeScript
94+
95+
```typescript
96+
import * as fs from "tns-core-modules/file-system";
97+
98+
// let's first create a File object using the tns file module
99+
const appPath = fs.knownFolders.currentApp().path;
100+
const logoPath = appPath + "/images/telerik-logo.png";
101+
102+
const storageRef = firebaseWebApi.storage().ref();
103+
const childRef = storageRef.child("uploads/images/telerik-logo-uploaded.png");
104+
105+
childRef.put(fs.File.fromPath(logoPath)).then(
106+
uploadedFile => console.log("Uploaded! " + JSON.stringify(uploadedFile)),
107+
error => console.log("firebase.doWebUploadFile error: " + error)
108+
);
109+
```
110+
</details>
69111

70112
### downloadFile
71113
As with `uploadFile` you can either pass in a full local path to a file, or (as a convenience) use the `file-system` module that comes shipped with {N} as standard.
72114

73115
In this example we'll download the previously uploaded file to a certain path on the local filesystem.
74116

117+
<details>
118+
<summary>Native API</summary>
119+
75120
```js
76121
// init the file-system module
77-
var fs = require("file-system");
122+
var fs = require("tns-core-modules/file-system");
78123

79124
// let's first determine where we'll create the file using the 'file-system' module
80125
var documents = fs.knownFolders.documents();
@@ -102,12 +147,37 @@ In this example we'll download the previously uploaded file to a certain path on
102147
}
103148
);
104149
```
150+
</details>
151+
152+
<details>
153+
<summary>Web API</summary>
154+
155+
#### TypeScript
156+
157+
```typescript
158+
import * as fs from "tns-core-modules/file-system";
159+
160+
const storageRef = firebaseWebApi.storage().ref();
161+
const childRef = storageRef.child("uploads/images/telerik-logo-uploaded.png");
162+
163+
// let's first determine where we'll create the file using the 'file-system' module
164+
const documents = fs.knownFolders.documents();
165+
const logoPath = documents.path + "/telerik-logo-downloaded.png";
166+
167+
childRef.download(logoPath)
168+
.then(() => console.log("The file has been downloaded"))
169+
.catch(error => console.log("Download error: " + error));
170+
```
171+
</details>
105172

106173
### getDownloadUrl
107174
If you just want to know the remote URL of a file in remote storage so you can either share it or download the file by any other means than `downloadFile` then use this method.
108175

109176
In this example we'll determine the remote URL of the previously uploaded file.
110177

178+
<details>
179+
<summary>Native API</summary>
180+
111181
```js
112182
firebase.getDownloadUrl({
113183
// optional, can also be passed during init() as 'storageBucket' param so we can cache it
@@ -123,10 +193,29 @@ In this example we'll determine the remote URL of the previously uploaded file.
123193
}
124194
);
125195
```
196+
</details>
197+
198+
<details>
199+
<summary>Web API</summary>
200+
201+
#### TypeScript
202+
203+
```typescript
204+
const storageRef = firebaseWebApi.storage().ref();
205+
const childRef = storageRef.child("uploads/images/telerik-logo-uploaded.png");
206+
207+
childRef.getDownloadURL()
208+
.then(theUrl => console.log("Download url: " + theUrl))
209+
.catch(error => console.log("Download error: " + error));
210+
```
211+
</details>
126212

127213
### deleteFile
128214
You can pass in remote file path to delete it.
129215

216+
<details>
217+
<summary>Native API</summary>
218+
130219
```js
131220
firebase.deleteFile({
132221
// optional, can also be passed during init() as 'storageBucket' param so we can cache it
@@ -142,3 +231,20 @@ You can pass in remote file path to delete it.
142231
}
143232
);
144233
```
234+
</details>
235+
236+
<details>
237+
<summary>Web API</summary>
238+
239+
#### TypeScript
240+
241+
```typescript
242+
const storageRef = firebaseWebApi.storage().ref();
243+
const childRef = storageRef.child("uploads/images/telerik-logo-uploaded.png");
244+
245+
childRef.delete().then(
246+
() => console.log("Deleted file"),
247+
error => console.log("Error deleting file: " + error)
248+
);
249+
```
250+
</details>

src/app/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as firebase from "../firebase";
77
import { auth as firebaseAuthModule } from "./auth";
88
import { database as firebaseDatabaseModule } from "./database";
99
import { firestore as firebaseFirestoreModule } from "./firestore";
10+
import { storage as firebaseStorageModule } from "./storage";
1011

1112
export function initializeApp(options?: firebase.InitOptions, name? /* ignored */: string): Promise<any> {
1213
return firebase.init(options);
@@ -23,7 +24,6 @@ export function auth(app?: any): firebaseAuthModule.Auth {
2324
return authCache;
2425
}
2526

26-
2727
let dbCache;
2828
export function database(app?: any): firebaseDatabaseModule.Database {
2929
if (app) {
@@ -35,7 +35,6 @@ export function database(app?: any): firebaseDatabaseModule.Database {
3535
return dbCache;
3636
}
3737

38-
3938
let firestoreCache;
4039
export function firestore(app?: any): firebaseFirestoreModule.Firestore {
4140
if (app) {
@@ -46,3 +45,14 @@ export function firestore(app?: any): firebaseFirestoreModule.Firestore {
4645
}
4746
return firestoreCache;
4847
}
48+
49+
let storageCache;
50+
export function storage(app?: any): firebaseStorageModule.Storage {
51+
if (app) {
52+
console.log("The 'app' param is ignored at the moment.");
53+
}
54+
if (!storageCache) {
55+
storageCache = new firebaseStorageModule.Storage();
56+
}
57+
return storageCache;
58+
}

0 commit comments

Comments
 (0)