darachhat
feat: build production-ready Khmer Document Corpus v0.2.0 with Typer CLI, PyMuPDF, Polars, and DI architecture
c4e128a
Raw
History Blame Contribute Delete
1.71 kB
"""Content validator for physical PDF documents."""
from __future__ import annotations
from pathlib import Path
from app.models.document import ValidationResult
class ContentValidator:
"""Validates physical PDF files based on size, accessibility, and header markers."""
def __init__(
self,
min_file_size_kb: float = 1.0,
max_file_size_mb: float = 500.0,
) -> None:
self._min_bytes = int(min_file_size_kb * 1024)
self._max_bytes = int(max_file_size_mb * 1024 * 1024)
def validate(self, pdf_path: Path) -> ValidationResult:
pdf_path = Path(pdf_path).resolve()
errors: list[str] = []
warnings: list[str] = []
if not pdf_path.exists():
return ValidationResult(is_valid=False, errors=[f"File does not exist: {pdf_path}"])
size = pdf_path.stat().st_size
if size < self._min_bytes:
errors.append(
f"File size ({size} bytes) below minimum threshold ({self._min_bytes} bytes)"
)
if size > self._max_bytes:
errors.append(
f"File size ({size} bytes) exceeds maximum threshold ({self._max_bytes} bytes)"
)
# Verify PDF header magic bytes (%PDF-)
try:
with pdf_path.open("rb") as f:
header = f.read(5)
if header != b"%PDF-":
errors.append("Invalid PDF header magic bytes (missing %PDF-)")
except Exception as exc:
errors.append(f"Failed to read file header: {exc}")
return ValidationResult(
is_valid=len(errors) == 0,
errors=errors,
warnings=warnings,
)