diff --git a/backend/handler/filesystem/base_handler.py b/backend/handler/filesystem/base_handler.py index fc9e72220..23207ba83 100644 --- a/backend/handler/filesystem/base_handler.py +++ b/backend/handler/filesystem/base_handler.py @@ -10,8 +10,8 @@ from pathlib import Path from typing import BinaryIO, Optional from config.config_manager import config_manager as cm -from fastapi import UploadFile from models.base import FILE_NAME_MAX_LENGTH +from starlette.datastructures import UploadFile from utils.filesystem import iter_directories, iter_files TAG_REGEX = re.compile(r"\(([^)]+)\)|\[([^]]+)\]") @@ -273,7 +273,7 @@ class FSHandler: Securely write file to filesystem. Args: - file: File object to write (UploadFile) + file: File-like object to write path: Relative path within base directory filename: Optional filename override overwrite: Allow overwriting existing files @@ -282,10 +282,7 @@ class FSHandler: Dictionary with operation result and file info """ - original_filename = filename - if isinstance(file, UploadFile): - original_filename = original_filename or file.filename - + original_filename = filename or getattr(file, "filename", None) if not original_filename: raise ValueError("Filename cannot be empty") diff --git a/backend/handler/filesystem/resources_handler.py b/backend/handler/filesystem/resources_handler.py index f98f179e5..cc719eba8 100644 --- a/backend/handler/filesystem/resources_handler.py +++ b/backend/handler/filesystem/resources_handler.py @@ -16,11 +16,7 @@ class FSResourcesHandler(FSHandler): super().__init__(base_path=RESOURCES_BASE_PATH) def get_platform_resources_path(self, platform_id: int) -> str: - return os.path.join( - self.base_path, - "roms", - str(platform_id), - ) + return os.path.join("roms", str(platform_id)) def cover_exists(self, entity: Rom | Collection, size: CoverSize) -> bool: """Check if rom cover exists in filesystem diff --git a/backend/handler/filesystem/tests/test_resources_handler.py b/backend/handler/filesystem/tests/test_resources_handler.py index ae7c49fff..2da2fb967 100644 --- a/backend/handler/filesystem/tests/test_resources_handler.py +++ b/backend/handler/filesystem/tests/test_resources_handler.py @@ -34,7 +34,7 @@ class TestFSResourcesHandler: """Test get_platform_resources_path method""" platform_id = 1 result = handler.get_platform_resources_path(platform_id) - expected = os.path.join(handler.base_path, "roms", "1") + expected = os.path.join("roms", "1") assert result == expected def test_get_platform_resources_path_different_ids( @@ -44,7 +44,7 @@ class TestFSResourcesHandler: test_ids = [1, 42, 123, 999] for platform_id in test_ids: result = handler.get_platform_resources_path(platform_id) - expected = os.path.join(handler.base_path, "roms", str(platform_id)) + expected = os.path.join("roms", str(platform_id)) assert result == expected def test_cover_exists_no_cover(self, handler: FSResourcesHandler, rom): diff --git a/backend/handler/metadata/ra_handler.py b/backend/handler/metadata/ra_handler.py index a9ca0667d..60debfe91 100644 --- a/backend/handler/metadata/ra_handler.py +++ b/backend/handler/metadata/ra_handler.py @@ -127,10 +127,7 @@ class RAHandler(MetadataHandler): platform_resources_path = fs_resource_handler.get_platform_resources_path( platform_id ) - return os.path.join( - platform_resources_path, - self.HASHES_FILE_NAME, - ) + return os.path.join(platform_resources_path, self.HASHES_FILE_NAME) def _exists_cache_file(self, platform_id: int) -> bool: return fs_resource_handler.file_exists(self._get_hashes_file_path(platform_id)) @@ -160,9 +157,15 @@ class RAHandler(MetadataHandler): include_hashes=True, ) + platform_resources_path = fs_resource_handler.get_platform_resources_path( + rom.platform.id + ) + json_file = json.dumps(roms, indent=4) fs_resource_handler.write_file( - json_file.encode("utf-8"), self._get_hashes_file_path(rom.platform.id) + json_file.encode("utf-8"), + platform_resources_path, + self.HASHES_FILE_NAME, ) else: # Read the roms result from the JSON file