Added photos/videos move methods

This commit is contained in:
Profitroll 2023-01-05 16:38:00 +01:00
parent ea1b92015d
commit 6df2d274b1
2 changed files with 70 additions and 0 deletions

View File

@ -1,6 +1,7 @@
import re import re
import pickle import pickle
from secrets import token_urlsafe from secrets import token_urlsafe
from shutil import move
from typing import List, Union from typing import List, Union
from magic import Magic from magic import Magic
from datetime import datetime, timedelta 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) 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") @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"])): async def photo_delete(id: str, current_user: User = Security(get_current_active_user, scopes=["photos.write"])):

View File

@ -1,6 +1,7 @@
import re import re
import pickle import pickle
from secrets import token_urlsafe from secrets import token_urlsafe
from shutil import move
from magic import Magic from magic import Magic
from datetime import datetime from datetime import datetime
from os import makedirs, path, remove 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) 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") @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"])): async def video_delete(id: str, current_user: User = Security(get_current_active_user, scopes=["videos.write"])):