STABLE: Final schema sync, optimized gitignore

This commit is contained in:
Kincses
2026-02-26 08:19:25 +01:00
parent 893f39fa15
commit 505543330a
203 changed files with 11590 additions and 9542 deletions

View File

@@ -1,82 +1,65 @@
# /opt/docker/dev/service_finder/backend/app/services/document_service.py
import os
import shutil
import time
from PIL import Image
from uuid import uuid4
from fastapi import UploadFile, BackgroundTasks
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.document import Document
from app.core.config import settings
class DocumentService:
@staticmethod
def _clean_temp(path: str):
"""30 perc után törli az ideiglenes fájlt (opcionális, ha maradunk a puffer mellett)"""
time.sleep(1800)
if os.path.exists(path):
os.remove(path)
@staticmethod
async def process_upload(
file: UploadFile,
parent_type: str,
parent_id: str,
db: AsyncSession,
background_tasks: BackgroundTasks
file: UploadFile, parent_type: str, parent_id: str,
db: AsyncSession, background_tasks: BackgroundTasks
):
""" Kép optimalizálás, Thumbnail generálás és NAS tárolás. """
file_uuid = str(uuid4())
ext = file.filename.split('.')[-1].lower() if '.' in file.filename else "webp"
# 1. Könyvtárstruktúra meghatározása
temp_dir = "/app/temp/uploads"
nas_vault_dir = f"/mnt/nas/app_data/organizations/{parent_id}/vault"
ssd_thumb_dir = f"/app/static/previews/organizations/{parent_id}"
# Útvonalak a settings-ből (vagy fallback)
nas_base = getattr(settings, "NAS_STORAGE_PATH", "/mnt/nas/app_data")
vault_dir = os.path.join(nas_base, parent_type, parent_id, "vault")
thumb_dir = os.path.join(settings.STATIC_DIR, "previews", parent_type, parent_id)
for d in [temp_dir, nas_vault_dir, ssd_thumb_dir]:
os.makedirs(d, exist_ok=True)
os.makedirs(vault_dir, exist_ok=True)
os.makedirs(thumb_dir, exist_ok=True)
# 2. Mentés a TEMP-be
temp_path = os.path.join(temp_dir, f"{file_uuid}_{file.filename}")
content = await file.read()
with open(temp_path, "wb") as f:
f.write(content)
temp_path = f"/tmp/{file_uuid}_{file.filename}"
with open(temp_path, "wb") as f: f.write(content)
# 3. Képfeldolgozás (Pillow)
# Képfeldolgozás
img = Image.open(temp_path)
# A) Thumbnail generálás (300px WebP az SSD-re)
# Thumbnail (SSD)
thumb_filename = f"{file_uuid}_thumb.webp"
thumb_path = os.path.join(ssd_thumb_dir, thumb_filename)
thumb_path = os.path.join(thumb_dir, thumb_filename)
thumb_img = img.copy()
thumb_img.thumbnail((300, 300))
thumb_img.save(thumb_path, "WEBP", quality=80)
# B) Nagy kép optimalizálás (Max 1600px WebP a NAS-ra)
# Optimalizált eredeti (NAS)
vault_filename = f"{file_uuid}.webp"
vault_path = os.path.join(nas_vault_dir, vault_filename)
vault_path = os.path.join(vault_dir, vault_filename)
if img.width > 1600:
ratio = 1600 / float(img.width)
new_height = int(float(img.height) * float(ratio))
img = img.resize((1600, new_height), Image.Resampling.LANCZOS)
img = img.resize((1600, int(img.height * (1600 / img.width))), Image.Resampling.LANCZOS)
img.save(vault_path, "WEBP", quality=85)
# 4. Adatbázis rögzítés
# Mentés az új Document modellbe
new_doc = Document(
id=uuid4(),
parent_type=parent_type,
parent_id=parent_id,
original_name=file.filename,
file_hash=file_uuid,
file_ext="webp",
mime_type="image/webp",
file_size=os.path.getsize(vault_path),
has_thumbnail=True,
thumbnail_path=f"/static/previews/organizations/{parent_id}/{thumb_filename}"
thumbnail_path=f"/static/previews/{parent_type}/{parent_id}/{thumb_filename}"
)
db.add(new_doc)
await db.commit()
# 5. Puffer törlés ütemezése (30 perc)
# background_tasks.add_task(DocumentService._clean_temp, temp_path)
# MVP-ben töröljük azonnal, ha már a NAS-on van a biztonságos másolat
os.remove(temp_path)
return new_doc