WIP: pathlib support

This commit is contained in:
2023-06-23 08:51:42 +00:00
parent a5cd6a215f
commit 88d8a38444
6 changed files with 50 additions and 91 deletions

View File

@@ -5,6 +5,8 @@ from secrets import token_urlsafe
from shutil import move
from typing import Union
from pathlib import Path
import aiofiles
from bson.errors import InvalidId
from bson.objectid import ObjectId
@@ -45,15 +47,11 @@ async def video_upload(
if col_albums.find_one({"user": current_user.user, "name": album}) is None:
raise AlbumNameNotFoundError(album)
makedirs(
path.join("data", "users", current_user.user, "albums", album), exist_ok=True
)
makedirs(Path(f"data/users/{current_user.user}/albums/{album}"), exist_ok=True)
filename = file.filename
if path.exists(
path.join("data", "users", current_user.user, "albums", album, file.filename)
):
if Path(f"data/users/{current_user.user}/albums/{album}/{file.filename}").exists():
base_name = file.filename.split(".")[:-1]
extension = file.filename.split(".")[-1]
filename = (
@@ -61,7 +59,7 @@ async def video_upload(
)
async with aiofiles.open(
path.join("data", "users", current_user.user, "albums", album, filename), "wb"
Path(f"data/users/{current_user.user}/albums/{album}/{filename}"), "wb"
) as f:
await f.write(await file.read())
@@ -125,8 +123,8 @@ async def video_get(
except InvalidId:
raise VideoNotFoundError(id)
video_path = path.join(
"data", "users", current_user.user, "albums", video["album"], video["filename"]
video_path = Path(
f"data/users/{current_user.user}/albums/{video['album']}/{video['filename']}"
)
mime = Magic(mime=True).from_file(video_path)
@@ -161,11 +159,9 @@ async def video_move(
if col_albums.find_one({"user": current_user.user, "name": album}) is None:
raise AlbumNameNotFoundError(album)
if path.exists(
path.join(
"data", "users", current_user.user, "albums", album, video["filename"]
)
):
if Path(
f"data"/users/{current_user.user}/albums/{album}/{video['filename']}"
).exists():
base_name = video["filename"].split(".")[:-1]
extension = video["filename"].split(".")[-1]
filename = (
@@ -186,15 +182,8 @@ async def video_move(
)
move(
path.join(
"data",
"users",
current_user.user,
"albums",
video["album"],
video["filename"],
),
path.join("data", "users", current_user.user, "albums", album, filename),
Path(f"data/users/{current_user.user}/albums/{video['album']}/{video['filename']}"),
Path(f"data/users/{current_user.user}/albums/{album}/{filename}"),
)
return UJSONResponse(
@@ -264,14 +253,7 @@ async def video_delete(
album = col_albums.find_one({"name": video["album"]})
remove(
path.join(
"data",
"users",
current_user.user,
"albums",
video["album"],
video["filename"],
)
Path(f"data/users/{current_user.user}/albums/{video['album']}/{video['filename']}")
)
return Response(status_code=HTTP_204_NO_CONTENT)