Skip to content

Commit d8d435c

Browse files
committed
feat: added paper upload function (#37)
1 parent 092e5f8 commit d8d435c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/lib/pastpaper/upload.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
 (0)