Files
romm/backend/exceptions/endpoint_exceptions.py
nendo 70e583b9a0 Add music favorites and playlists API
Spotify-style library features for soundtrack tracks: per-user favorites
(liked songs) and user-owned playlists with manual ordering and optional
public read-only sharing.

Track references are stored as (rom_id, md5_hash) rather than rom_file_id:
scans purge and re-insert rom_files rows, so file ids churn while rom ids
and content hashes are stable. Entries survive rescans and renames, and
resolve to the current file at query time; entries whose content is gone
are omitted from listings until the file returns.

To make the content hash reliably present, soundtrack audio files are now
always hashed individually during scans (including NON_HASHABLE_PLATFORMS
and SKIP_HASH_CALCULATION, whose file-size concerns don't apply to audio),
and soundtrack uploads compute crc/md5/sha1 on write. Existing libraries
backfill via a HASHES scan; adding an unhashed track returns 400.

New playlists.read/playlists.write scopes are wired through the granular
permission system (PermEntity.PLAYLISTS); the migration mirrors every
group's collections grants onto the playlists entity so existing groups
keep working without admin intervention.

Endpoints under /api/music: favorites GET/POST/DELETE, is_favorite and
md5_hash on track rows, playlists CRUD, playlist tracks GET/POST/DELETE
and PUT /tracks/order.
2026-07-23 15:32:32 +09:00

109 lines
3.4 KiB
Python

from fastapi import HTTPException, status
from logger.logger import log
class PlatformNotFoundInDatabaseException(Exception):
def __init__(self, id):
self.message = f"Platform with id '{id}' not found"
super().__init__(self.message)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=self.message)
def __repr__(self) -> str:
return self.message
class RomNotFoundInDatabaseException(Exception):
def __init__(self, id):
self.message = f"Rom with id '{id}' not found"
super().__init__(self.message)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=self.message)
def __repr__(self) -> str:
return self.message
class CollectionNotFoundInDatabaseException(Exception):
def __init__(self, id):
self.message = f"Collection with id '{id}' not found"
super().__init__(self.message)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=self.message)
def __repr__(self) -> str:
return self.message
class CollectionPermissionError(Exception):
def __init__(self, id):
self.message = f"Permission denied for collection with id '{id}'"
super().__init__(self.message)
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=self.message)
def __repr__(self) -> str:
return self.message
class CollectionAlreadyExistsException(Exception):
def __init__(self, name):
self.message = f"Collection with name '{name}' already exists"
super().__init__(self.message)
log.critical(self.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=self.message
)
def __repr__(self) -> str:
return self.message
class MusicPlaylistNotFoundException(Exception):
def __init__(self, id):
self.message = f"Playlist with id '{id}' not found"
super().__init__(self.message)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=self.message)
def __repr__(self) -> str:
return self.message
class MusicPlaylistPermissionError(Exception):
def __init__(self, id):
self.message = f"Permission denied for playlist with id '{id}'"
super().__init__(self.message)
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=self.message)
def __repr__(self) -> str:
return self.message
class MusicPlaylistAlreadyExistsException(Exception):
def __init__(self, name):
self.message = f"Playlist with name '{name}' already exists"
super().__init__(self.message)
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail=self.message
)
def __repr__(self) -> str:
return self.message
class RomNotFoundInRetroAchievementsException(Exception):
def __init__(self, id):
self.message = f"Rom with id '{id}' does not exist on RetroAchievements"
super().__init__(self.message)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=self.message)
class SGDBInvalidAPIKeyException(Exception):
def __init__(self):
self.message = "Invalid SGDB API key"
super().__init__(self.message)
log.critical(self.message)
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail=self.message
)
def __repr__(self) -> str:
return self.message