Support for geo search
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import re
|
||||
import pickle
|
||||
from secrets import token_urlsafe
|
||||
from typing import List, Union
|
||||
from magic import Magic
|
||||
from datetime import datetime, timedelta
|
||||
from os import makedirs, path, remove, system
|
||||
from classes.models import Photo, SearchResults
|
||||
from modules.exif_reader import extract_location
|
||||
from modules.hasher import get_phash, get_duplicates
|
||||
from modules.scheduler import scheduler
|
||||
from modules.security import User, get_current_active_user
|
||||
@@ -15,7 +17,7 @@ from bson.errors import InvalidId
|
||||
|
||||
from fastapi import HTTPException, UploadFile, Security
|
||||
from fastapi.responses import UJSONResponse, Response
|
||||
from starlette.status import HTTP_204_NO_CONTENT, HTTP_400_BAD_REQUEST, HTTP_401_UNAUTHORIZED, HTTP_404_NOT_FOUND, HTTP_409_CONFLICT
|
||||
from starlette.status import HTTP_204_NO_CONTENT, HTTP_400_BAD_REQUEST, HTTP_401_UNAUTHORIZED, HTTP_404_NOT_FOUND, HTTP_409_CONFLICT, HTTP_422_UNPROCESSABLE_ENTITY
|
||||
|
||||
from modules.utils import logWrite
|
||||
|
||||
@@ -72,7 +74,8 @@ async def photo_upload(file: UploadFile, album: str, ignore_duplicates: bool = F
|
||||
status_code=HTTP_409_CONFLICT
|
||||
)
|
||||
|
||||
uploaded = col_photos.insert_one( {"user": current_user.user, "album": album, "hash": file_hash, "filename": filename} )
|
||||
coords = extract_location(path.join("data", "users", current_user.user, "albums", album, filename))
|
||||
uploaded = col_photos.insert_one( {"user": current_user.user, "album": album, "hash": file_hash, "filename": filename, "location": [coords["lng"], coords["lat"], coords["alt"]]} )
|
||||
|
||||
if compress is True:
|
||||
scheduler.add_job(compress_image, trigger="date", run_date=datetime.now()+timedelta(seconds=1), args=[path.join("data", "users", current_user.user, "albums", album, filename)])
|
||||
@@ -124,7 +127,7 @@ async def photo_delete(id: str, current_user: User = Security(get_current_active
|
||||
return Response(status_code=HTTP_204_NO_CONTENT)
|
||||
|
||||
@app.get("/albums/{album}/photos", response_class=UJSONResponse, response_model=SearchResults, description="Find a photo by filename")
|
||||
async def photo_find(q: str, album: str, page: int = 1, page_size: int = 100, current_user: User = Security(get_current_active_user, scopes=["photos.list"])):
|
||||
async def photo_find(album: str, q: 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:
|
||||
return HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Provided album '{album}' does not exist.")
|
||||
@@ -134,12 +137,24 @@ async def photo_find(q: str, album: str, page: int = 1, page_size: int = 100, cu
|
||||
|
||||
output = {"results": []}
|
||||
skip = (page-1)*page_size
|
||||
images = list(col_photos.find({"user": current_user.user, "album": album, "filename": re.compile(q)}, limit=page_size, skip=skip))
|
||||
|
||||
radius = 5000 if radius is None else radius
|
||||
|
||||
if (lat is not None) and (lng is not None):
|
||||
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:
|
||||
raise HTTPException(status_code=HTTP_422_UNPROCESSABLE_ENTITY, detail="You must provide query or coordinates to look for photos")
|
||||
else:
|
||||
db_query = {"user": current_user.user, "album": album, "filename": re.compile(q)}
|
||||
db_query_count = {"user": current_user.user, "album": album, "filename": re.compile(q)}
|
||||
|
||||
images = list(col_photos.find(db_query, limit=page_size, skip=skip))
|
||||
|
||||
for image in images:
|
||||
output["results"].append({"id": image["_id"].__str__(), "filename": image["filename"]})
|
||||
|
||||
if col_photos.count_documents( {"user": current_user.user, "album": album, "filename": re.compile(q)} ) > page*page_size:
|
||||
if col_photos.count_documents( db_query_count ) > page*page_size:
|
||||
token = str(token_urlsafe(32))
|
||||
col_tokens.insert_one( {"token": token, "query": q, "album": album, "page": page+1, "page_size": page_size, "user": pickle.dumps(current_user)} )
|
||||
output["next_page"] = f"/albums/{album}/photos/token?token={token}" # type: ignore
|
||||
|
Reference in New Issue
Block a user