Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

refactor: address djpetti review comments for PDF storage

LearningCircuite5c9581ada85
diff --git a/src/local_deep_research/research_library/routes/library_routes.py b/src/local_deep_research/research_library/routes/library_routes.py
index c421de5a0a..3b36b3b00a 100644
--- a/src/local_deep_research/research_library/routes/library_routes.py
+++ b/src/local_deep_research/research_library/routes/library_routes.py
@@ -29,7 +29,6 @@ from ...database.session_context import get_user_db_session
from ...database.models.research import ResearchResource
from ...database.models.library import (
Document as Document,
- DocumentBlob,
DocumentStatus,
DownloadQueue as LibraryDownloadQueue,
Collection,
@@ -37,6 +36,7 @@ from ...database.models.library import (
from ...library.download_management import ResourceFilter
from ..services.download_service import DownloadService
from ..services.library_service import LibraryService
+from ..services.pdf_storage_manager import PDFStorageManager
from ..utils import open_file_location, handle_api_error
from ...security.path_validator import PathValidator
from ...utilities.db_utils import get_settings_manager
@@ -440,7 +440,7 @@ def get_pdf_url(document_id):
@library_bp.route("/document/<string:document_id>/pdf")
@login_required
def view_pdf_page(document_id):
- """Page for viewing PDF file - database first, filesystem fallback."""
+ """Page for viewing PDF file - uses PDFStorageManager for retrieval."""
username = session.get("username")
with get_user_db_session(username) as db_session:
@@ -458,47 +458,34 @@ def view_pdf_page(document_id):
f"file_path={document.file_path}"
)
- # 1. First check database for blob (regardless of storage_mode)
- blob = (
- db_session.query(DocumentBlob)
- .filter_by(document_id=document_id)
- .first()
+ # Get settings for PDF storage manager
+ settings = get_settings_manager(username)
+ storage_mode = settings.get_setting(
+ "research_library.pdf_storage_mode", "none"
)
+ library_root = Path(
+ settings.get_setting(
+ "research_library.storage_path",
+ str(Path.home() / ".local-deep-research" / "library"),
+ )
+ ).expanduser()
+
+ # Use PDFStorageManager to load PDF (handles database and filesystem)
+ pdf_manager = PDFStorageManager(library_root, storage_mode)
+ pdf_bytes = pdf_manager.load_pdf(document, db_session)
- if blob and blob.pdf_binary:
+ if pdf_bytes:
logger.info(
- f"Serving PDF from database: {document_id} "
- f"({len(blob.pdf_binary)} bytes)"
+ f"Serving PDF for document {document_id} ({len(pdf_bytes)} bytes)"
)
return send_file(
- BytesIO(blob.pdf_binary),
+ BytesIO(pdf_bytes),
mimetype="application/pdf",
as_attachment=False,
download_name=document.filename or "document.pdf",
)
- # 2. Fallback to filesystem
- if document.file_path and document.file_path != "metadata_only":
- # Get library root from settings
- settings = get_settings_manager(username)
- library_root = settings.get_setting(
- "research_library.storage_path",
- str(Path.home() / ".local-deep-research" / "library"),
- )
- file_path = Path(library_root) / document.file_path
-
- if file_path.exists():
- logger.info(f"Serving PDF from filesystem: {file_path}")
- return send_file(
- str(file_path),
- mimetype="application/pdf",
- as_attachment=False,
- download_name=document.filename or "document.pdf",
- )
-
- logger.warning(f"PDF file not found on disk: {file_path}")
-
- # 3. No PDF found anywhere
+ # No PDF found anywhere
logger.warning(f"No PDF available for document {document_id}")
return "PDF not available", 404
diff --git a/src/local_deep_research/research_library/services/library_service.py b/src/local_deep_research/research_library/services/library_service.py
index 6e4b788f77..ba91f6b0be 100644
--- a/src/local_deep_research/research_library/services/library_service.py
+++ b/src/local_deep_research/research_library/services/library_service.py
@@ -18,6 +18,7 @@ from sqlalchemy import and_, or_, func, Integer, case
from ...database.models.download_tracker import DownloadTracker
from ...database.models.library import (
Document,
+ DocumentBlob,
DocumentStatus,
Collection,
DocumentCollection,
@@ -278,9 +279,15 @@ class LibraryService:
)
# Check if PDF is available (filesystem OR database)
- has_pdf = (
- bool(file_absolute_path) or doc.storage_mode == "database"
- )
+ has_pdf = bool(file_absolute_path)
+ if not has_pdf and doc.storage_mode == "database":
+ # Verify blob actually exists in database
+ has_pdf = (
+ session.query(DocumentBlob.document_id)
+ .filter_by(document_id=doc.id)
+ .first()
+ is not None
+ )
has_text_db = bool(doc.text_content) # Text now in Document
# Use DocumentCollection from query results
@@ -570,11 +577,19 @@ class LibraryService:
)
# Check if PDF is available (database OR filesystem)
- has_pdf = doc.storage_mode == "database" or bool(
+ has_pdf = bool(
doc.file_path
and doc.file_path != "metadata_only"
and doc.file_path != "text_only_not_stored"
)
+ if not has_pdf and doc.storage_mode == "database":
+ # Verify blob actually exists in database
+ has_pdf = (
+ session.query(DocumentBlob.document_id)
+ .filter_by(document_id=doc.id)
+ .first()
+ is not None
+ )
return {
"id": doc.id,