Files
romm/backend/endpoints/screenshots.py
Georges-Antoine Assi ee8b55e6ef last set of changes
2026-03-07 09:56:17 -05:00

91 lines
3.0 KiB
Python

from fastapi import File, HTTPException, Request, UploadFile, status
from decorators.auth import protected_route
from endpoints.responses.assets import ScreenshotSchema
from exceptions.endpoint_exceptions import RomNotFoundInDatabaseException
from handler.auth.constants import Scope
from handler.database import db_rom_handler, db_screenshot_handler
from handler.filesystem import fs_asset_handler
from handler.scan_handler import scan_screenshot
from logger.formatter import BLUE
from logger.formatter import highlight as hl
from logger.logger import log
from utils.filesystem import sanitize_filename
from utils.router import APIRouter
router = APIRouter(
prefix="/screenshots",
tags=["screenshots"],
)
SCREENSHOT_FILE_UPLOAD = File(..., description="Screenshot file to upload.")
@protected_route(router.post, "", [Scope.ASSETS_WRITE])
async def add_screenshot(
request: Request,
rom_id: int,
screenshotFile: UploadFile = SCREENSHOT_FILE_UPLOAD,
) -> ScreenshotSchema:
rom = db_rom_handler.get_rom(id=rom_id)
if not rom:
raise RomNotFoundInDatabaseException(rom_id)
current_user = request.user
log.info(f"Uploading screenshots to {hl(str(rom.name), color=BLUE)}")
screenshots_path = fs_asset_handler.build_screenshots_file_path(
user=request.user, platform_fs_slug=rom.platform_slug, rom_id=rom.id
)
if not screenshotFile.filename:
log.error("Screenshot file has no filename")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Screenshot file has no filename",
)
try:
sanitized_screenshot_filename = sanitize_filename(screenshotFile.filename)
except ValueError as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid screenshot filename: {str(exc)}",
) from exc
await fs_asset_handler.write_file(
file=screenshotFile,
path=screenshots_path,
filename=sanitized_screenshot_filename,
)
# Scan or update screenshot
scanned_screenshot = await scan_screenshot(
file_name=sanitized_screenshot_filename,
user=request.user,
platform_fs_slug=rom.platform_slug,
rom_id=rom.id,
)
db_screenshot = db_screenshot_handler.get_screenshot(
file_name=sanitized_screenshot_filename,
rom_id=rom.id,
user_id=current_user.id,
)
if db_screenshot:
db_screenshot = db_screenshot_handler.update_screenshot(
db_screenshot.id,
{"file_size_bytes": scanned_screenshot.file_size_bytes},
)
else:
scanned_screenshot.rom_id = rom.id
scanned_screenshot.user_id = current_user.id
db_screenshot = db_screenshot_handler.add_screenshot(
screenshot=scanned_screenshot
)
rom = db_rom_handler.get_rom(id=rom_id)
if not rom:
raise RomNotFoundInDatabaseException(rom_id)
return ScreenshotSchema.model_validate(db_screenshot)