1+ import os
2+ from uuid import uuid4
3+
14import aiofiles .os
25import asyncpg # type: ignore[import-untyped]
36from fastapi import APIRouter , Depends , HTTPException , UploadFile
912from bracket .logic .subscriptions import check_requirement
1013from bracket .logic .tournaments import get_tournament_logo_path
1114from bracket .models .db .tournament import (
12- Tournament ,
1315 TournamentBody ,
1416 TournamentToInsert ,
1517 TournamentUpdateBody ,
2527from bracket .schema import tournaments
2628from bracket .sql .tournaments import (
2729 sql_delete_tournament ,
30+ sql_get_tournament ,
2831 sql_get_tournament_by_endpoint_name ,
2932 sql_get_tournaments ,
3033)
3134from bracket .sql .users import get_user_access_to_club , get_which_clubs_has_user_access_to
32- from bracket .utils .db import fetch_one_parsed_certain
3335from bracket .utils .errors import (
3436 ForeignKey ,
3537 UniqueIndex ,
5153async def get_tournament (
5254 tournament_id : int , user : UserPublic | None = Depends (user_authenticated_or_public_dashboard )
5355) -> TournamentResponse :
54- tournament = await fetch_one_parsed_certain (
55- database , Tournament , tournaments .select ().where (tournaments .c .id == tournament_id )
56- )
56+ tournament = await sql_get_tournament (tournament_id )
5757 if user is None and not tournament .dashboard_public :
5858 raise unauthorized_exception
5959
@@ -154,13 +154,22 @@ async def upload_logo(
154154 tournament_id : int ,
155155 file : UploadFile | None = None ,
156156 _ : UserPublic = Depends (user_authenticated_for_tournament ),
157- ) -> SuccessResponse :
157+ ) -> TournamentResponse :
158158 old_logo_path = await get_tournament_logo_path (tournament_id )
159- new_logo_path = f"static/{ file .filename } " if file is not None else None
159+ filename : str | None = None
160+ new_logo_path : str | None = None
160161
161- if file and new_logo_path :
162- async with aiofiles .open (new_logo_path , "wb" ) as f :
163- await f .write (await file .read ())
162+ if file :
163+ assert file .filename is not None
164+ extension = os .path .splitext (file .filename )[1 ]
165+ assert extension in (".png" , ".jpg" , ".jpeg" )
166+
167+ filename = f"{ uuid4 ()} { extension } "
168+ new_logo_path = f"static/{ filename } " if file is not None else None
169+
170+ if new_logo_path :
171+ async with aiofiles .open (new_logo_path , "wb" ) as f :
172+ await f .write (await file .read ())
164173
165174 if old_logo_path is not None and old_logo_path != new_logo_path :
166175 try :
@@ -170,6 +179,6 @@ async def upload_logo(
170179
171180 await database .execute (
172181 tournaments .update ().where (tournaments .c .id == tournament_id ),
173- values = {"logo_path" : file . filename if file else None },
182+ values = {"logo_path" : filename },
174183 )
175- return SuccessResponse ( )
184+ return TournamentResponse ( data = await sql_get_tournament ( tournament_id ) )
0 commit comments