From bd38eddd11e75a7d7ba98cdea5aa2d3f1d3f5fe6 Mon Sep 17 00:00:00 2001 From: zurdi Date: Mon, 20 Apr 2026 08:33:35 +0000 Subject: [PATCH] feat: add multi-file ROM fixture for testing and enhance soundtrack player functionality - Introduced a new fixture `multi_file_rom` in `conftest.py` to create a ROM with multiple files for testing purposes. - Updated `test_manual.py` and `test_soundtrack.py` to utilize the new fixture for testing manual uploads and soundtrack functionalities. - Enhanced the audio tag extraction logic in `audio_tags.py` to handle oversized audio files and added a function to check allowed audio file extensions. - Modified `nginx.py` to support inline file serving for audio files, allowing for better streaming capabilities. - Improved error handling in the Vue components for soundtrack management, including user feedback for playback errors and metadata loading issues. - Refactored the soundtrack player store to use local storage for volume and mute settings, simplifying state management. - Added new localization strings for soundtrack player actions and error messages in both English (US and GB) locale files. --- .../versions/0080_rom_category_soundtrack.py | 49 ++-- backend/endpoints/responses/rom.py | 9 + backend/endpoints/roms/files.py | 12 +- backend/endpoints/roms/manual.py | 53 +++-- backend/endpoints/roms/soundtrack.py | 57 ++--- backend/handler/database/roms_handler.py | 28 ++- backend/handler/filesystem/roms_handler.py | 5 +- backend/tests/endpoints/conftest.py | 12 + backend/tests/endpoints/roms/conftest.py | 43 ++++ backend/tests/endpoints/roms/test_manual.py | 151 +++++++++---- .../tests/endpoints/roms/test_soundtrack.py | 209 ++++++++++++++---- backend/utils/audio_tags.py | 142 +++++++++--- backend/utils/nginx.py | 12 +- frontend/src/components/Details/MediaTab.vue | 26 ++- .../components/Details/SoundtrackPlayer.vue | 38 +++- .../common/SoundtrackMiniPlayer.vue | 79 ++++++- frontend/src/locales/en_GB/rom.json | 10 + frontend/src/locales/en_US/rom.json | 10 + frontend/src/stores/soundtrackPlayer.ts | 73 +++--- 19 files changed, 764 insertions(+), 254 deletions(-) create mode 100644 backend/tests/endpoints/roms/conftest.py diff --git a/backend/alembic/versions/0080_rom_category_soundtrack.py b/backend/alembic/versions/0080_rom_category_soundtrack.py index eba8e5786..769a6f34a 100644 --- a/backend/alembic/versions/0080_rom_category_soundtrack.py +++ b/backend/alembic/versions/0080_rom_category_soundtrack.py @@ -9,7 +9,6 @@ Create Date: 2026-04-17 00:00:00.000000 import sqlalchemy as sa from alembic import op -from sqlalchemy.dialects.postgresql import ENUM from utils.database import CustomJSON, is_postgresql @@ -24,23 +23,21 @@ def upgrade() -> None: connection = op.get_bind() if is_postgresql(connection): - rom_file_category_enum = ENUM( - "GAME", - "DLC", - "HACK", - "MANUAL", - "PATCH", - "UPDATE", - "MOD", - "DEMO", - "TRANSLATION", - "PROTOTYPE", - "CHEAT", - "SOUNDTRACK", - name="romfilecategory", - create_type=False, + # `ALTER TYPE ... ADD VALUE` must run outside a transaction in PostgreSQL. + # autocommit_block breaks out of alembic's wrapping transaction for + # exactly this operation. + with op.get_context().autocommit_block(): + op.execute( + "ALTER TYPE romfilecategory ADD VALUE IF NOT EXISTS 'SOUNDTRACK'" + ) + + # audio_meta column (added separately so the enum change sticks even + # if the JSON column already exists from a partial prior run). + op.add_column( + "rom_files", + sa.Column("audio_meta", CustomJSON(), nullable=True), + if_not_exists=True, ) - rom_file_category_enum.create(connection, checkfirst=True) else: rom_file_category_enum = sa.Enum( "GAME", @@ -57,15 +54,19 @@ def upgrade() -> None: "SOUNDTRACK", name="romfilecategory", ) - - with op.batch_alter_table("rom_files", schema=None) as batch_op: - batch_op.alter_column("category", type_=rom_file_category_enum, nullable=True) - batch_op.add_column( - sa.Column("audio_meta", CustomJSON(), nullable=True), - if_not_exists=True, - ) + with op.batch_alter_table("rom_files", schema=None) as batch_op: + batch_op.alter_column( + "category", type_=rom_file_category_enum, nullable=True + ) + batch_op.add_column( + sa.Column("audio_meta", CustomJSON(), nullable=True), + if_not_exists=True, + ) def downgrade() -> None: + # PostgreSQL cannot remove enum values, so on downgrade we only drop the + # audio_meta column and leave SOUNDTRACK in the enum. Any rows that still + # hold the new value keep working; a re-upgrade is a no-op. with op.batch_alter_table("rom_files", schema=None) as batch_op: batch_op.drop_column("audio_meta", if_exists=True) diff --git a/backend/endpoints/responses/rom.py b/backend/endpoints/responses/rom.py index ae649544b..669654a7a 100644 --- a/backend/endpoints/responses/rom.py +++ b/backend/endpoints/responses/rom.py @@ -182,6 +182,15 @@ class RomFileSchema(BaseModel): audio_meta: RomFileAudioMetaSchema | None = None +class SoundtrackTrackMetaSchema(BaseModel): + model_config = ConfigDict(from_attributes=True) + + file_id: int + file_name: str + file_size_bytes: int + audio_meta: RomFileAudioMetaSchema | None = None + + class RomMetadataSchema(BaseModel): model_config = ConfigDict(from_attributes=True) diff --git a/backend/endpoints/roms/files.py b/backend/endpoints/roms/files.py index 2dca2e854..41124fdad 100644 --- a/backend/endpoints/roms/files.py +++ b/backend/endpoints/roms/files.py @@ -92,21 +92,23 @@ async def get_romfile_content( media_type = ( _guess_media_type(file_name) if is_audio else "application/octet-stream" ) + disposition = "inline" if is_audio else "attachment" # Serve the file directly in development mode for emulatorjs if DEV_MODE: rom_path = fs_rom_handler.validate_path(file.full_path) - # Audio: serve inline so the browser streams + seeks via Range. Other - # categories: keep the attachment disposition so navigation downloads. - # In both cases Starlette sets Content-Length and honors Range natively. + # Starlette sets Content-Length and honors Range natively — inline + # disposition lets