|
| 1 | +import { toRefs, reactive, ref } from "vue"; |
| 2 | +import firebase from "firebase"; |
| 3 | +// Required for side-effects |
| 4 | +import "firebase/firestore"; |
| 5 | + |
| 6 | +import FIREBASE_CONFIG from "./.env.firebase"; |
| 7 | + |
| 8 | +// initialize firebase, this is directly from the firebase documentation |
| 9 | +// regarding getting started for the web |
| 10 | +if (firebase.apps.length === 0) { |
| 11 | + firebase.initializeApp(FIREBASE_CONFIG); |
| 12 | +} |
| 13 | + |
| 14 | +const STORAGE_FILE_PATH = "image-capture/"; |
| 15 | + |
| 16 | +export default function() { |
| 17 | + const progress = ref(0); |
| 18 | + const files = ref<any>([]); |
| 19 | + const state = reactive<{ |
| 20 | + error: any | null; |
| 21 | + loading: boolean; |
| 22 | + resultData: any | null; |
| 23 | + }>({ |
| 24 | + // error if one happens |
| 25 | + error: null, |
| 26 | + // if the query is loading or not |
| 27 | + loading: false, |
| 28 | + // result from upload |
| 29 | + resultData: {}, |
| 30 | + }); |
| 31 | + |
| 32 | + // get the database storage |
| 33 | + const storageRef = firebase.storage().ref(); |
| 34 | + |
| 35 | + const listFiles = () => { |
| 36 | + files.value = []; |
| 37 | + return firebase |
| 38 | + .storage() |
| 39 | + .ref("image-capture/") |
| 40 | + .listAll() |
| 41 | + .then((r: firebase.storage.ListResult) => { |
| 42 | + console.log(r); |
| 43 | + r.items.forEach(async (e: firebase.storage.Reference) => { |
| 44 | + console.log(e.fullPath); |
| 45 | + const url = await e.getDownloadURL() |
| 46 | + files.value.push({name : e.fullPath, url}); |
| 47 | + }); |
| 48 | + |
| 49 | + return files.value; |
| 50 | + }); |
| 51 | + }; |
| 52 | + |
| 53 | + (async ()=>{ |
| 54 | + await listFiles() |
| 55 | + })(); |
| 56 | + |
| 57 | + const uploadData = async (fileData: string, name?: string) => { |
| 58 | + // initialize upload information |
| 59 | + state.error = null; |
| 60 | + state.loading = true; |
| 61 | + |
| 62 | + progress.value = 0; |
| 63 | + // ensure unique file names |
| 64 | + const uniquePathName = new Date().getTime() + "-" + name; |
| 65 | + const ext = name?.split(".")[1].toLowerCase(); |
| 66 | + |
| 67 | + try { |
| 68 | + const ref = storageRef.child( |
| 69 | + `${STORAGE_FILE_PATH}\\${uniquePathName}.${ext}` |
| 70 | + ); |
| 71 | + const uploadTask = ref.putString(fileData, "data_url", { |
| 72 | + contentType: "image/jpeg", |
| 73 | + }); |
| 74 | + |
| 75 | + // The first example. |
| 76 | + uploadTask.on( |
| 77 | + firebase.storage.TaskEvent.STATE_CHANGED, |
| 78 | + (_progress) => { |
| 79 | + const prog = _progress.bytesTransferred / _progress.totalBytes; |
| 80 | + progress.value = prog; |
| 81 | + console.log("STATE_CHANGED", prog); |
| 82 | + }, |
| 83 | + (_error) => { |
| 84 | + state.error = _error; |
| 85 | + state.loading = false; |
| 86 | + console.log("ERROR", _error); |
| 87 | + }, |
| 88 | + // eslint-disable-next-line no-unused-vars |
| 89 | + async () => { |
| 90 | + state.error = null; |
| 91 | + state.loading = false; |
| 92 | + |
| 93 | + const downloadUrl = await uploadTask.snapshot.ref.getDownloadURL(); |
| 94 | + state.resultData = { |
| 95 | + downloadUrl, |
| 96 | + name: uploadTask.snapshot.metadata.name, |
| 97 | + image: { |
| 98 | + ref: uploadTask.snapshot.ref.fullPath, |
| 99 | + size: uploadTask.snapshot.metadata.size, |
| 100 | + contentType: uploadTask.snapshot.metadata.contentType, |
| 101 | + timeCreated: uploadTask.snapshot.metadata.timeCreated, |
| 102 | + }, |
| 103 | + }; |
| 104 | + |
| 105 | + // set progress back to zero when complete |
| 106 | + progress.value = 0; |
| 107 | + } |
| 108 | + ); |
| 109 | + } catch (_error) { |
| 110 | + state.loading = false; |
| 111 | + state.error = _error; |
| 112 | + progress.value = 0; |
| 113 | + } |
| 114 | + }; |
| 115 | + return { |
| 116 | + ...toRefs(state), |
| 117 | + progress, |
| 118 | + uploadData, |
| 119 | + listFiles, |
| 120 | + files |
| 121 | + }; |
| 122 | +} |
0 commit comments