Skip to content

Commit

Permalink
changes from self reivew
Browse files Browse the repository at this point in the history
  • Loading branch information
gantoine committed Aug 17, 2024
1 parent 716114e commit faba97d
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions backend/endpoints/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,33 @@ async def add_rom(request: Request):
Raises:
HTTPException: No files were uploaded
"""
platform_id = int(request.headers.get("x-upload-platform", None))
filename = request.headers.get("x-upload-filename", None)
platform_id = request.headers.get("x-upload-platform")
filename = request.headers.get("x-upload-filename")
if not platform_id or not filename:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="No platform ID provided",
detail="No platform ID or filename provided",
)

platform_fs_slug = db_platform_handler.get_platform(platform_id).fs_slug
platform_fs_slug = db_platform_handler.get_platform(int(platform_id)).fs_slug
roms_path = fs_rom_handler.build_upload_file_path(platform_fs_slug)
log.info(f"Uploading roms to {platform_fs_slug}")
log.info(f"Uploading file to {platform_fs_slug}")

file_location = f"{roms_path}/{filename}"
file_location = Path(f"{roms_path}/{filename}")
parser = StreamingFormDataParser(headers=request.headers)
parser.register("x-upload-platform", NullTarget())
parser.register(filename, FileTarget(file_location))
parser.register(filename, FileTarget(str(file_location)))

if await Path(file_location).exists():
if await file_location.exists():
log.warning(f" - Skipping {filename} since the file already exists")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"File {filename} already exists",
)

async def cleanup_partial_file():
if await Path(file_location).exists():
await Path(file_location).unlink()
if await file_location.exists():
await file_location.unlink()

try:
async for chunk in request.stream():
Expand All @@ -84,7 +84,6 @@ async def cleanup_partial_file():
except Exception as exc:
log.error("Error uploading files", exc_info=exc)
await cleanup_partial_file()

raise HTTPException(

Check failure on line 87 in backend/endpoints/rom.py

View check run for this annotation

Trunk.io / Trunk Check

ruff(B904)

[new] Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling

Check failure on line 87 in backend/endpoints/rom.py

View workflow job for this annotation

GitHub Actions / Trunk Check

ruff(B904)

[new] Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="There was an error uploading the file(s)",
Expand Down

0 comments on commit faba97d

Please sign in to comment.