-
-
Save DharmarajX24/90f5e42f77933c47bd1c1b34a2f0a05b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { ref } from 'vue'; | |
import { httpsCallable } from 'firebase/functions'; | |
import { functions } from 'boot/firebase'; | |
const files = ref<File[]>([]); | |
type UploadFilesPayload = { files: Array<{ name: string; size: number }> }; | |
type UploadFilesResult = { | |
result: Record<string, string>; | |
error?: { message: string }; | |
}; | |
const uploadFiles = async () => { | |
const uploadFiles = httpsCallable<UploadFilesPayload, UploadFilesResult>( | |
functions, | |
'getUploadSignedUrls' | |
); | |
const payload = files.value.map((file) => ({ | |
name: file.name, | |
size: file.size, | |
})); | |
const { data } = await uploadFiles({ files: payload }); | |
if (data.result) { | |
const gcsUploads = files.value.map((file) => { | |
const url = data.result[file.name]; | |
return fetch(url, { | |
method: 'PUT', | |
headers: { | |
'Content-Type': file.type, | |
}, | |
body: file, | |
}); | |
}); | |
try { | |
await Promise.all(gcsUploads); | |
alert('Files uploaded successfully'); | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment