Exceptions and type handling done
This commit is contained in:
@@ -7,7 +7,7 @@ from typing import Union
|
||||
from magic import Magic
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from os import makedirs, path, remove, system
|
||||
from classes.exceptions import AlbumNameNotFoundError, PhotoNotFoundError, SearchPageInvalidError, SearchTokenInvalidError
|
||||
from classes.exceptions import AlbumNameNotFoundError, PhotoNotFoundError, PhotoSearchQueryEmptyError, SearchPageInvalidError, SearchTokenInvalidError
|
||||
from classes.models import Photo, PhotoPublic, SearchResultsPhoto
|
||||
from modules.exif_reader import extract_location
|
||||
from modules.hasher import get_phash, get_duplicates
|
||||
@@ -20,9 +20,9 @@ from bson.objectid import ObjectId
|
||||
from bson.errors import InvalidId
|
||||
from plum.exceptions import UnpackError
|
||||
|
||||
from fastapi import HTTPException, UploadFile, Security
|
||||
from fastapi import UploadFile, Security
|
||||
from fastapi.responses import UJSONResponse, Response
|
||||
from starlette.status import HTTP_204_NO_CONTENT, HTTP_409_CONFLICT, HTTP_422_UNPROCESSABLE_ENTITY
|
||||
from starlette.status import HTTP_204_NO_CONTENT, HTTP_409_CONFLICT
|
||||
|
||||
from modules.utils import logWrite
|
||||
|
||||
@@ -52,7 +52,7 @@ async def compress_image(image_path: str):
|
||||
size_after = path.getsize(image_path) / 1024
|
||||
logWrite(f"Compressed '{path.split(image_path)[-1]}' from {size_before} Kb to {size_after} Kb")
|
||||
|
||||
photo_post_reponses = {
|
||||
photo_post_responses = {
|
||||
404: AlbumNameNotFoundError("name").openapi,
|
||||
409: {
|
||||
"description": "Image Duplicates Found",
|
||||
@@ -68,7 +68,7 @@ photo_post_reponses = {
|
||||
}
|
||||
}
|
||||
}
|
||||
@app.post("/albums/{album}/photos", description="Upload a photo to album", response_class=UJSONResponse, response_model=Photo, responses=photo_post_reponses)
|
||||
@app.post("/albums/{album}/photos", description="Upload a photo to album", response_class=UJSONResponse, response_model=Photo, responses=photo_post_responses)
|
||||
async def photo_upload(file: UploadFile, album: str, ignore_duplicates: bool = False, compress: bool = True, caption: Union[str, None] = None, current_user: User = Security(get_current_active_user, scopes=["photos.write"])):
|
||||
|
||||
if col_albums.find_one( {"user": current_user.user, "name": album} ) is None:
|
||||
@@ -142,7 +142,10 @@ async def photo_upload(file: UploadFile, album: str, ignore_duplicates: bool = F
|
||||
}
|
||||
)
|
||||
|
||||
@app.get("/photos/{id}", description="Get a photo by id")
|
||||
photo_get_responses = {
|
||||
404: PhotoNotFoundError("id").openapi
|
||||
}
|
||||
@app.get("/photos/{id}", description="Get a photo by id", responses=photo_get_responses)
|
||||
async def photo_get(id: str, current_user: User = Security(get_current_active_user, scopes=["photos.read"])):
|
||||
|
||||
try:
|
||||
@@ -243,11 +246,12 @@ async def photo_delete(id: str, current_user: User = Security(get_current_active
|
||||
|
||||
return Response(status_code=HTTP_204_NO_CONTENT)
|
||||
|
||||
photo_find_reponses = {
|
||||
photo_find_responses = {
|
||||
400: SearchPageInvalidError().openapi,
|
||||
404: AlbumNameNotFoundError("name").openapi
|
||||
404: AlbumNameNotFoundError("name").openapi,
|
||||
422: PhotoSearchQueryEmptyError().openapi
|
||||
}
|
||||
@app.get("/albums/{album}/photos", description="Find a photo by filename", response_class=UJSONResponse, response_model=SearchResultsPhoto, responses=photo_find_reponses)
|
||||
@app.get("/albums/{album}/photos", description="Find a photo by filename", response_class=UJSONResponse, response_model=SearchResultsPhoto, responses=photo_find_responses)
|
||||
async def photo_find(album: str, q: Union[str, None] = None, caption: Union[str, None] = None, page: int = 1, page_size: int = 100, lat: Union[float, None] = None, lng: Union[float, None] = None, radius: Union[int, None] = None, current_user: User = Security(get_current_active_user, scopes=["photos.list"])):
|
||||
|
||||
if col_albums.find_one( {"user": current_user.user, "name": album} ) is None:
|
||||
@@ -265,7 +269,7 @@ async def photo_find(album: str, q: Union[str, None] = None, caption: Union[str,
|
||||
db_query = {"user": current_user.user, "album": album, "location": { "$nearSphere": {"$geometry": {"type": "Point", "coordinates": [lng, lat]}, "$maxDistance": radius} } }
|
||||
db_query_count = {"user": current_user.user, "album": album, "location": { "$geoWithin": { "$centerSphere": [ [lng, lat], radius ] } } }
|
||||
elif q is None and caption is None:
|
||||
raise HTTPException(status_code=HTTP_422_UNPROCESSABLE_ENTITY, detail="You must provide query, caption or coordinates to look for photos")
|
||||
raise PhotoSearchQueryEmptyError()
|
||||
elif q is None and caption is not None:
|
||||
db_query = {"user": current_user.user, "album": album, "caption": re.compile(caption)}
|
||||
db_query_count = {"user": current_user.user, "album": album, "caption": re.compile(caption)}
|
||||
@@ -290,7 +294,10 @@ async def photo_find(album: str, q: Union[str, None] = None, caption: Union[str,
|
||||
|
||||
return UJSONResponse(output)
|
||||
|
||||
@app.get("/albums/{album}/photos/token", description="Find a photo by token", response_class=UJSONResponse, response_model=SearchResultsPhoto)
|
||||
photo_find_token_responses = {
|
||||
401: SearchTokenInvalidError().openapi
|
||||
}
|
||||
@app.get("/albums/{album}/photos/token", description="Find a photo by token", response_class=UJSONResponse, response_model=SearchResultsPhoto, responses=photo_find_token_responses)
|
||||
async def photo_find_token(token: str):
|
||||
|
||||
found_record = col_tokens.find_one( {"token": token} )
|
||||
|
Reference in New Issue
Block a user