Skip to content

Instantly share code, notes, and snippets.

@DharmarajX24
Last active October 29, 2022 13:43
Show Gist options
  • Save DharmarajX24/90f5e42f77933c47bd1c1b34a2f0a05b to your computer and use it in GitHub Desktop.
Save DharmarajX24/90f5e42f77933c47bd1c1b34a2f0a05b to your computer and use it in GitHub Desktop.
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