Spaces:
Sleeping
Sleeping
Commit
·
acf837b
1
Parent(s):
92f1cfa
Solve Failed to save to textbook storage: No default database name defined or provided.
Browse files- app/db.py +12 -2
- app/routers/import_doc.py +10 -2
app/db.py
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorGridFSBucket
|
| 3 |
import os
|
| 4 |
import logging
|
|
|
|
| 5 |
|
| 6 |
logger = logging.getLogger("book-query")
|
| 7 |
|
|
@@ -21,8 +22,17 @@ def get_gridfs():
|
|
| 21 |
# == PDF SAVER ==
|
| 22 |
async def save_to_textbook_fs(doc_id: str, file_path: str):
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
textbook_fs = AsyncIOMotorGridFSBucket(textbook_db)
|
| 27 |
# Read
|
| 28 |
with open(file_path, "rb") as f:
|
|
|
|
| 2 |
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorGridFSBucket
|
| 3 |
import os
|
| 4 |
import logging
|
| 5 |
+
from pymongo.uri_parser import parse_uri
|
| 6 |
|
| 7 |
logger = logging.getLogger("book-query")
|
| 8 |
|
|
|
|
| 22 |
# == PDF SAVER ==
|
| 23 |
async def save_to_textbook_fs(doc_id: str, file_path: str):
|
| 24 |
try:
|
| 25 |
+
uri = TEXTBOOK_URI
|
| 26 |
+
if not uri:
|
| 27 |
+
raise ValueError("TEXTBOOK_URI not set")
|
| 28 |
+
# Parse to correct db
|
| 29 |
+
parsed = parse_uri(uri)
|
| 30 |
+
db_name = parsed.get("database")
|
| 31 |
+
if not db_name:
|
| 32 |
+
raise ValueError("No default database name defined or provided.")
|
| 33 |
+
#
|
| 34 |
+
textbook_client = AsyncIOMotorClient(uri)
|
| 35 |
+
textbook_db = textbook_client[db_name]
|
| 36 |
textbook_fs = AsyncIOMotorGridFSBucket(textbook_db)
|
| 37 |
# Read
|
| 38 |
with open(file_path, "rb") as f:
|
app/routers/import_doc.py
CHANGED
|
@@ -72,7 +72,6 @@ async def import_book(req: ImportRequest):
|
|
| 72 |
except Exception as e:
|
| 73 |
logger.error(f"💥 Failed to upload to GridFS: {e}")
|
| 74 |
raise HTTPException(500, "Storage failed")
|
| 75 |
-
|
| 76 |
# Doc tags
|
| 77 |
doc = {
|
| 78 |
"_id": req.candidate_id,
|
|
@@ -84,7 +83,16 @@ async def import_book(req: ImportRequest):
|
|
| 84 |
await db.documents.insert_one(doc)
|
| 85 |
asyncio.create_task(parse_and_index(req.candidate_id))
|
| 86 |
logger.info(f"📚 Document {req.candidate_id} queued for indexing")
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
# Fetch textbook on id
|
| 90 |
@router.get("/textbook/{doc_id}")
|
|
|
|
| 72 |
except Exception as e:
|
| 73 |
logger.error(f"💥 Failed to upload to GridFS: {e}")
|
| 74 |
raise HTTPException(500, "Storage failed")
|
|
|
|
| 75 |
# Doc tags
|
| 76 |
doc = {
|
| 77 |
"_id": req.candidate_id,
|
|
|
|
| 83 |
await db.documents.insert_one(doc)
|
| 84 |
asyncio.create_task(parse_and_index(req.candidate_id))
|
| 85 |
logger.info(f"📚 Document {req.candidate_id} queued for indexing")
|
| 86 |
+
# Head back to frontend
|
| 87 |
+
uri = f"/import/textbook/{req.candidate_id}"
|
| 88 |
+
return {
|
| 89 |
+
"id": req.candidate_id,
|
| 90 |
+
"title": req.title,
|
| 91 |
+
"source": req.source,
|
| 92 |
+
"documentId": req.candidate_id,
|
| 93 |
+
"uri": uri
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
|
| 97 |
# Fetch textbook on id
|
| 98 |
@router.get("/textbook/{doc_id}")
|