feat: enhance photo upload and management features
- Added `taken_at` timestamp to the photos table for improved photo metadata. - Updated the photo upload API to support optional EXIF data extraction and file last modified timestamp. - Enhanced the photo upload process to allow skipping EXIF data, defaulting to today's date if no other date is provided. - Improved the photo display in various components to utilize a unified caption format. - Refactored photo sorting and grouping logic for better organization in the UI.
This commit is contained in:
1 parent
c91317df8e
commit
0035d08149
12 files changed
+225
-36
No files matched your search
+55
-14
@@ -6,6 +6,7 @@ Handles progress photo uploads and retrieval.
|
||||
import os
|
||||
import uuid
|
||||
import logging
|
||||
from datetime import date as date_cls
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
@@ -14,6 +15,7 @@ from fastapi.responses import FileResponse
|
||||
import aiofiles
|
||||
|
||||
from db import get_db, get_cursor, r2d
|
||||
from photo_exif import extract_taken_at_from_image_bytes, taken_at_from_file_last_modified_ms
|
||||
from auth import require_auth, require_auth_flexible, check_feature_access, increment_feature_usage
|
||||
from routers.profiles import get_pid
|
||||
from feature_logger import log_feature_usage
|
||||
@@ -46,9 +48,20 @@ def resolve_photo_path(stored: Optional[str]) -> Optional[Path]:
|
||||
|
||||
|
||||
@router.post("")
|
||||
async def upload_photo(file: UploadFile=File(...), date: str=Form(""),
|
||||
x_profile_id: Optional[str]=Header(default=None), session: dict=Depends(require_auth)):
|
||||
"""Upload progress photo."""
|
||||
async def upload_photo(
|
||||
file: UploadFile = File(...),
|
||||
date: str = Form(""),
|
||||
skip_exif: str = Form(""),
|
||||
file_last_modified: str = Form(""),
|
||||
x_profile_id: Optional[str] = Header(default=None),
|
||||
session: dict = Depends(require_auth),
|
||||
):
|
||||
"""
|
||||
Upload progress photo.
|
||||
|
||||
Reihenfolge (wenn nicht ``skip_exif``): EXIF → Datei-Zeitstempel (Browser ``File.lastModified``)
|
||||
→ Formularfeld ``date`` → heute. Bei ``skip_exif``: nur Formular / heute (weder EXIF noch Datei-Zeit).
|
||||
"""
|
||||
pid = get_pid(x_profile_id)
|
||||
|
||||
# Phase 4: Check feature access and ENFORCE
|
||||
@@ -66,25 +79,53 @@ async def upload_photo(file: UploadFile=File(...), date: str=Form(""),
|
||||
f"Bitte kontaktiere den Admin oder warte bis zum nächsten Reset."
|
||||
)
|
||||
|
||||
raw = await file.read()
|
||||
ignore_exif = skip_exif.strip().lower() in ("1", "true", "yes", "on")
|
||||
taken_at = None
|
||||
exif_used = False
|
||||
file_mtime_used = False
|
||||
|
||||
if not ignore_exif:
|
||||
taken_at = extract_taken_at_from_image_bytes(raw)
|
||||
exif_used = taken_at is not None
|
||||
if not taken_at:
|
||||
taken_at = taken_at_from_file_last_modified_ms(file_last_modified)
|
||||
file_mtime_used = taken_at is not None
|
||||
|
||||
if taken_at:
|
||||
photo_date = taken_at.date()
|
||||
elif date and date.strip():
|
||||
photo_date = date_cls.fromisoformat(date.strip()[:10])
|
||||
else:
|
||||
photo_date = date_cls.today()
|
||||
|
||||
fid = str(uuid.uuid4())
|
||||
ext = Path(file.filename).suffix or '.jpg'
|
||||
ext = Path(file.filename).suffix or ".jpg"
|
||||
fname = f"{fid}{ext}"
|
||||
path = PHOTOS_DIR / fname
|
||||
async with aiofiles.open(path,'wb') as f: await f.write(await file.read())
|
||||
|
||||
# Convert empty string to NULL for date field
|
||||
photo_date = date if date and date.strip() else None
|
||||
async with aiofiles.open(path, "wb") as f:
|
||||
await f.write(raw)
|
||||
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
# Nur Dateiname in DB — Auflösung über PHOTOS_DIR (auch für ältere absolute Pfade: resolve_photo_path)
|
||||
cur.execute("INSERT INTO photos (id,profile_id,date,path,created) VALUES (%s,%s,%s,%s,CURRENT_TIMESTAMP)",
|
||||
(fid,pid,photo_date,fname))
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO photos (id, profile_id, date, path, taken_at, created)
|
||||
VALUES (%s, %s, %s, %s, %s, CURRENT_TIMESTAMP)
|
||||
""",
|
||||
(fid, pid, photo_date, fname, taken_at),
|
||||
)
|
||||
|
||||
# Phase 2: Increment usage counter
|
||||
increment_feature_usage(pid, 'photos')
|
||||
increment_feature_usage(pid, "photos")
|
||||
|
||||
return {"id":fid,"date":photo_date}
|
||||
return {
|
||||
"id": fid,
|
||||
"date": photo_date.isoformat(),
|
||||
"taken_at": taken_at.isoformat() if taken_at else None,
|
||||
"exif_used": exif_used,
|
||||
"file_mtime_used": file_mtime_used,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{fid}")
|
||||
@@ -114,7 +155,7 @@ def list_photos(x_profile_id: Optional[str]=Header(default=None), session: dict=
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT * FROM photos WHERE profile_id=%s
|
||||
ORDER BY date DESC NULLS LAST, created DESC
|
||||
ORDER BY COALESCE(taken_at, date::timestamptz, created) DESC NULLS LAST
|
||||
LIMIT 500
|
||||
""",
|
||||
(pid,),
|
||||
|
||||
Reference in new issue
Block a user