Skip to content

Commit 8391f41

Browse files
committed
feat: added util to upload blob (#37)
1 parent a09c731 commit 8391f41

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/lib/firebase.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,22 @@ export const discussionsCommentsColRef = collection(firebase.firebaseStore, 'dis
6565
export const workFlowColRef = collection(firebase.firebaseStore, 'workflow');
6666
export const discussionSubColName = 'participants';
6767
export const electionColRef = collection(firebase.firebaseStore, 'election');
68+
69+
///
70+
/// firebase storage
71+
///
72+
import { getDownloadURL, ref, uploadBytes } from 'firebase/storage';
73+
import { v4 as uuidv4 } from 'uuid';
74+
75+
export async function uploadBlobToFirestore(
76+
blob: Blob,
77+
destinationBlobName?: string
78+
): Promise<string> {
79+
// Create a reference to the destination blob
80+
const storageRef = ref(firebase.firebaseStorage, `images/${destinationBlobName || uuidv4()}`);
81+
// Upload the blob to Firebase Storage
82+
await uploadBytes(storageRef, blob);
83+
// Get the URL of the uploaded file
84+
const url = await getDownloadURL(storageRef);
85+
return url;
86+
}

src/lib/util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,9 @@ export const findCoursesTimeConflicts = (coursesObj: SubjectOjectType): SubjectO
386386
export function clamp(num: number, min: number, max: number) {
387387
return num < min ? min : num > max ? max : num;
388388
}
389+
390+
// converts file to blob
391+
export function fileToBlob(file: File): Blob {
392+
const blobFile = new Blob([file], { type: file.type });
393+
return blobFile;
394+
}

0 commit comments

Comments
 (0)