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.

4.5 KB
Raw Blame
"""
Direct PDF Link Downloader
"""

from typing import Dict, Optional
from urllib.parse import urlparse
from loguru import logger

from .base import BaseDownloader, ContentType, DownloadResult
from ...security.ssrf_validator import redact_url_for_log


class DirectPDFDownloader(BaseDownloader):
    """Downloader for direct PDF links."""

    def can_handle(self, url: str) -> bool:
        """Check if URL is a direct PDF link using proper URL parsing."""
        try:
            # Parse the URL
            parsed = urlparse(url.lower())
            path = parsed.path or ""
            query = parsed.query or ""

            # Check for .pdf extension in path
            if path.endswith(".pdf"):
                return True

            # Check for .pdf with query parameters
            if ".pdf?" in url.lower():
                return True

            # Check for /pdf/ in path
            if "/pdf/" in path:
                return True

            # Check query parameters for PDF format
            if "type=pdf" in query or "format=pdf" in query:
                return True

            return False

        except Exception:
            logger.warning(f"Error parsing URL {redact_url_for_log(url)}")
            return False

    def download(
        self, url: str, content_type: ContentType = ContentType.PDF
    ) -> Optional[bytes]:
        """Download PDF directly from URL."""
        if content_type == ContentType.TEXT:
            # Download PDF and extract text
            pdf_content = self._download_pdf(url)
            if pdf_content:
                text = self.extract_text_from_pdf(pdf_content)
                if text:
                    return text.encode("utf-8")
            return None
        return self._download_pdf(url)

    def download_with_result(
        self, url: str, content_type: ContentType = ContentType.PDF
    ) -> DownloadResult:
        """Download content and return detailed result with skip reason."""
        if content_type == ContentType.TEXT:
            # Download PDF and extract text
            pdf_content = self._download_pdf(url)
            if pdf_content:
                text = self.extract_text_from_pdf(pdf_content)
                if text:
                    return DownloadResult(
                        content=text.encode("utf-8"),
                        is_success=True,
                        pdf_content=pdf_content,
                    )
                return DownloadResult(
                    skip_reason="PDF downloaded but text extraction failed"
                )
            return DownloadResult(
                skip_reason="Could not download PDF from direct link"
            )
        # Try to download PDF directly
        logger.info(
            f"Attempting direct PDF download from {redact_url_for_log(url)}"
        )
        pdf_content = self._download_pdf(url)

        if pdf_content:
            logger.info(
                f"Successfully downloaded PDF directly from "
                f"{redact_url_for_log(url)}"
            )
            return DownloadResult(
                content=pdf_content, is_success=True, pdf_content=pdf_content
            )
        # Try to determine specific reason for failure
        try:
            response = self.session.head(url, timeout=5, allow_redirects=True)
            if response.status_code == 404:
                return DownloadResult(
                    skip_reason="PDF file not found (404) - link may be broken",
                    status_code=404,
                )
            if response.status_code == 403:
                return DownloadResult(
                    skip_reason="Access denied (403) - PDF requires authentication",
                    status_code=403,
                )
            if response.status_code >= 500:
                return DownloadResult(
                    skip_reason=f"Server error ({response.status_code}) - try again later",
                    status_code=response.status_code,
                )
            return DownloadResult(
                skip_reason=f"Could not download PDF - server returned status {response.status_code}",
                status_code=response.status_code,
            )
        except Exception:
            return DownloadResult(
                skip_reason="Failed to download PDF from direct link"
            )

    def _download_pdf(
        self, url: str, headers: Optional[Dict[str, str]] = None
    ) -> Optional[bytes]:
        """Download PDF directly from URL."""
        logger.info(f"Downloading PDF directly from: {redact_url_for_log(url)}")
        return super()._download_pdf(url)