feat(storage): save MIME type into storage service

This commit is contained in:
MingxuanGame
2025-08-30 12:36:43 +00:00
parent c167cbcea6
commit 7d79f3cee7
6 changed files with 16 additions and 21 deletions

View File

@@ -681,7 +681,4 @@ async def save_replay(
): ):
replay_data = req.mreplay replay_data = req.mreplay
replay_path = f"replays/{req.score_id}_{req.beatmap_id}_{req.user_id}_lazer_replay.osr" replay_path = f"replays/{req.score_id}_{req.beatmap_id}_{req.user_id}_lazer_replay.osr"
await storage_service.write_file( await storage_service.write_file(replay_path, base64.b64decode(replay_data), "application/x-osu-replay")
replay_path,
base64.b64decode(replay_data),
)

View File

@@ -35,7 +35,7 @@ async def upload_avatar(
""" """
# check file # 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: if url := current_user.avatar_url:
path = storage.get_file_name_by_url(url) path = storage.get_file_name_by_url(url)
@@ -45,7 +45,7 @@ async def upload_avatar(
filehash = hashlib.sha256(content).hexdigest() filehash = hashlib.sha256(content).hexdigest()
storage_path = f"avatars/{current_user.id}_{filehash}.png" storage_path = f"avatars/{current_user.id}_{filehash}.png"
if not await storage.is_exists(storage_path): 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) url = await storage.get_file_url(storage_path)
current_user.avatar_url = url current_user.avatar_url = url
await session.commit() await session.commit()

View File

@@ -35,7 +35,7 @@ async def upload_cover(
""" """
# check file # 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"]: if url := current_user.cover["url"]:
path = storage.get_file_name_by_url(url) path = storage.get_file_name_by_url(url)
@@ -45,7 +45,7 @@ async def upload_cover(
filehash = hashlib.sha256(content).hexdigest() filehash = hashlib.sha256(content).hexdigest()
storage_path = f"cover/{current_user.id}_{filehash}.png" storage_path = f"cover/{current_user.id}_{filehash}.png"
if not await storage.is_exists(storage_path): 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) url = await storage.get_file_url(storage_path)
current_user.cover = UserProfileCover(url=url) current_user.cover = UserProfileCover(url=url)
await session.commit() await session.commit()

View File

@@ -52,8 +52,8 @@ async def create_team(
if is_existed: if is_existed:
raise HTTPException(status_code=409, detail="Short name already exists") raise HTTPException(status_code=409, detail="Short name already exists")
check_image(flag, 2 * 1024 * 1024, 240, 120) flag_format = check_image(flag, 2 * 1024 * 1024, 240, 120)
check_image(cover, 10 * 1024 * 1024, 3000, 2000) cover_format = check_image(cover, 10 * 1024 * 1024, 3000, 2000)
now = utcnow() now = utcnow()
team = Team(name=name, short_name=short_name, leader_id=user_id, created_at=now) 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() filehash = hashlib.sha256(flag).hexdigest()
storage_path = f"team_flag/{team.id}_{filehash}.png" storage_path = f"team_flag/{team.id}_{filehash}.png"
if not await storage.is_exists(storage_path): 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) team.flag_url = await storage.get_file_url(storage_path)
filehash = hashlib.sha256(cover).hexdigest() filehash = hashlib.sha256(cover).hexdigest()
storage_path = f"team_cover/{team.id}_{filehash}.png" storage_path = f"team_cover/{team.id}_{filehash}.png"
if not await storage.is_exists(storage_path): 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.cover_url = await storage.get_file_url(storage_path)
team_member = TeamMember(user_id=user_id, team_id=team.id, joined_at=now) 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 team.short_name = short_name
if flag: 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: if old_flag := team.flag_url:
path = storage.get_file_name_by_url(old_flag) path = storage.get_file_name_by_url(old_flag)
@@ -129,10 +129,10 @@ async def update_team(
filehash = hashlib.sha256(flag).hexdigest() filehash = hashlib.sha256(flag).hexdigest()
storage_path = f"team_flag/{team.id}_{filehash}.png" storage_path = f"team_flag/{team.id}_{filehash}.png"
if not await storage.is_exists(storage_path): 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) team.flag_url = await storage.get_file_url(storage_path)
if cover: 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: if old_cover := team.cover_url:
path = storage.get_file_name_by_url(old_cover) path = storage.get_file_name_by_url(old_cover)
@@ -141,7 +141,7 @@ async def update_team(
filehash = hashlib.sha256(cover).hexdigest() filehash = hashlib.sha256(cover).hexdigest()
storage_path = f"team_cover/{team.id}_{filehash}.png" storage_path = f"team_cover/{team.id}_{filehash}.png"
if not await storage.is_exists(storage_path): 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) team.cover_url = await storage.get_file_url(storage_path)
if leader_id is not None: if leader_id is not None:

View File

@@ -135,10 +135,7 @@ async def save_replay(
storage_service = get_storage_service() storage_service = get_storage_service()
replay_path = f"replays/{score.id}_{score.beatmap_id}_{score.user_id}_lazer_replay.osr" replay_path = f"replays/{score.id}_{score.beatmap_id}_{score.user_id}_lazer_replay.osr"
await storage_service.write_file( await storage_service.write_file(replay_path, bytes(data), "application/x-osu-replay")
replay_path,
bytes(data),
)
class SpectatorHub(Hub[StoreClientState]): class SpectatorHub(Hub[StoreClientState]):

View File

@@ -135,7 +135,7 @@ def truncate(text: str, limit: int = 100, ellipsis: str = "...") -> str:
return text 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 if len(content) > size: # 10MB limit
raise HTTPException(status_code=400, detail="File size exceeds 10MB limit") raise HTTPException(status_code=400, detail="File size exceeds 10MB limit")
elif len(content) == 0: elif len(content) == 0:
@@ -149,6 +149,7 @@ def check_image(content: bytes, size: int, width: int, height: int) -> None:
status_code=400, status_code=400,
detail=f"Image size exceeds {width}x{height} pixels", detail=f"Image size exceeds {width}x{height} pixels",
) )
return img.format.lower()
except Exception as e: except Exception as e:
raise HTTPException(status_code=400, detail=f"Error processing image: {e}") raise HTTPException(status_code=400, detail=f"Error processing image: {e}")