phase 22
All checks were successful
CI/CD Pipeline / VM Test - backend-integration (push) Successful in 12s
CI/CD Pipeline / VM Test - full-stack (push) Successful in 9s
CI/CD Pipeline / VM Test - performance (push) Successful in 9s
CI/CD Pipeline / VM Test - security (push) Successful in 9s
CI/CD Pipeline / Backend Linting (push) Successful in 3s
CI/CD Pipeline / Frontend Linting (push) Successful in 24s
CI/CD Pipeline / Nix Flake Check (push) Successful in 53s
CI/CD Pipeline / CI Summary (push) Successful in 1s
CI/CD Pipeline / VM Test - backend-integration (pull_request) Successful in 2s
CI/CD Pipeline / VM Test - full-stack (pull_request) Successful in 2s
CI/CD Pipeline / VM Test - performance (pull_request) Successful in 2s
CI/CD Pipeline / VM Test - security (pull_request) Successful in 2s
CI/CD Pipeline / Backend Linting (pull_request) Successful in 2s
CI/CD Pipeline / Frontend Linting (pull_request) Successful in 16s
CI/CD Pipeline / Nix Flake Check (pull_request) Successful in 38s
CI/CD Pipeline / CI Summary (pull_request) Successful in 0s
All checks were successful
CI/CD Pipeline / VM Test - backend-integration (push) Successful in 12s
CI/CD Pipeline / VM Test - full-stack (push) Successful in 9s
CI/CD Pipeline / VM Test - performance (push) Successful in 9s
CI/CD Pipeline / VM Test - security (push) Successful in 9s
CI/CD Pipeline / Backend Linting (push) Successful in 3s
CI/CD Pipeline / Frontend Linting (push) Successful in 24s
CI/CD Pipeline / Nix Flake Check (push) Successful in 53s
CI/CD Pipeline / CI Summary (push) Successful in 1s
CI/CD Pipeline / VM Test - backend-integration (pull_request) Successful in 2s
CI/CD Pipeline / VM Test - full-stack (pull_request) Successful in 2s
CI/CD Pipeline / VM Test - performance (pull_request) Successful in 2s
CI/CD Pipeline / VM Test - security (pull_request) Successful in 2s
CI/CD Pipeline / Backend Linting (pull_request) Successful in 2s
CI/CD Pipeline / Frontend Linting (pull_request) Successful in 16s
CI/CD Pipeline / Nix Flake Check (pull_request) Successful in 38s
CI/CD Pipeline / CI Summary (pull_request) Successful in 0s
This commit is contained in:
74
backend/app/images/search.py
Normal file
74
backend/app/images/search.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""Image search and filtering functionality."""
|
||||
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database.models.image import Image
|
||||
|
||||
|
||||
def search_images(
|
||||
user_id: str,
|
||||
db: Session,
|
||||
query: str | None = None,
|
||||
limit: int = 50,
|
||||
offset: int = 0,
|
||||
) -> list[Image]:
|
||||
"""
|
||||
Search user's image library with optional filters.
|
||||
|
||||
Args:
|
||||
user_id: User UUID
|
||||
db: Database session
|
||||
query: Search query (searches filename)
|
||||
limit: Maximum results (default 50)
|
||||
offset: Pagination offset (default 0)
|
||||
|
||||
Returns:
|
||||
List of matching images
|
||||
"""
|
||||
# Base query - get user's images
|
||||
stmt = db.query(Image).filter(Image.user_id == user_id)
|
||||
|
||||
# Add search filter if query provided
|
||||
if query:
|
||||
search_term = f"%{query}%"
|
||||
stmt = stmt.filter(
|
||||
or_(
|
||||
Image.filename.ilike(search_term),
|
||||
Image.image_metadata["format"].astext.ilike(search_term),
|
||||
)
|
||||
)
|
||||
|
||||
# Order by most recently uploaded
|
||||
stmt = stmt.order_by(Image.created_at.desc())
|
||||
|
||||
# Apply pagination
|
||||
stmt = stmt.limit(limit).offset(offset)
|
||||
|
||||
return stmt.all()
|
||||
|
||||
|
||||
def count_images(user_id: str, db: Session, query: str | None = None) -> int:
|
||||
"""
|
||||
Count images matching search criteria.
|
||||
|
||||
Args:
|
||||
user_id: User UUID
|
||||
db: Database session
|
||||
query: Search query (optional)
|
||||
|
||||
Returns:
|
||||
Count of matching images
|
||||
"""
|
||||
stmt = db.query(Image).filter(Image.user_id == user_id)
|
||||
|
||||
if query:
|
||||
search_term = f"%{query}%"
|
||||
stmt = stmt.filter(
|
||||
or_(
|
||||
Image.filename.ilike(search_term),
|
||||
Image.image_metadata["format"].astext.ilike(search_term),
|
||||
)
|
||||
)
|
||||
|
||||
return stmt.count()
|
||||
103
backend/app/images/serve.py
Normal file
103
backend/app/images/serve.py
Normal file
@@ -0,0 +1,103 @@
|
||||
"""Image serving with quality-based thumbnail selection."""
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from app.database.models.image import Image
|
||||
|
||||
|
||||
def get_thumbnail_path(image: Image, quality: str) -> str:
|
||||
"""
|
||||
Get thumbnail path for specified quality level.
|
||||
|
||||
Args:
|
||||
image: Image model instance
|
||||
quality: Quality level ('low', 'medium', 'high', 'original')
|
||||
|
||||
Returns:
|
||||
Storage path to thumbnail
|
||||
|
||||
Raises:
|
||||
ValueError: If quality level is invalid
|
||||
"""
|
||||
if quality == "original":
|
||||
return image.storage_path
|
||||
|
||||
# Get thumbnail paths from metadata
|
||||
thumbnails = image.image_metadata.get("thumbnails", {})
|
||||
|
||||
# Map quality to thumbnail size
|
||||
if quality == "low":
|
||||
thumbnail_path = thumbnails.get("low")
|
||||
elif quality == "medium":
|
||||
thumbnail_path = thumbnails.get("medium")
|
||||
elif quality == "high":
|
||||
thumbnail_path = thumbnails.get("high")
|
||||
else:
|
||||
raise ValueError(f"Invalid quality level: {quality}")
|
||||
|
||||
# Fall back to original if thumbnail doesn't exist
|
||||
if not thumbnail_path:
|
||||
return image.storage_path
|
||||
|
||||
return thumbnail_path
|
||||
|
||||
|
||||
async def serve_image_with_quality(
|
||||
image: Image, quality: str = "medium", filename: str | None = None
|
||||
) -> StreamingResponse:
|
||||
"""
|
||||
Serve image with specified quality level.
|
||||
|
||||
Args:
|
||||
image: Image model instance
|
||||
quality: Quality level ('low', 'medium', 'high', 'original')
|
||||
filename: Optional custom filename for download
|
||||
|
||||
Returns:
|
||||
StreamingResponse with image data
|
||||
|
||||
Raises:
|
||||
HTTPException: If image cannot be served
|
||||
"""
|
||||
from app.images.download import download_single_image
|
||||
|
||||
try:
|
||||
# Get appropriate thumbnail path
|
||||
storage_path = get_thumbnail_path(image, quality)
|
||||
|
||||
# Use original filename if not specified
|
||||
if filename is None:
|
||||
filename = image.filename
|
||||
|
||||
# Serve the image
|
||||
return await download_single_image(storage_path, filename)
|
||||
|
||||
except ValueError as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=str(e),
|
||||
) from e
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to serve image: {str(e)}",
|
||||
) from e
|
||||
|
||||
|
||||
def determine_quality_from_speed(speed_mbps: float) -> str:
|
||||
"""
|
||||
Determine appropriate quality level based on connection speed.
|
||||
|
||||
Args:
|
||||
speed_mbps: Connection speed in Mbps
|
||||
|
||||
Returns:
|
||||
Quality level string
|
||||
"""
|
||||
if speed_mbps < 1.0:
|
||||
return "low"
|
||||
elif speed_mbps < 5.0:
|
||||
return "medium"
|
||||
else:
|
||||
return "high"
|
||||
Reference in New Issue
Block a user