PhotosAPI/extensions/videos.py

458 lines
12 KiB
Python
Raw Normal View History

2023-06-22 14:17:53 +03:00
import re
from datetime import datetime, timezone
2023-06-23 12:17:02 +03:00
from os import makedirs, remove
from pathlib import Path
2023-06-27 14:51:18 +03:00
from random import randint
2022-12-21 00:59:35 +02:00
from secrets import token_urlsafe
2023-01-05 17:38:00 +02:00
from shutil import move
2023-01-17 15:39:21 +02:00
from typing import Union
2023-06-22 14:17:53 +03:00
import aiofiles
from bson.errors import InvalidId
from bson.objectid import ObjectId
from fastapi import Security, UploadFile
from fastapi.responses import Response, UJSONResponse
2022-12-21 00:59:35 +02:00
from magic import Magic
2023-06-22 14:17:53 +03:00
from pymongo import DESCENDING
from starlette.status import HTTP_204_NO_CONTENT
2023-03-12 15:59:13 +02:00
from classes.exceptions import (
AlbumNameNotFoundError,
2023-06-27 14:51:18 +03:00
SearchLimitInvalidError,
2023-03-12 15:59:13 +02:00
SearchPageInvalidError,
SearchTokenInvalidError,
2023-11-25 18:50:09 +02:00
UserMediaQuotaReached,
2023-03-12 15:59:13 +02:00
VideoNotFoundError,
VideoSearchQueryEmptyError,
)
2023-06-27 14:51:18 +03:00
from classes.models import (
RandomSearchResultsVideo,
SearchResultsVideo,
Video,
VideoPublic,
)
2022-12-21 00:59:35 +02:00
from modules.app import app
2023-11-25 18:50:09 +02:00
from modules.database import col_albums, col_photos, col_tokens, col_videos
2023-06-22 14:17:53 +03:00
from modules.security import User, get_current_active_user
2022-12-21 00:59:35 +02:00
2023-11-25 18:50:09 +02:00
video_post_responses = {
403: UserMediaQuotaReached().openapi,
404: AlbumNameNotFoundError("name").openapi,
}
2023-03-12 15:59:13 +02:00
@app.post(
"/albums/{album}/videos",
description="Upload a video to album",
response_class=UJSONResponse,
response_model=Video,
responses=video_post_responses,
)
async def video_upload(
file: UploadFile,
album: str,
caption: Union[str, None] = None,
current_user: User = Security(get_current_active_user, scopes=["videos.write"]),
):
2023-08-14 14:44:07 +03:00
if (await col_albums.find_one({"user": current_user.user, "name": album})) is None:
2023-02-16 16:44:54 +02:00
raise AlbumNameNotFoundError(album)
2022-12-21 00:59:35 +02:00
2023-11-25 18:50:09 +02:00
user_media_count = (
await col_videos.count_documents({"user": current_user.user})
) + (await col_photos.count_documents({"user": current_user.user}))
if user_media_count >= current_user.quota and not current_user.quota == -1: # type: ignore
raise UserMediaQuotaReached()
2023-06-23 11:51:42 +03:00
makedirs(Path(f"data/users/{current_user.user}/albums/{album}"), exist_ok=True)
2022-12-21 00:59:35 +02:00
filename = file.filename
2023-06-23 11:51:42 +03:00
if Path(f"data/users/{current_user.user}/albums/{album}/{file.filename}").exists():
2022-12-21 00:59:35 +02:00
base_name = file.filename.split(".")[:-1]
extension = file.filename.split(".")[-1]
2023-03-12 15:59:13 +02:00
filename = (
".".join(base_name) + f"_{int(datetime.now().timestamp())}." + extension
)
2022-12-21 00:59:35 +02:00
2023-06-22 14:16:12 +03:00
async with aiofiles.open(
2023-06-23 11:51:42 +03:00
Path(f"data/users/{current_user.user}/albums/{album}/{filename}"), "wb"
2023-03-12 15:59:13 +02:00
) as f:
2023-06-22 14:16:12 +03:00
await f.write(await file.read())
2022-12-21 00:59:35 +02:00
2023-02-16 16:50:02 +02:00
# Hashing and duplicates check should be here
2023-03-12 15:59:13 +02:00
2023-02-16 16:50:02 +02:00
# Coords extraction should be here
2023-08-14 14:44:07 +03:00
uploaded = await col_videos.insert_one(
2023-01-10 16:23:49 +02:00
{
"user": current_user.user,
"album": album,
"filename": filename,
"dates": {
2023-01-25 17:02:28 +02:00
"uploaded": datetime.now(tz=timezone.utc),
2023-03-12 15:59:13 +02:00
"modified": datetime.now(tz=timezone.utc),
2023-01-10 16:23:49 +02:00
},
2023-03-12 15:59:13 +02:00
"caption": caption,
2023-01-10 16:23:49 +02:00
}
)
2022-12-21 00:59:35 +02:00
return UJSONResponse(
{
"id": uploaded.inserted_id.__str__(),
"album": album,
2023-03-12 15:59:13 +02:00
"hash": "", # SHOULD BE DONE
"filename": filename,
2022-12-21 00:59:35 +02:00
}
)
2023-06-22 14:51:04 +03:00
video_get_responses = {
2023-06-22 15:43:00 +03:00
200: {
"content": {
"application/octet-stream": {
"schema": {
"type": "string",
"format": "binary",
"contentMediaType": "video/*",
}
}
}
},
2023-06-22 14:51:04 +03:00
404: VideoNotFoundError("id").openapi,
}
2023-03-12 15:59:13 +02:00
2023-06-22 14:51:04 +03:00
@app.get(
"/videos/{id}",
description="Get a video by id",
responses=video_get_responses,
response_class=Response,
)
2023-03-12 15:59:13 +02:00
async def video_get(
id: str,
current_user: User = Security(get_current_active_user, scopes=["videos.read"]),
):
2022-12-21 00:59:35 +02:00
try:
2023-08-14 14:44:07 +03:00
video = await col_videos.find_one({"_id": ObjectId(id)})
2022-12-21 00:59:35 +02:00
if video is None:
raise InvalidId(id)
2023-08-14 14:44:07 +03:00
except InvalidId as exc:
raise VideoNotFoundError(id) from exc
2022-12-21 00:59:35 +02:00
2023-06-23 11:51:42 +03:00
video_path = Path(
f"data/users/{current_user.user}/albums/{video['album']}/{video['filename']}"
2023-03-12 15:59:13 +02:00
)
2022-12-21 00:59:35 +02:00
mime = Magic(mime=True).from_file(video_path)
2023-06-22 14:51:04 +03:00
async with aiofiles.open(video_path, "rb") as f:
2023-06-22 14:16:12 +03:00
video_file = await f.read()
2022-12-21 00:59:35 +02:00
2023-06-22 14:51:04 +03:00
return Response(content=video_file, media_type=mime)
2022-12-21 00:59:35 +02:00
2023-01-05 17:38:00 +02:00
2023-03-12 15:59:13 +02:00
video_move_responses = {404: VideoNotFoundError("id").openapi}
@app.put(
"/videos/{id}",
description="Move a video into another album",
response_model=VideoPublic,
responses=video_move_responses,
)
async def video_move(
id: str,
album: str,
current_user: User = Security(get_current_active_user, scopes=["videos.write"]),
):
2023-01-05 17:38:00 +02:00
try:
2023-08-14 14:44:07 +03:00
video = await col_videos.find_one({"_id": ObjectId(id)})
2023-01-05 17:38:00 +02:00
if video is None:
raise InvalidId(id)
2023-08-14 14:44:07 +03:00
except InvalidId as exc:
raise VideoNotFoundError(id) from exc
2023-01-05 17:38:00 +02:00
2023-08-14 14:44:07 +03:00
if (await col_albums.find_one({"user": current_user.user, "name": album})) is None:
2023-02-16 16:44:54 +02:00
raise AlbumNameNotFoundError(album)
2023-01-05 17:38:00 +02:00
2023-06-23 11:51:42 +03:00
if Path(
2023-06-23 12:30:18 +03:00
f"data/users/{current_user.user}/albums/{album}/{video['filename']}"
).exists():
2023-01-05 17:38:00 +02:00
base_name = video["filename"].split(".")[:-1]
extension = video["filename"].split(".")[-1]
2023-03-12 15:59:13 +02:00
filename = (
".".join(base_name) + f"_{int(datetime.now().timestamp())}." + extension
)
2023-01-05 17:38:00 +02:00
else:
filename = video["filename"]
2023-08-14 14:44:07 +03:00
await col_videos.find_one_and_update(
2023-03-12 15:59:13 +02:00
{"_id": ObjectId(id)},
{
"$set": {
"album": album,
"filename": filename,
"dates.modified": datetime.now(tz=timezone.utc),
}
},
)
2023-01-05 17:38:00 +02:00
move(
2023-06-23 12:30:18 +03:00
Path(
f"data/users/{current_user.user}/albums/{video['album']}/{video['filename']}"
),
2023-06-23 11:51:42 +03:00
Path(f"data/users/{current_user.user}/albums/{album}/{filename}"),
2023-01-05 17:38:00 +02:00
)
return UJSONResponse(
{
"id": video["_id"].__str__(),
2023-02-16 16:44:54 +02:00
"caption": video["caption"],
2023-03-12 15:59:13 +02:00
"filename": filename,
2023-01-05 17:38:00 +02:00
}
)
2023-01-17 15:39:21 +02:00
2023-03-12 15:59:13 +02:00
video_patch_responses = {404: VideoNotFoundError("id").openapi}
@app.patch(
"/videos/{id}",
description="Change properties of a video",
response_model=VideoPublic,
responses=video_patch_responses,
)
async def video_patch(
id: str,
caption: str,
current_user: User = Security(get_current_active_user, scopes=["videos.write"]),
):
2023-01-17 15:39:21 +02:00
try:
2023-08-14 14:44:07 +03:00
video = await col_videos.find_one({"_id": ObjectId(id)})
2023-01-17 15:39:21 +02:00
if video is None:
raise InvalidId(id)
2023-08-14 14:44:07 +03:00
except InvalidId as exc:
raise VideoNotFoundError(id) from exc
2023-01-17 15:39:21 +02:00
2023-08-14 14:44:07 +03:00
await col_videos.find_one_and_update(
2023-03-12 15:59:13 +02:00
{"_id": ObjectId(id)},
{"$set": {"caption": caption, "dates.modified": datetime.now(tz=timezone.utc)}},
)
2023-01-17 15:39:21 +02:00
return UJSONResponse(
{
"id": video["_id"].__str__(),
2023-02-16 16:44:54 +02:00
"caption": video["caption"],
2023-03-12 15:59:13 +02:00
"filename": video["filename"],
2023-01-17 15:39:21 +02:00
}
)
2022-12-21 00:59:35 +02:00
2023-03-12 15:59:13 +02:00
video_delete_responses = {404: VideoNotFoundError("id").openapi}
@app.delete(
"/videos/{id}",
description="Delete a video by id",
status_code=HTTP_204_NO_CONTENT,
responses=video_delete_responses,
)
async def video_delete(
id: str,
current_user: User = Security(get_current_active_user, scopes=["videos.write"]),
):
2022-12-21 00:59:35 +02:00
try:
2023-08-14 14:44:07 +03:00
video = await col_videos.find_one_and_delete({"_id": ObjectId(id)})
2022-12-21 00:59:35 +02:00
if video is None:
raise InvalidId(id)
2023-08-14 14:44:07 +03:00
except InvalidId as exc:
raise VideoNotFoundError(id) from exc
2022-12-21 00:59:35 +02:00
2023-08-14 14:44:07 +03:00
album = await col_albums.find_one({"name": video["album"]})
2023-03-12 15:59:13 +02:00
remove(
2023-06-23 12:30:18 +03:00
Path(
f"data/users/{current_user.user}/albums/{video['album']}/{video['filename']}"
)
2023-03-12 15:59:13 +02:00
)
2022-12-21 00:59:35 +02:00
return Response(status_code=HTTP_204_NO_CONTENT)
2023-03-12 15:59:13 +02:00
2023-06-27 14:51:18 +03:00
video_random_responses = {
400: SearchLimitInvalidError().openapi,
404: AlbumNameNotFoundError("name").openapi,
}
@app.get(
"/albums/{album}/videos/random",
description="Get one random video, optionally by caption",
response_class=UJSONResponse,
response_model=RandomSearchResultsVideo,
responses=video_random_responses,
)
async def video_random(
album: str,
caption: Union[str, None] = None,
limit: int = 100,
current_user: User = Security(get_current_active_user, scopes=["videos.list"]),
):
2023-08-14 14:44:07 +03:00
if (await col_albums.find_one({"user": current_user.user, "name": album})) is None:
2023-06-27 14:51:18 +03:00
raise AlbumNameNotFoundError(album)
if limit <= 0:
raise SearchLimitInvalidError()
output = {"results": []}
db_query = (
{
"user": current_user.user,
"album": album,
"caption": re.compile(caption),
}
if caption is not None
else {
"user": current_user.user,
"album": album,
}
)
2023-08-14 14:44:07 +03:00
documents_count = await col_videos.count_documents(db_query)
2023-06-27 14:51:18 +03:00
skip = randint(0, documents_count - 1) if documents_count > 1 else 0
2023-08-14 14:44:07 +03:00
async for video in col_videos.aggregate(
[
{"$match": db_query},
{"$skip": skip},
{"$limit": limit},
]
):
2023-06-27 14:51:18 +03:00
output["results"].append(
{
"id": video["_id"].__str__(),
"filename": video["filename"],
"caption": video["caption"],
}
)
return UJSONResponse(output)
2023-02-16 16:44:54 +02:00
video_find_responses = {
400: SearchPageInvalidError().openapi,
2023-03-23 13:34:18 +02:00
401: SearchTokenInvalidError().openapi,
2023-02-16 16:44:54 +02:00
404: AlbumNameNotFoundError("name").openapi,
2023-03-12 15:59:13 +02:00
422: VideoSearchQueryEmptyError().openapi,
2023-02-16 16:44:54 +02:00
}
2022-12-21 00:59:35 +02:00
2023-03-12 15:59:13 +02:00
@app.get(
"/albums/{album}/videos",
2023-03-23 13:34:18 +02:00
description="Find a video by filename, caption or token",
2023-03-12 15:59:13 +02:00
response_class=UJSONResponse,
response_model=SearchResultsVideo,
responses=video_find_responses,
)
async def video_find(
album: str,
q: Union[str, None] = None,
caption: Union[str, None] = None,
2023-03-23 13:34:18 +02:00
token: Union[str, None] = None,
2023-03-12 15:59:13 +02:00
page: int = 1,
page_size: int = 100,
current_user: User = Security(get_current_active_user, scopes=["videos.list"]),
):
2023-03-23 13:34:18 +02:00
if token is not None:
2023-08-14 14:44:07 +03:00
found_record = await col_tokens.find_one({"token": token})
2023-03-23 13:34:18 +02:00
if found_record is None:
raise SearchTokenInvalidError()
return await video_find(
album=album,
q=found_record["query"],
caption=found_record["caption"],
page=found_record["page"],
page_size=found_record["page_size"],
current_user=current_user,
)
2023-08-14 14:44:07 +03:00
if (await col_albums.find_one({"user": current_user.user, "name": album})) is None:
2023-02-16 16:44:54 +02:00
raise AlbumNameNotFoundError(album)
2022-12-21 00:59:35 +02:00
if page <= 0 or page_size <= 0:
2023-02-16 16:44:54 +02:00
raise SearchPageInvalidError()
2022-12-21 00:59:35 +02:00
output = {"results": []}
2023-03-12 15:59:13 +02:00
skip = (page - 1) * page_size
2023-01-17 15:39:21 +02:00
if q is None and caption is None:
2023-02-16 16:44:54 +02:00
raise VideoSearchQueryEmptyError()
2023-03-12 15:59:13 +02:00
2023-06-23 13:17:01 +03:00
if q is None:
2023-03-12 15:59:13 +02:00
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),
}
2023-06-23 13:17:01 +03:00
elif caption is None:
2023-08-14 14:55:49 +03:00
db_query = {
"user": current_user.user,
"album": album,
"filename": re.compile(q),
}
2023-03-12 15:59:13 +02:00
db_query_count = {
"user": current_user.user,
"album": album,
"caption": re.compile(q),
}
2023-01-17 15:39:21 +02:00
else:
2023-08-14 14:55:49 +03:00
db_query = {
"user": current_user.user,
"album": album,
"filename": re.compile(q),
"caption": re.compile(caption),
}
2023-08-14 14:44:07 +03:00
db_query_count = {
"user": current_user.user,
"album": album,
"filename": re.compile(q),
"caption": re.compile(caption),
}
2022-12-21 00:59:35 +02:00
2023-08-14 14:44:07 +03:00
async for video in col_videos.find(db_query, limit=page_size, skip=skip).sort(
2023-08-14 14:55:49 +03:00
"dates.uploaded", direction=DESCENDING
2023-08-14 14:44:07 +03:00
):
2023-03-12 15:59:13 +02:00
output["results"].append(
{
"id": video["_id"].__str__(),
"filename": video["filename"],
"caption": video["caption"],
}
)
2023-08-14 14:44:07 +03:00
if (await col_videos.count_documents(db_query_count)) > page * page_size:
2022-12-21 00:59:35 +02:00
token = str(token_urlsafe(32))
2023-08-14 14:44:07 +03:00
await col_tokens.insert_one(
2023-03-12 15:59:13 +02:00
{
"token": token,
"query": q,
2023-03-23 13:34:18 +02:00
"caption": caption,
2023-03-12 15:59:13 +02:00
"page": page + 1,
"page_size": page_size,
}
)
2023-03-23 13:34:18 +02:00
output["next_page"] = f"/albums/{album}/videos/?token={token}" # type: ignore
2022-12-21 00:59:35 +02:00
else:
2023-03-12 15:59:13 +02:00
output["next_page"] = None # type: ignore
2022-12-21 00:59:35 +02:00
return UJSONResponse(output)