feat(storage): save MIME type into storage service
This commit is contained in:
@@ -681,7 +681,4 @@ async def save_replay(
|
||||
):
|
||||
replay_data = req.mreplay
|
||||
replay_path = f"replays/{req.score_id}_{req.beatmap_id}_{req.user_id}_lazer_replay.osr"
|
||||
await storage_service.write_file(
|
||||
replay_path,
|
||||
base64.b64decode(replay_data),
|
||||
)
|
||||
await storage_service.write_file(replay_path, base64.b64decode(replay_data), "application/x-osu-replay")
|
||||
|
||||
@@ -35,7 +35,7 @@ async def upload_avatar(
|
||||
"""
|
||||
|
||||
# check file
|
||||
check_image(content, 5 * 1024 * 1024, 256, 256)
|
||||
format_ = check_image(content, 5 * 1024 * 1024, 256, 256)
|
||||
|
||||
if url := current_user.avatar_url:
|
||||
path = storage.get_file_name_by_url(url)
|
||||
@@ -45,7 +45,7 @@ async def upload_avatar(
|
||||
filehash = hashlib.sha256(content).hexdigest()
|
||||
storage_path = f"avatars/{current_user.id}_{filehash}.png"
|
||||
if not await storage.is_exists(storage_path):
|
||||
await storage.write_file(storage_path, content)
|
||||
await storage.write_file(storage_path, content, f"image/{format_}")
|
||||
url = await storage.get_file_url(storage_path)
|
||||
current_user.avatar_url = url
|
||||
await session.commit()
|
||||
|
||||
@@ -35,7 +35,7 @@ async def upload_cover(
|
||||
"""
|
||||
|
||||
# check file
|
||||
check_image(content, 10 * 1024 * 1024, 3000, 2000)
|
||||
format_ = check_image(content, 10 * 1024 * 1024, 3000, 2000)
|
||||
|
||||
if url := current_user.cover["url"]:
|
||||
path = storage.get_file_name_by_url(url)
|
||||
@@ -45,7 +45,7 @@ async def upload_cover(
|
||||
filehash = hashlib.sha256(content).hexdigest()
|
||||
storage_path = f"cover/{current_user.id}_{filehash}.png"
|
||||
if not await storage.is_exists(storage_path):
|
||||
await storage.write_file(storage_path, content)
|
||||
await storage.write_file(storage_path, content, f"image/{format_}")
|
||||
url = await storage.get_file_url(storage_path)
|
||||
current_user.cover = UserProfileCover(url=url)
|
||||
await session.commit()
|
||||
|
||||
@@ -52,8 +52,8 @@ async def create_team(
|
||||
if is_existed:
|
||||
raise HTTPException(status_code=409, detail="Short name already exists")
|
||||
|
||||
check_image(flag, 2 * 1024 * 1024, 240, 120)
|
||||
check_image(cover, 10 * 1024 * 1024, 3000, 2000)
|
||||
flag_format = check_image(flag, 2 * 1024 * 1024, 240, 120)
|
||||
cover_format = check_image(cover, 10 * 1024 * 1024, 3000, 2000)
|
||||
|
||||
now = utcnow()
|
||||
team = Team(name=name, short_name=short_name, leader_id=user_id, created_at=now)
|
||||
@@ -64,13 +64,13 @@ async def create_team(
|
||||
filehash = hashlib.sha256(flag).hexdigest()
|
||||
storage_path = f"team_flag/{team.id}_{filehash}.png"
|
||||
if not await storage.is_exists(storage_path):
|
||||
await storage.write_file(storage_path, flag)
|
||||
await storage.write_file(storage_path, flag, f"image/{flag_format}")
|
||||
team.flag_url = await storage.get_file_url(storage_path)
|
||||
|
||||
filehash = hashlib.sha256(cover).hexdigest()
|
||||
storage_path = f"team_cover/{team.id}_{filehash}.png"
|
||||
if not await storage.is_exists(storage_path):
|
||||
await storage.write_file(storage_path, cover)
|
||||
await storage.write_file(storage_path, cover, f"image/{cover_format}")
|
||||
team.cover_url = await storage.get_file_url(storage_path)
|
||||
|
||||
team_member = TeamMember(user_id=user_id, team_id=team.id, joined_at=now)
|
||||
@@ -120,7 +120,7 @@ async def update_team(
|
||||
team.short_name = short_name
|
||||
|
||||
if flag:
|
||||
check_image(flag, 2 * 1024 * 1024, 240, 120)
|
||||
format_ = check_image(flag, 2 * 1024 * 1024, 240, 120)
|
||||
|
||||
if old_flag := team.flag_url:
|
||||
path = storage.get_file_name_by_url(old_flag)
|
||||
@@ -129,10 +129,10 @@ async def update_team(
|
||||
filehash = hashlib.sha256(flag).hexdigest()
|
||||
storage_path = f"team_flag/{team.id}_{filehash}.png"
|
||||
if not await storage.is_exists(storage_path):
|
||||
await storage.write_file(storage_path, flag)
|
||||
await storage.write_file(storage_path, flag, f"image/{format_}")
|
||||
team.flag_url = await storage.get_file_url(storage_path)
|
||||
if cover:
|
||||
check_image(cover, 10 * 1024 * 1024, 3000, 2000)
|
||||
format_ = check_image(cover, 10 * 1024 * 1024, 3000, 2000)
|
||||
|
||||
if old_cover := team.cover_url:
|
||||
path = storage.get_file_name_by_url(old_cover)
|
||||
@@ -141,7 +141,7 @@ async def update_team(
|
||||
filehash = hashlib.sha256(cover).hexdigest()
|
||||
storage_path = f"team_cover/{team.id}_{filehash}.png"
|
||||
if not await storage.is_exists(storage_path):
|
||||
await storage.write_file(storage_path, cover)
|
||||
await storage.write_file(storage_path, cover, f"image/{format_}")
|
||||
team.cover_url = await storage.get_file_url(storage_path)
|
||||
|
||||
if leader_id is not None:
|
||||
|
||||
@@ -135,10 +135,7 @@ async def save_replay(
|
||||
|
||||
storage_service = get_storage_service()
|
||||
replay_path = f"replays/{score.id}_{score.beatmap_id}_{score.user_id}_lazer_replay.osr"
|
||||
await storage_service.write_file(
|
||||
replay_path,
|
||||
bytes(data),
|
||||
)
|
||||
await storage_service.write_file(replay_path, bytes(data), "application/x-osu-replay")
|
||||
|
||||
|
||||
class SpectatorHub(Hub[StoreClientState]):
|
||||
|
||||
@@ -135,7 +135,7 @@ def truncate(text: str, limit: int = 100, ellipsis: str = "...") -> str:
|
||||
return text
|
||||
|
||||
|
||||
def check_image(content: bytes, size: int, width: int, height: int) -> None:
|
||||
def check_image(content: bytes, size: int, width: int, height: int) -> str:
|
||||
if len(content) > size: # 10MB limit
|
||||
raise HTTPException(status_code=400, detail="File size exceeds 10MB limit")
|
||||
elif len(content) == 0:
|
||||
@@ -149,6 +149,7 @@ def check_image(content: bytes, size: int, width: int, height: int) -> None:
|
||||
status_code=400,
|
||||
detail=f"Image size exceeds {width}x{height} pixels",
|
||||
)
|
||||
return img.format.lower()
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=f"Error processing image: {e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user