diff --git a/backend/handler/filesystem/base_handler.py b/backend/handler/filesystem/base_handler.py index cea4a20e1..b93042cf4 100644 --- a/backend/handler/filesystem/base_handler.py +++ b/backend/handler/filesystem/base_handler.py @@ -210,22 +210,24 @@ class FSHandler: return match.group(0) if match else "" def exclude_single_files(self, files: list[str]) -> list[str]: - excluded_extensions = cm.get_config().EXCLUDED_SINGLE_EXT - excluded_names = cm.get_config().EXCLUDED_SINGLE_FILES + cnfg = cm.get_config() + excluded_extensions = cnfg.EXCLUDED_SINGLE_EXT + excluded_names = cnfg.EXCLUDED_SINGLE_FILES excluded_files: list[str] = [] for file_name in files: - # Split the file name to get the extension. - ext = self.parse_file_extension(file_name) + file_name_lower = file_name.lower() - # Exclude the file if it has no extension or the extension is in the excluded list. - if ext and ext.lower() in excluded_extensions: + # Check whether the filename ends with any excluded extension entry. + if any(file_name_lower.endswith("." + ext) for ext in excluded_extensions): excluded_files.append(file_name) + continue - # Additionally, check if the file name mathes a pattern in the excluded list. - for name in excluded_names: - if file_name == name or fnmatch.fnmatch(file_name, name): - excluded_files.append(file_name) + # Check if the file name matches a pattern in the excluded list. + if file_name in excluded_names or any( + fnmatch.fnmatch(file_name, name) for name in excluded_names + ): + excluded_files.append(file_name) # Return files that are not in the filtered list. return [f for f in files if f not in excluded_files] diff --git a/backend/handler/filesystem/roms_handler.py b/backend/handler/filesystem/roms_handler.py index 8145a1f1e..411f24f6c 100644 --- a/backend/handler/filesystem/roms_handler.py +++ b/backend/handler/filesystem/roms_handler.py @@ -433,8 +433,9 @@ class FSRomsHandler(FSHandler): rom.platform_slug not in NON_HASHABLE_PLATFORMS and calculate_hashes ) - excluded_file_names = cm.get_config().EXCLUDED_MULTI_PARTS_FILES - excluded_file_exts = cm.get_config().EXCLUDED_MULTI_PARTS_EXT + cnfg = cm.get_config() + excluded_file_names = cnfg.EXCLUDED_MULTI_PARTS_FILES + excluded_file_exts = cnfg.EXCLUDED_MULTI_PARTS_EXT rom_crc_c = 0 rom_md5_h = hashlib.md5(usedforsecurity=False) if calculate_hashes else None @@ -455,11 +456,14 @@ class FSRomsHandler(FSHandler): for f_path, file_name in iter_files( f"{abs_fs_path}/{rom.fs_name}", recursive=True ): - # Check if file is excluded - ext = self.parse_file_extension(file_name) - if ext in excluded_file_exts: + # Check if file is excluded by extension. + file_name_lower = file_name.lower() + if any( + file_name_lower.endswith("." + ext) for ext in excluded_file_exts + ): continue + # Check if the file name matches a pattern in the excluded list. if any( file_name == exc_name or fnmatch.fnmatch(file_name, exc_name) for exc_name in excluded_file_names diff --git a/backend/tests/handler/filesystem/test_base_handler.py b/backend/tests/handler/filesystem/test_base_handler.py index 0df61ccfb..3fe7efbd7 100644 --- a/backend/tests/handler/filesystem/test_base_handler.py +++ b/backend/tests/handler/filesystem/test_base_handler.py @@ -161,6 +161,43 @@ class TestFSHandler: assert "game.rom" in result assert "data.json" in result + def test_exclude_single_files_multi_dot(self, handler: FSHandler): + """Test that files with multiple dots are excluded by last or compound extension""" + files = [ + "game.nds", + "game.nds.hash.txt", + "game.nds.enc.hash.txt", + "readme.txt", + "game.rom", + ] + + with patch("handler.filesystem.base_handler.cm.get_config") as mock_config: + # Exclude by last single extension + mock_config.return_value.EXCLUDED_SINGLE_EXT = ["txt"] + mock_config.return_value.EXCLUDED_SINGLE_FILES = [] + + result = handler.exclude_single_files(files) + + assert "game.nds.hash.txt" not in result + assert "game.nds.enc.hash.txt" not in result + assert "readme.txt" not in result + assert "game.nds" in result + assert "game.rom" in result + + with patch("handler.filesystem.base_handler.cm.get_config") as mock_config: + # Exclude by compound sub-extension "hash.txt" + mock_config.return_value.EXCLUDED_SINGLE_EXT = ["hash.txt"] + mock_config.return_value.EXCLUDED_SINGLE_FILES = [] + + result = handler.exclude_single_files(files) + + assert "game.nds.hash.txt" not in result + assert "game.nds.enc.hash.txt" not in result + # "readme.txt" does NOT end in "hash.txt" — should remain + assert "readme.txt" in result + assert "game.nds" in result + assert "game.rom" in result + async def test_make_directory(self, handler: FSHandler): """Test directory creation""" await handler.make_directory("test_dir") diff --git a/backend/tests/handler/filesystem/test_roms_handler.py b/backend/tests/handler/filesystem/test_roms_handler.py index e6c5bdc74..ed7787931 100644 --- a/backend/tests/handler/filesystem/test_roms_handler.py +++ b/backend/tests/handler/filesystem/test_roms_handler.py @@ -378,6 +378,64 @@ class TestFSRomsHandler: assert rom_file.file_size_bytes > 0 assert rom_file.last_modified is not None + @pytest.mark.asyncio + async def test_get_rom_files_multi_rom_multi_dot_exclusion( + self, handler: FSRomsHandler, rom_multi + ): + """Multi-dot filenames in a multi-part dir are excluded by simple or compound ext rules.""" + multi_dot_file = ( + handler.base_path / "n64/roms/Super Mario 64 (J) (Rev A)/game.n64.hash.txt" + ) + multi_dot_file.write_text("hash data") + + try: + # Exclude by the last single extension "txt" + config_txt = Config( + EXCLUDED_PLATFORMS=[], + EXCLUDED_SINGLE_EXT=[], + EXCLUDED_SINGLE_FILES=[], + EXCLUDED_MULTI_FILES=[], + EXCLUDED_MULTI_PARTS_EXT=["txt"], + EXCLUDED_MULTI_PARTS_FILES=[], + PLATFORMS_BINDING={}, + PLATFORMS_VERSIONS={}, + ROMS_FOLDER_NAME="roms", + FIRMWARE_FOLDER_NAME="bios", + ) + with pytest.MonkeyPatch.context() as m: + m.setattr( + "handler.filesystem.roms_handler.cm.get_config", lambda: config_txt + ) + parsed = await handler.get_rom_files(rom_multi) + file_names = [rf.file_name for rf in parsed.rom_files] + assert "game.n64.hash.txt" not in file_names + assert "Super Mario 64 (J) (Rev A) [Part 1].z64" in file_names + + # Exclude by the compound extension "hash.txt" + config_compound = Config( + EXCLUDED_PLATFORMS=[], + EXCLUDED_SINGLE_EXT=[], + EXCLUDED_SINGLE_FILES=[], + EXCLUDED_MULTI_FILES=[], + EXCLUDED_MULTI_PARTS_EXT=["hash.txt"], + EXCLUDED_MULTI_PARTS_FILES=[], + PLATFORMS_BINDING={}, + PLATFORMS_VERSIONS={}, + ROMS_FOLDER_NAME="roms", + FIRMWARE_FOLDER_NAME="bios", + ) + with pytest.MonkeyPatch.context() as m: + m.setattr( + "handler.filesystem.roms_handler.cm.get_config", + lambda: config_compound, + ) + parsed = await handler.get_rom_files(rom_multi) + file_names = [rf.file_name for rf in parsed.rom_files] + assert "game.n64.hash.txt" not in file_names + assert "Super Mario 64 (J) (Rev A) [Part 1].z64" in file_names + finally: + multi_dot_file.unlink(missing_ok=True) + async def test_rename_fs_rom_same_name(self, handler: FSRomsHandler): """Test rename_fs_rom when old and new names are the same""" old_name = "test_rom.n64"