Skip to content

Commit abfb528

Browse files
committed
refactor: support UploadTask and StorageRef
Made changes to current PR to align with Firebase Storage SDK features
1 parent 1fcb080 commit abfb528

File tree

7 files changed

+215
-180
lines changed

7 files changed

+215
-180
lines changed

README.md

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -437,30 +437,33 @@ Slot Props & Events:
437437

438438
Note: Each data item in the collection contains the document data AND fields for the `id` and `ref` (DocumentReference).
439439

440-
### `<File>`
440+
### `<StorageRef>`
441441

442-
Retrives a file from Firebase storage.
442+
Retrives a downloadURL and metada from Firebase storage.
443443

444444
Props:
445445

446-
- *path (required)* to file in storage i.e `images/mountain.jpg`
446+
- *path (required)* to file in storage i.e `images/mountain.jpg` or a [Reference](https://firebase.google.com/docs/reference/js/firebase.storage.Reference)
447+
- *meta* include metadata with file. Default `false`.
448+
- *startWith* start with a default URL. Pass an object like `{ url: someURL }`
447449

448450
Slots:
449451

450-
- *default slot* shown when file is available.
452+
- *default slot* shown when downloadURL is available.
451453
- *loading* shown when waiting for response.
452454
- *fallback* shown when error occurs.
453455

454456

455457
Slot Props & Events:
456458

457-
- *url* url to resource
458-
- *ref* File Reference for direct acess
459+
- *downloadURL* url to resource
460+
- *metadata* file metadata
461+
- *ref* Storage Reference for direct access
459462

460463
```html
461-
<File {path} let:url let:ref>
464+
<StorageRef {path} let:downloadURL let:ref meta let:metadata>
462465

463-
<img src={url} />
466+
<img src={downloadURL} />
464467

465468
<div slot="loading">
466469
Loading...
@@ -470,42 +473,47 @@ Slot Props & Events:
470473
Error
471474
</div>
472475

473-
</File>
476+
</StorageRef>
474477
```
475478

476-
### `<Upload>`
479+
### `<UploadTask>`
480+
481+
Creates an [UploadTask](https://firebase.google.com/docs/reference/js/firebase.storage.UploadTask) that transmits a file to Firebase storage.
477482

478483
Props:
479484

480-
- *path (required)* to upload to i.e "images/mountain.jpg"
481-
- *file* file to upload as a file object `Blob` or `Unit8Array`
485+
- *path (required)* to upload to i.e "images/mountain.jpg" or a [Reference](https://firebase.google.com/docs/reference/js/firebase.storage.Reference)
486+
- *file* file to upload as a `File` object `Blob` or `Unit8Array`.
487+
-
482488

483489
Slots:
484490

485-
- *default slot* shown before upload is complete.
486-
- *complete* shown when upload is complete.
491+
- *default slot* shown while task is created.
492+
- *complete* shown when task state is `success` and url is available.
487493
- *fallback* shown when error occurs or upload is cancelled.
488494

489495

490496
Slot Props & Events:
491497

492-
- *url* url to uploaded file.
493-
- *task* Firebase upload task.
494-
- *snapshot* snapshot of upload, useful for monitoring progress.
498+
- *snapshot* snapshot of upload, useful for monitoring progress.
499+
- *task* Firebase upload task. Use it to pause, resume, and cancel. `task.pause()`
500+
- *downLoadURL* url to uploaded file.
495501

496502

497503
```html
498-
<Upload {file} {path} let:task let:snapshot let:url>
504+
<UploadTask {file} {path} let:task let:snapshot let:downloadURL={url}>
505+
506+
Uploading your file...
499507

500-
Uploading your file... {snaphot.bytesTransferred}
508+
Progress: {(snapshot.bytesTransferred / snapshot.totalBytes) * 100} %
501509

502510
<div slot="complete">
503-
Download here {url}
511+
Success! Download here {url}
504512
</div>
505513

506514
<div slot="fallback">
507515
Error or canceled
508516
</div>
509517

510-
</Upload>
518+
</UploadTask>
511519
```

example/src/App.svelte

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
export let name;
33
import { writable } from "svelte/store";
44
import { onMount } from "svelte";
5-
import { FirebaseApp, Doc, Collection, User } from "sveltefire";
5+
import { FirebaseApp, Doc, Collection, User, UploadTask, StorageRef } from "sveltefire";
66
77
import firebase from "firebase/app";
88
import "firebase/firestore";
99
import "firebase/auth";
10+
import "firebase/storage";
1011
import "firebase/performance"; // Optional
1112
import "firebase/analytics"; // Optional
1213
@@ -28,6 +29,8 @@
2829
let eventData = {};
2930
let eventRef = {};
3031
32+
let readyToUpload = false;
33+
3134
function onData(e) {
3235
eventData = e.detail.data;
3336
}
@@ -61,14 +64,18 @@
6164
</script>
6265

6366
<style>
64-
67+
h1 {
68+
border-bottom: 1px dashed rgb(185, 185, 185);
69+
}
6570
</style>
6671

6772
<FirebaseApp {firebase} on:initializeApp={useEmulator} perf analytics>
6873

69-
<h1>Firebase Ready {appName}</h1>
74+
<h1>FirebaseApp</h1>
75+
76+
<p>Firebase Ready {appName}</p>
7077

71-
<h1>User Login</h1>
78+
<h1>User</h1>
7279
<User let:user let:auth>
7380

7481
<div slot="signed-out">
@@ -82,7 +89,7 @@
8289
UID: {user.uid}
8390
<button on:click={() => auth.signOut()}>Sign Out</button>
8491

85-
<h2>Firestore</h2>
92+
<h1>Firestore</h1>
8693

8794
<Doc path={`posts/first-post`} let:data={post} let:ref={postRef} log>
8895
<div slot="loading">Loading...</div>
@@ -176,7 +183,7 @@
176183
</Doc>
177184

178185

179-
<h2>Events</h2>
186+
<h3>Events</h3>
180187

181188
<p>Path: {eventRef.path}</p>
182189
<p>Event Data: {eventData && eventData.title}</p>
@@ -185,4 +192,43 @@
185192
</button>
186193

187194
<Doc path={'posts/event-post'} on:data={onData} on:ref={onRef} />
195+
196+
197+
198+
199+
<h1>Storage</h1>
200+
201+
<button on:click={(e) => readyToUpload = true }>Start Upload</button>
202+
203+
{#if readyToUpload}
204+
205+
<h3>UploadTask:</h3>
206+
207+
<UploadTask path={'myfile.txt'} file={new File(['hello world'], 'filename')} let:task let:snapshot let:downloadURL>
208+
209+
Progress {(snapshot.bytesTransferred / snapshot.totalBytes) * 100} <br>
210+
State {snapshot.state}
211+
212+
<button on:click={() => task.pause()}>Pause</button>
213+
214+
<div slot="complete">DownloadURL: {downloadURL}</div>
215+
216+
</UploadTask>
217+
218+
{/if}
219+
220+
221+
<h3>File</h3>
222+
223+
<StorageRef path={'myfile.txt'} meta let:metadata let:downloadURL>
224+
225+
URL: {downloadURL} <br>
226+
Meta: {JSON.stringify(metadata)}
227+
228+
<div slot="loading">Loading file...</div>
229+
</StorageRef>
230+
231+
188232
</FirebaseApp>
233+
234+
<div style="height: 500px"></div>

src/File.svelte

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/StorageRef.svelte

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<script>
2+
export let path = "";
3+
export let log = false;
4+
export let traceId = "";
5+
export let startWith = undefined;
6+
export let url = true;
7+
export let meta = false;
8+
9+
import { onDestroy, onMount, createEventDispatcher } from "svelte";
10+
import { fileDownloadStore } from "./storage";
11+
12+
const opts = {
13+
startWith,
14+
traceId,
15+
log,
16+
meta,
17+
url,
18+
};
19+
20+
let store = fileDownloadStore(path, opts);
21+
22+
const dispatch = createEventDispatcher();
23+
24+
let unsub;
25+
26+
// Props changed
27+
$: {
28+
if (unsub) {
29+
// Unsub and create new store
30+
unsub();
31+
store = fileDownloadStore(path, opts);
32+
dispatch("ref", { ref: store.ref });
33+
}
34+
35+
unsub = store.subscribe(result => {
36+
if (result) {
37+
dispatch("storageResult", {
38+
downloadURL: result[0],
39+
metadata: result[1],
40+
});
41+
}
42+
43+
});
44+
}
45+
46+
onMount(() => dispatch("ref", { ref: store.ref }));
47+
onDestroy(() => unsub());
48+
</script>
49+
50+
<slot name="before" />
51+
52+
{#if $store}
53+
<slot
54+
downloadURL={$store && $store.url}
55+
metadata={$store && $store.metadata}
56+
ref={store.ref}
57+
error={store.error} />
58+
{:else if store.loading}
59+
<slot name="loading" />
60+
{:else}
61+
<slot name="fallback" />
62+
{/if}
63+
64+
<slot name="after" />

0 commit comments

Comments
 (0)