fix some aggressive usage

This commit is contained in:
Georges-Antoine Assi
2025-07-17 21:20:02 -04:00
parent c93687a0f4
commit 0df4eb1985
4 changed files with 14 additions and 18 deletions

View File

@@ -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")

View File

@@ -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

View File

@@ -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):

View File

@@ -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