From 6df2d274b155fc4d2b58c8aaa01164140edbb987 Mon Sep 17 00:00:00 2001 From: profitroll Date: Thu, 5 Jan 2023 16:38:00 +0100 Subject: [PATCH] Added photos/videos move methods --- extensions/photos.py | 35 +++++++++++++++++++++++++++++++++++ extensions/videos.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/extensions/photos.py b/extensions/photos.py index 0d78c0f..e8f4132 100644 --- a/extensions/photos.py +++ b/extensions/photos.py @@ -1,6 +1,7 @@ import re import pickle from secrets import token_urlsafe +from shutil import move from typing import List, Union from magic import Magic from datetime import datetime, timedelta @@ -107,6 +108,40 @@ async def photo_get(id: str, current_user: User = Security(get_current_active_us return Response(image_file, media_type=mime) +@app.put("/photos/{id}", description="Move a photo into another album") +async def photo_move(id: str, album: str, current_user: User = Security(get_current_active_user, scopes=["photos.write"])): + + try: + image = col_photos.find_one( {"_id": ObjectId(id)} ) + if image is None: + raise InvalidId(id) + except InvalidId: + return HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an image with such id.") + + 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.") + + if path.exists(path.join("data", "users", current_user.user, "albums", album, image["filename"])): + base_name = image["filename"].split(".")[:-1] + extension = image["filename"].split(".")[-1] + filename = ".".join(base_name)+f"_{int(datetime.now().timestamp())}."+extension + else: + filename = image["filename"] + + col_photos.find_one_and_update( {"_id": ObjectId(id)}, {"$set": {"album": album, "filename": filename}} ) + + move( + path.join("data", "users", current_user.user, "albums", image["album"], image["filename"]), + path.join("data", "users", current_user.user, "albums", album, filename) + ) + + return UJSONResponse( + { + "id": image["_id"].__str__(), + "filename": filename + } + ) + @app.delete("/photos/{id}", description="Delete a photo by id") async def photo_delete(id: str, current_user: User = Security(get_current_active_user, scopes=["photos.write"])): diff --git a/extensions/videos.py b/extensions/videos.py index 76bbf71..f409627 100644 --- a/extensions/videos.py +++ b/extensions/videos.py @@ -1,6 +1,7 @@ import re import pickle from secrets import token_urlsafe +from shutil import move from magic import Magic from datetime import datetime from os import makedirs, path, remove @@ -77,6 +78,40 @@ async def video_get(id: str, current_user: User = Security(get_current_active_us return Response(video_file, media_type=mime) +@app.put("/videos/{id}", description="Move a video into another album") +async def video_move(id: str, album: str, current_user: User = Security(get_current_active_user, scopes=["videos.write"])): + + try: + video = col_videos.find_one( {"_id": ObjectId(id)} ) + if video is None: + raise InvalidId(id) + except InvalidId: + return HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Could not find an video with such id.") + + 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.") + + if path.exists(path.join("data", "users", current_user.user, "albums", album, video["filename"])): + base_name = video["filename"].split(".")[:-1] + extension = video["filename"].split(".")[-1] + filename = ".".join(base_name)+f"_{int(datetime.now().timestamp())}."+extension + else: + filename = video["filename"] + + col_videos.find_one_and_update( {"_id": ObjectId(id)}, {"$set": {"album": album, "filename": filename}} ) + + move( + path.join("data", "users", current_user.user, "albums", video["album"], video["filename"]), + path.join("data", "users", current_user.user, "albums", album, filename) + ) + + return UJSONResponse( + { + "id": video["_id"].__str__(), + "filename": filename + } + ) + @app.delete("/videos/{id}", description="Delete a video by id") async def video_delete(id: str, current_user: User = Security(get_current_active_user, scopes=["videos.write"])):