-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdb.py
33 lines (22 loc) · 798 Bytes
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import gridfs
from werkzeug.datastructures import FileStorage
from constants import *
def store_pdf(cohere_db: str, file: FileStorage, filepath):
# Create a GridFS object
# this connects to mongo
fs = gridfs.GridFS(cohere_db, collection=COLLECTION_NAME)
# TODO this isn't tested, with file.read(), I hope it works
# Read the PDF file as binary data
pdf_data = file.read()
# Store the binary data in GridFS
pdf_id = fs.put(pdf_data, filename=filepath)
return pdf_id
def db_main():
test_file = FileStorage(
stream=open("backend/pdfs/harry-potter-and-the-sorcerers-stone-2001.pdf", "rb"),
filename="test.pdf",
content_type="application/pdf",
)
store_pdf(test_file, "test.pdf")
if __name__ == "__main__":
db_main()