feat(preview-generation): implement thumbnail generation for documents; add preview upload to EspoCRM
This commit is contained in:
@@ -20,110 +20,41 @@
|
||||
|
||||
## ⏳ In Arbeit
|
||||
|
||||
### 4. Thumbnail-Generierung (`generate_thumbnail()`)
|
||||
### 4. Preview-Generierung (`generate_thumbnail()`)
|
||||
|
||||
**Anforderungen:**
|
||||
- Erste Seite eines PDFs als Vorschaubild
|
||||
- DOCX/DOC → PDF → Image Konvertierung
|
||||
- Bild-Dateien: Resize auf Thumbnail-Größe
|
||||
- Fallback: Generic File-Icons basierend auf MIME-Type
|
||||
**✅ Implementiert** - Bereit zum Installieren der Dependencies
|
||||
|
||||
**Konfiguration:**
|
||||
- **Feld in EspoCRM**: `preview` (Attachment)
|
||||
- **Format**: **WebP** (bessere Kompression als PNG/JPEG)
|
||||
- **Größe**: **600x800px** (behält Aspect Ratio)
|
||||
- **Qualität**: 85% (guter Kompromiss zwischen Qualität und Dateigröße)
|
||||
|
||||
**Unterstützte Formate:**
|
||||
- ✅ PDF: Erste Seite als Preview
|
||||
- ✅ DOCX/DOC: Konvertierung zu PDF, dann erste Seite
|
||||
- ✅ Images (JPG, PNG, etc.): Resize auf Preview-Größe
|
||||
- ❌ Andere: Kein Preview (TODO: Generic File-Icons)
|
||||
|
||||
**Benötigte Dependencies:**
|
||||
```bash
|
||||
# Python Packages
|
||||
pip install pdf2image python-docx Pillow docx2pdf
|
||||
pip install pdf2image Pillow docx2pdf
|
||||
|
||||
# System Dependencies (Ubuntu/Debian)
|
||||
apt-get install poppler-utils libreoffice
|
||||
```
|
||||
|
||||
**Implementierungs-Schritte:**
|
||||
**Installation:**
|
||||
```bash
|
||||
cd /opt/motia-iii/bitbylaw
|
||||
/opt/bin/uv pip install pdf2image Pillow docx2pdf
|
||||
|
||||
1. **PDF Handling** (Priorität 1):
|
||||
```python
|
||||
from pdf2image import convert_from_path
|
||||
from PIL import Image
|
||||
import io
|
||||
|
||||
def generate_pdf_thumbnail(pdf_path: str) -> bytes:
|
||||
# Konvertiere erste Seite zu Image
|
||||
images = convert_from_path(pdf_path, first_page=1, last_page=1, dpi=150)
|
||||
thumbnail = images[0]
|
||||
|
||||
# Resize auf Thumbnail-Größe (z.B. 200x280)
|
||||
thumbnail.thumbnail((200, 280), Image.Resampling.LANCZOS)
|
||||
|
||||
# Convert zu bytes
|
||||
buffer = io.BytesIO()
|
||||
thumbnail.save(buffer, format='PNG')
|
||||
return buffer.getvalue()
|
||||
# System packages
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y poppler-utils libreoffice
|
||||
```
|
||||
|
||||
2. **DOCX Handling** (Priorität 2):
|
||||
```python
|
||||
from docx2pdf import convert
|
||||
import tempfile
|
||||
import os
|
||||
|
||||
def generate_docx_thumbnail(docx_path: str) -> bytes:
|
||||
# Temporäres PDF erstellen
|
||||
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as tmp:
|
||||
pdf_path = tmp.name
|
||||
|
||||
# DOCX → PDF Konvertierung (benötigt LibreOffice)
|
||||
convert(docx_path, pdf_path)
|
||||
|
||||
# PDF-Thumbnail generieren
|
||||
thumbnail = generate_pdf_thumbnail(pdf_path)
|
||||
|
||||
# Cleanup
|
||||
os.remove(pdf_path)
|
||||
|
||||
return thumbnail
|
||||
```
|
||||
|
||||
3. **Image Handling** (Priorität 3):
|
||||
```python
|
||||
from PIL import Image
|
||||
import io
|
||||
|
||||
def generate_image_thumbnail(image_path: str) -> bytes:
|
||||
img = Image.open(image_path)
|
||||
img.thumbnail((200, 280), Image.Resampling.LANCZOS)
|
||||
|
||||
buffer = io.BytesIO()
|
||||
img.save(buffer, format='PNG')
|
||||
return buffer.getvalue()
|
||||
```
|
||||
|
||||
4. **Thumbnail Upload zu EspoCRM**:
|
||||
```python
|
||||
# EspoCRM unterstützt Preview-Images via Attachment API
|
||||
async def upload_thumbnail_to_espocrm(
|
||||
document_id: str,
|
||||
thumbnail_bytes: bytes,
|
||||
espocrm_api
|
||||
):
|
||||
# Create Attachment
|
||||
attachment_data = {
|
||||
'name': 'preview.png',
|
||||
'type': 'image/png',
|
||||
'role': 'Inline Attachment',
|
||||
'parentType': 'Document',
|
||||
'parentId': document_id,
|
||||
'field': 'previewImage' # Custom field?
|
||||
}
|
||||
|
||||
# Upload via EspoCRM Attachment API
|
||||
# POST /api/v1/Attachment mit multipart/form-data
|
||||
# TODO: espocrm.py muss upload_attachment() Methode bekommen
|
||||
```
|
||||
|
||||
**Offene Fragen:**
|
||||
- Welches Feld in EspoCRM Document für Preview? `previewImage`? `thumbnail`?
|
||||
- Größe des Thumbnails? (empfohlen: 200x280 oder 300x400)
|
||||
- Format: PNG oder JPEG?
|
||||
|
||||
## ❌ Noch nicht implementiert
|
||||
|
||||
### 5. xAI Service (`xai_service.py`)
|
||||
@@ -202,7 +133,7 @@ class XAIService:
|
||||
- `xaiSyncedHash` (String): Hash beim letzten erfolgreichen Sync
|
||||
- `xaiSyncStatus` (Enum): "syncing", "synced", "failed"
|
||||
- `xaiSyncError` (Text): Fehlermeldung bei Sync-Fehler
|
||||
- `previewImage` (Attachment?): Vorschaubild
|
||||
- **`preview` (Attachment)**: Vorschaubild im WebP-Format (600x800px)
|
||||
|
||||
## 🚀 Nächste Schritte
|
||||
|
||||
|
||||
Reference in New Issue
Block a user