-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from codediodeio/upload-task
Upload task
- Loading branch information
Showing
14 changed files
with
363 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
title: DownloadURL Component | ||
pubDate: 2023-07-23 | ||
description: SvelteFire DownloadURL Component API reference | ||
layout: ../../layouts/MainLayout.astro | ||
--- | ||
|
||
# DownloadURL | ||
|
||
Returns the download URL for a file in Firebase Storage. | ||
|
||
### Props | ||
|
||
- `ref` - A Firebase Storage reference or path string (e.g. `files/hi-mom.txt`) | ||
|
||
### Slots | ||
|
||
- `default` - Shown when the url is available | ||
- `loading` - Shown while the url is loading | ||
|
||
### Slot Props | ||
|
||
- `link` - The download URL | ||
- `ref` - Storage reference | ||
- `storage` - The Firestore instance | ||
|
||
### Example | ||
|
||
```svelte | ||
<script> | ||
import { DownloadURL } from "sveltefire"; | ||
</script> | ||
<DownloadURL ref="images/pic.png" let:link let:ref> | ||
<!-- show img --> | ||
<img src={link} /> | ||
<!-- or download via link --> | ||
<a href={link} download>{ref?.name}</a> | ||
</DownloadURL> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
title: StorageList Component | ||
pubDate: 2023-07-23 | ||
description: SvelteFire StorageList Component API reference | ||
layout: ../../layouts/MainLayout.astro | ||
--- | ||
|
||
# StorageList | ||
|
||
Returns a list of files stored in Firebase Storage. | ||
|
||
### Props | ||
|
||
- `ref` - A Firebase Storage reference or path string (e.g. `files/hi-mom.txt`) | ||
|
||
### Slots | ||
|
||
- `default` - Shown when the list is available | ||
- `loading` - Shown while the list is loading | ||
|
||
### Slot Props | ||
|
||
- `list` - The list of files and prefixes | ||
- `ref` - Storage reference | ||
- `storage` - The Firestore instance | ||
|
||
### Example | ||
|
||
```svelte | ||
<script> | ||
import { StorageList } from "sveltefire"; | ||
</script> | ||
<StorageList ref="images/uid" let:list> | ||
<ul> | ||
{#if list === null} | ||
<li>Loading...</li> | ||
{:else if list.prefixes.length === 0 && list.items.length === 0} | ||
<li>Empty</li> | ||
{:else} | ||
<!-- Listing the prefixes --> | ||
{#each list.prefixes as prefix} | ||
<li> | ||
{prefix.name} | ||
</li> | ||
{/each} | ||
<!-- Listing the objects in the given folder --> | ||
{#each list.items as item} | ||
<li> | ||
{item.name} | ||
</li> | ||
{/each} | ||
{/if} | ||
</ul> | ||
</StorageList> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
title: UploadTask Component | ||
pubDate: 2023-07-23 | ||
description: SvelteFire UploadTask Component API reference | ||
layout: ../../layouts/MainLayout.astro | ||
--- | ||
|
||
# UploadTask | ||
|
||
Uploads a file to a Firebase storage bucket. | ||
|
||
### Props | ||
|
||
- `ref` - A Firebase Storage reference or path string (e.g. `files/hi-mom.txt`) | ||
- `data` - the file data to be uploaded as `Blob | Uint8Array | ArrayBuffer` | ||
- `metadata` - (optional) file metadata | ||
|
||
|
||
### Slots | ||
|
||
- `default` | ||
|
||
### Slot Props | ||
|
||
- `snapshot` - Firebase UploadTaskSnapshot | ||
- `task` - Firebase UploadTask | ||
- `progress` - Number as a percentage of the upload progress | ||
- `storage` - The Firestore instance | ||
|
||
### Example | ||
|
||
```svelte | ||
<script> | ||
import { DownloadURL, UploadTask } from "sveltefire"; | ||
let file; | ||
function chooseFile(e) { | ||
file = e.target.files[0]; | ||
} | ||
</script> | ||
<input type="file" on:change={chooseFile} /> | ||
{#if file} | ||
<UploadTask ref="myFile.txt" data={file} let:progress let:snapshot> | ||
{#if snapshot?.state === "running"} | ||
{progress}% uploaded | ||
{/if} | ||
{#if snapshot?.state === "success"} | ||
<DownloadURL ref={snapshot?.ref} let:link> | ||
<a href={link} download>Link</a> | ||
</DownloadURL> | ||
{/if} | ||
</UploadTask> | ||
{/if} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<script lang="ts"> | ||
import { uploadTaskStore } from "$lib/stores/storage.js"; | ||
import { getFirebaseContext } from "$lib/stores/sdk.js"; | ||
import type { | ||
FirebaseStorage, | ||
UploadTask, | ||
StorageReference, | ||
UploadMetadata, | ||
UploadTaskSnapshot, | ||
} from "firebase/storage"; | ||
export let ref: string | StorageReference; | ||
export let data: Blob | Uint8Array | ArrayBuffer; | ||
export let metadata: UploadMetadata | undefined = undefined; | ||
const { storage } = getFirebaseContext(); | ||
const upload = uploadTaskStore(storage!, ref, data, metadata); | ||
interface $$Slots { | ||
default: { | ||
task: UploadTask | undefined; | ||
ref: StorageReference | null; | ||
snapshot: UploadTaskSnapshot | null; | ||
progress: number; | ||
storage?: FirebaseStorage; | ||
}; | ||
} | ||
$: progress = ($upload?.bytesTransferred! / $upload?.totalBytes!) * 100 ?? 0; | ||
</script> | ||
|
||
{#if $upload !== undefined} | ||
<slot task={$upload?.task} snapshot={$upload} {progress} ref={upload.reference} {storage} /> | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.