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

@@ -1,6 +1,7 @@
import re
from datetime import datetime, timedelta, timezone
from os import makedirs, path, remove, system
from pathlib import Path
from secrets import token_urlsafe
from shutil import move
from threading import Thread
@@ -109,15 +110,11 @@ async def photo_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 = (
@@ -125,12 +122,12 @@ async def photo_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())
file_hash = await get_phash(
path.join("data", "users", current_user.user, "albums", album, filename)
Path(f"data/users/{current_user.user}/albums/{album}/{filename}")
)
duplicates = await get_duplicates(file_hash, album)
@@ -168,7 +165,7 @@ async def photo_upload(
try:
coords = extract_location(
path.join("data", "users", current_user.user, "albums", album, filename)
Path(f"data/users/{current_user.user}/albums/{album}/{filename}")
)
except (UnpackError, ValueError):
coords = {"lng": 0.0, "lat": 0.0, "alt": 0.0}
@@ -193,9 +190,7 @@ async def photo_upload(
compress_image,
trigger="date",
run_date=datetime.now() + timedelta(seconds=1),
args=[
path.join("data", "users", current_user.user, "albums", album, filename)
],
args=[Path(f"data/users/{current_user.user}/albums/{album}/{filename}")],
)
return UJSONResponse(
@@ -254,8 +249,8 @@ if configGet("media_token_access") is True:
except InvalidId:
raise PhotoNotFoundError(id)
image_path = path.join(
"data", "users", user.user, "albums", image["album"], image["filename"]
image_path = Path(
f"data/users/{user.user}/albums/{image['album']}/{image['filename']}"
)
mime = Magic(mime=True).from_file(image_path)
@@ -299,8 +294,8 @@ async def photo_get(
except InvalidId:
raise PhotoNotFoundError(id)
image_path = path.join(
"data", "users", current_user.user, "albums", image["album"], image["filename"]
image_path = Path(
f"data/users/{current_user.user}/albums/{image['album']}/{image['filename']}"
)
mime = Magic(mime=True).from_file(image_path)
@@ -335,11 +330,9 @@ async def photo_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, image["filename"]
)
):
if Path(
f"data/users/{current_user.user}/albums/{album}/{image['filename']}"
).exists():
base_name = image["filename"].split(".")[:-1]
extension = image["filename"].split(".")[-1]
filename = (
@@ -360,15 +353,10 @@ async def photo_move(
)
move(
path.join(
"data",
"users",
current_user.user,
"albums",
image["album"],
image["filename"],
Path(
f"data/users/{current_user.user}/albums/{image['album']}/{image['filename']}"
),
path.join("data", "users", current_user.user, "albums", album, filename),
Path(f"data/users/{current_user.user}/albums/{album}/{filename}"),
)
return UJSONResponse(
@@ -441,13 +429,8 @@ async def photo_delete(
col_albums.update_one({"name": image["album"]}, {"$set": {"cover": None}})
remove(
path.join(
"data",
"users",
current_user.user,
"albums",
image["album"],
image["filename"],
Path(
f"data/users/{current_user.user}/albums/{image['album']}/{image['filename']}"
)
)