Skip to content

Commit e799c68

Browse files
committed
chore: add playground example for storage
1 parent 5dddcf2 commit e799c68

File tree

4 files changed

+120
-7
lines changed

4 files changed

+120
-7
lines changed

playground/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@types/node": "^16.11.65",
2323
"@vitejs/plugin-vue": "^3.0.3",
2424
"@vue/tsconfig": "^0.1.3",
25+
"@vueuse/core": "^9.5.0",
2526
"npm-run-all": "^4.1.5",
2627
"typescript": "~4.8.4",
2728
"unplugin-vue-router": "^0.2.3",

playground/src/pages/storage.vue

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<script lang="ts" setup>
2+
import { computed, ref, watch } from 'vue'
3+
import { useFileDialog } from '@vueuse/core'
4+
import { useStorage, useStorageTask } from 'vuefire'
5+
import { ref as storageRef, type StorageReference } from 'firebase/storage'
6+
7+
const filename = ref<string>()
8+
const { files, open, reset } = useFileDialog()
9+
// automatically set the filename when a file is selected
10+
watch(
11+
() => files.value?.item(0)?.name,
12+
(name) => {
13+
// avoid clearing out the filename
14+
if (name && !filename.value) {
15+
filename.value = name
16+
}
17+
}
18+
)
19+
20+
const storage = useStorage()
21+
22+
const storageBucket = storageRef(storage, 'demo')
23+
const storageSource = ref<StorageReference>()
24+
// automatically compute the data
25+
26+
const { progress, url, error, snapshot, uploadTask, data } =
27+
useStorageTask(storageSource)
28+
29+
// TODO: move to tests
30+
// useStorageTask(storageSource, null).data
31+
// // should fail
32+
// useStorageTask(storageSource, new Blob()).data
33+
34+
function uploadPicture() {
35+
storageSource.value = storageRef(storageBucket, filename.value)
36+
data.value = files.value?.item(0)
37+
}
38+
</script>
39+
40+
<template>
41+
<form @submit.prevent="uploadPicture">
42+
<fieldset :disabled="!!uploadTask">
43+
<button
44+
type="button"
45+
@click="open({ accept: 'image/*', multiple: false })"
46+
>
47+
<template v-if="files?.length">
48+
<template v-if="files.length === 1"
49+
>Selected file: {{ files.item(0)!.name }} (Click to select
50+
another)</template
51+
>
52+
<template v-else>{{ files.length }} files (you hacker 😢)</template>
53+
</template>
54+
<template v-else> Select one picture </template>
55+
</button>
56+
57+
<br />
58+
59+
<label>
60+
Filename to use (leave blank to auto generate)
61+
<input type="text" v-model="filename" />
62+
</label>
63+
64+
<br />
65+
66+
<button>Upload</button>
67+
</fieldset>
68+
</form>
69+
70+
<div v-if="error">
71+
<p>
72+
Error: {{ error.name }} ({{ error.code }})
73+
<br />
74+
{{ error.message }}
75+
<br />
76+
</p>
77+
<pre v-if="error.stack">{{ error.stack }}</pre>
78+
<pre v-if="error.customData">{{ error.customData }}</pre>
79+
</div>
80+
<div v-else-if="progress != null">
81+
<progress max="1" :value="progress">{{ progress * 100 }}%</progress>
82+
</div>
83+
<p v-if="url">
84+
Success: {{ url }}
85+
<br />
86+
<img :src="url" />
87+
</p>
88+
<p v-if="snapshot">File: {{ snapshot.ref.name }}</p>
89+
90+
<p v-if="storageSource">Select a new file to simply update it</p>
91+
<p v-else>Clear the input to delete the file.</p>
92+
<button @click="data = null" :disabled="!data || !storageSource">
93+
Delete the picture
94+
</button>
95+
</template>

playground/typed-router.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ declare module 'vue-router/auto/routes' {
8484
Record<never, never>,
8585
Record<never, never>
8686
>
87+
'/storage': RouteRecordInfo<
88+
'/storage',
89+
'/storage',
90+
Record<never, never>,
91+
Record<never, never>
92+
>
8793
'/todos': RouteRecordInfo<
8894
'/todos',
8995
'/todos',

pnpm-lock.yaml

Lines changed: 18 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)