forked from AOSSIE-Org/PictoPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimages.py
More file actions
142 lines (121 loc) · 3.95 KB
/
Copy pathimages.py
File metadata and controls
142 lines (121 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from fastapi import APIRouter, HTTPException, Query, status
from typing import List, Optional
from app.database.images import db_get_all_images
from app.schemas.images import ErrorResponse
from app.utils.images import image_util_parse_metadata
from pydantic import BaseModel
from app.database.images import db_toggle_image_favourite_status, db_get_image_by_id
from app.logging.setup_logging import get_logger
# Initialize logger
logger = get_logger(__name__)
router = APIRouter()
# Response Models
class MetadataModel(BaseModel):
name: str
date_created: Optional[str]
width: int
height: int
file_location: str
file_size: int
item_type: str
latitude: Optional[float] = None
longitude: Optional[float] = None
location: Optional[str] = None
class ImageData(BaseModel):
id: str
path: str
folder_id: str
thumbnailPath: str
metadata: MetadataModel
isTagged: bool
isFavourite: bool
tags: Optional[List[str]] = None
class GetAllImagesResponse(BaseModel):
success: bool
message: str
data: List[ImageData]
@router.get(
"/",
response_model=GetAllImagesResponse,
responses={500: {"model": ErrorResponse}},
)
def get_all_images(
tagged: Optional[bool] = Query(None, description="Filter images by tagged status")
):
"""Get all images from the database."""
try:
# Get all images with tags from database (single query with optional filter)
images = db_get_all_images(tagged=tagged)
# Convert to response format
image_data = [
ImageData(
id=image["id"],
path=image["path"],
folder_id=image["folder_id"],
thumbnailPath=image["thumbnailPath"],
metadata=image_util_parse_metadata(image["metadata"]),
isTagged=image["isTagged"],
isFavourite=image.get("isFavourite", False),
tags=image["tags"],
)
for image in images
]
return GetAllImagesResponse(
success=True,
message=f"Successfully retrieved {len(image_data)} images",
data=image_data,
)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=ErrorResponse(
success=False,
error="Internal server error",
message=f"Unable to retrieve images: {str(e)}",
).model_dump(),
)
# adding add to favourite and remove from favourite routes
class ToggleFavouriteRequest(BaseModel):
image_id: str
@router.post("/toggle-favourite")
def toggle_favourite(req: ToggleFavouriteRequest):
"""
Toggle the favorite status of an image.
"""
image_id = req.image_id
try:
success = db_toggle_image_favourite_status(image_id)
if not success:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Image not found or failed to toggle",
)
# Fetch updated status to return
image = db_get_image_by_id(image_id)
if not image:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Image not found after toggle",
)
return {
"success": True,
"image_id": image_id,
"isFavourite": image.get("isFavourite", False),
}
except HTTPException:
raise # Re-raise HTTPExceptions to preserve status codes
except Exception as e:
logger.error(f"error in /toggle-favourite route: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Internal server error: {e}",
)
class ImageInfoResponse(BaseModel):
id: str
path: str
folder_id: str
thumbnailPath: str
metadata: MetadataModel
isTagged: bool
isFavourite: bool
tags: Optional[List[str]] = None