|
| 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> |
0 commit comments