File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { UserDocType } from "../firebase_doctypes" ;
2
+ import { pastPapersCol , uploadBlobToFirestore } from "../firebase" ;
3
+ import { fileToBlob } from "../util" ;
4
+ import { PastPaperDocType } from "./types" ;
5
+ import { doc , serverTimestamp , setDoc } from "firebase/firestore" ;
6
+
7
+ interface UploadProps {
8
+ file : File | null
9
+ currUser : UserDocType ,
10
+ subject_name : string ;
11
+ }
12
+
13
+ export default async function upload (
14
+ { file, subject_name, currUser } : UploadProps
15
+ ) {
16
+ if ( ! file ) return ;
17
+
18
+ // todo: validate file input via gemini OR may be try some free otpion
19
+
20
+ try {
21
+ const photo_url = await uploadBlobToFirestore ( fileToBlob ( file ) )
22
+
23
+ const docRef = doc ( pastPapersCol ) ;
24
+ const docData : PastPaperDocType = {
25
+ photo_url,
26
+ uid : docRef . id ,
27
+ subject_name,
28
+ upload_at : serverTimestamp ( ) ,
29
+ uploader : {
30
+ displayName : currUser . displayName ,
31
+ photoURL : currUser . photoURL ,
32
+ uid : currUser . uid
33
+ } ,
34
+ votes_count : 0 ,
35
+ uploader_uid : currUser . uid
36
+ }
37
+
38
+ await setDoc ( docRef , docData )
39
+
40
+ } catch ( err ) {
41
+ return false ;
42
+ }
43
+
44
+ return true
45
+ }
You can’t perform that action at this time.
0 commit comments