Merge branch 'main' into 51-feature-request-switch-google-drive-integration-to-oauth-for-user-based-authentication

This commit is contained in:
Christian Krakau-Louis
2025-04-11 00:18:10 +02:00
committed by GitHub
4 changed files with 35 additions and 24 deletions
+21 -14
View File
@@ -2,7 +2,7 @@
import os
import shutil
import fitz # PyMuPDF for PDF metadata editing
import PyPDF2 # Replace fitz with PyPDF2
import json
from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry
@@ -51,7 +51,6 @@ def embed_metadata_into_pdf(local_file_path: str, extracted_text: str, metadata:
After processing, the file is moved to
<workdir>/processed/<suggested_filename.pdf>
where <suggested_filename.pdf> is derived from metadata["filename"].
The output PDF is saved incrementally while preserving its original encryption.
Additionally, the metadata is persisted to a JSON file with the same base name.
"""
# Check for file existence; if not found, try the known shared tmp directory.
@@ -74,18 +73,26 @@ def embed_metadata_into_pdf(local_file_path: str, extracted_text: str, metadata:
try:
print(f"[DEBUG] Embedding metadata into {processed_file}...")
# Open the PDF
doc = fitz.open(processed_file)
# Set PDF metadata using only the standard keys.
doc.set_metadata({
"title": metadata.get("filename", "Unknown Document"),
"author": metadata.get("absender", "Unknown"),
"subject": metadata.get("document_type", "Unknown"),
"keywords": ", ".join(metadata.get("tags", []))
})
# Save incrementally and preserve encryption
doc.save(processed_file, incremental=True, encryption=fitz.PDF_ENCRYPT_KEEP)
doc.close()
# Open the PDF and modify metadata
with open(processed_file, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
pdf_writer = PyPDF2.PdfWriter()
# Copy all pages from the reader to the writer
for page in pdf_reader.pages:
pdf_writer.add_page(page)
# Set PDF metadata
pdf_writer.add_metadata({
"/Title": metadata.get("filename", "Unknown Document"),
"/Author": metadata.get("absender", "Unknown"),
"/Subject": metadata.get("document_type", "Unknown"),
"/Keywords": ", ".join(metadata.get("tags", []))
})
# Write the modified PDF
with open(processed_file, 'wb') as output_file:
pdf_writer.write(output_file)
print(f"[INFO] Metadata embedded successfully in {processed_file}")
+12 -8
View File
@@ -4,7 +4,7 @@ import os
import uuid
import shutil
import mimetypes
import fitz # PyMuPDF for checking embedded text
import PyPDF2 # Replace fitz with PyPDF2
from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry
@@ -81,19 +81,23 @@ def process_document(original_local_file: str):
db.commit()
# 2. Check for embedded text (outside the DB session to avoid long open transactions)
pdf_doc = fitz.open(new_local_path)
has_text = any(page.get_text() for page in pdf_doc)
pdf_doc.close()
with open(new_local_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
has_text = False
for page in pdf_reader.pages:
if page.extract_text().strip():
has_text = True
break
if has_text:
print(f"[INFO] PDF {original_local_file} contains embedded text. Processing locally.")
# Extract text locally
extracted_text = ""
pdf_doc = fitz.open(new_local_path)
for page in pdf_doc:
extracted_text += page.get_text("text") + "\n"
pdf_doc.close()
with open(new_local_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
for page in pdf_reader.pages:
extracted_text += page.extract_text() + "\n"
# Call metadata extraction directly
extract_metadata_with_gpt.delay(new_filename, extracted_text)
+1
View File
@@ -19,3 +19,4 @@ __all__ = [
'dump_all_settings',
'check_all_configs'
]
+1 -2
View File
@@ -5,8 +5,7 @@ redis # Message broker for Celery
sqlalchemy # Database ORM
pydantic # Data validation
openai # GPT integration for metadata extraction
pymupdf # PDF processing, text extraction, and detection (imported as 'fitz')
PyPDF2 # PDF processing for page counting and now also for rotation
PyPDF2>=3.0.0 # PDF processing for text extraction, metadata editing and rotation (replaces PyMuPDF)
requests # HTTP client
dropbox>=11.36.0 # Dropbox integration
azure-ai-documentintelligence # Azure OCR service