Skip to content

Commit

Permalink
Add category column to rom file
Browse files Browse the repository at this point in the history
  • Loading branch information
gantoine committed Jan 16, 2025
1 parent 0bb938f commit 346bbb2
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 11 deletions.
16 changes: 16 additions & 0 deletions backend/alembic/versions/0031_rom_file_and_hashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ def upgrade() -> None:
sa.Column("crc_hash", sa.String(length=100), nullable=True),
sa.Column("md5_hash", sa.String(length=100), nullable=True),
sa.Column("sha1_hash", sa.String(length=100), nullable=True),
sa.Column(
"category",
sa.Enum(
"DLC",
"HACK",
"MANUAL",
"PATCH",
"UPDATE",
"MOD",
"DEMO",
"TRANSLATION",
"PROTOTYPE",
name="romfilecategory",
),
nullable=True,
),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
Expand Down
3 changes: 2 additions & 1 deletion backend/endpoints/responses/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from fastapi import Request
from handler.metadata.igdb_handler import IGDBMetadata
from handler.metadata.moby_handler import MobyMetadata
from models.rom import Rom, RomUserStatus
from models.rom import Rom, RomFileCategory, RomUserStatus
from pydantic import computed_field

from .base import BaseModel
Expand Down Expand Up @@ -108,6 +108,7 @@ class RomFileSchema(BaseModel):
crc_hash: str | None
md5_hash: str | None
sha1_hash: str | None
category: RomFileCategory | None

class Config:
from_attributes = True
Expand Down
4 changes: 4 additions & 0 deletions backend/endpoints/sockets/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,17 @@ async def _identify_rom(
file_path=file.file_path,
file_size_bytes=file.file_size_bytes,
last_modified=file.last_modified,
category=file.category,
)
)

# Calculating hashes is expensive, so we only do it if necessary
if not rom or scan_type == ScanType.COMPLETE or scan_type == ScanType.HASHES:
# Skip hashing games for platforms that don't have a hash database
if platform.slug not in NON_HASHABLE_PLATFORMS:
# Uncomment this to run scan in the current process
# _set_rom_hashes(_added_rom.id)

low_prio_queue.enqueue(
_set_rom_hashes,
_added_rom.id,
Expand Down
32 changes: 22 additions & 10 deletions backend/handler/filesystem/roms_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from config import LIBRARY_BASE_PATH
from config.config_manager import config_manager as cm
from exceptions.fs_exceptions import RomAlreadyExistsException, RomsNotFoundException
from models.rom import Rom, RomFile
from models.rom import Rom, RomFile, RomFileCategory
from py7zr.exceptions import (
Bad7zFile,
DecompressionError,
Expand Down Expand Up @@ -173,6 +173,10 @@ def read_bz2_file(file_path: Path) -> Iterator[bytes]:
yield chunk


def category_matches(category: str, path_parts: list[str]):
return category in path_parts or f"{category}s" in path_parts


class FSRomsHandler(FSHandler):
def __init__(self) -> None:
pass
Expand Down Expand Up @@ -238,16 +242,26 @@ def _exclude_multi_roms(self, roms: list[str]) -> list[str]:

return [f for f in roms if f not in filtered_files]

def _build_rom_file(self, rom_path: str, file_name: str) -> RomFile:
abs_file_path = Path(
LIBRARY_BASE_PATH, rom_path, file_name
) # Absolute path to roms
def _build_rom_file(self, rom_path: Path, file_name: str) -> RomFile:
# Absolute path to roms
abs_file_path = Path(LIBRARY_BASE_PATH, rom_path, file_name)

path_parts_lower = list(map(str.lower, rom_path.parts))
matching_category = next(
(
category
for category in RomFileCategory
if category_matches(category.value, path_parts_lower)
),
None,
)

return RomFile(
file_name=file_name,
file_path=rom_path,
file_path=str(rom_path),
file_size_bytes=os.stat(abs_file_path).st_size,
last_modified=os.path.getmtime(abs_file_path),
category=matching_category,
)

def get_rom_files(self, rom: str, roms_path: str) -> list[RomFile]:
Expand All @@ -258,12 +272,10 @@ def get_rom_files(self, rom: str, roms_path: str) -> list[RomFile]:
if os.path.isdir(f"{abs_fs_path}/{rom}"):
for f_path, file in iter_files(f"{abs_fs_path}/{rom}", recursive=True):
rom_files.append(
self._build_rom_file(
str(f_path.relative_to(LIBRARY_BASE_PATH)), file
)
self._build_rom_file(f_path.relative_to(LIBRARY_BASE_PATH), file)
)
else:
rom_files.append(self._build_rom_file(roms_path, rom))
rom_files.append(self._build_rom_file(Path(roms_path), rom))

return rom_files

Expand Down
15 changes: 15 additions & 0 deletions backend/models/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@
from models.user import User


class RomFileCategory(enum.StrEnum):
DLC = "dlc"
HACK = "hack"
MANUAL = "manual"
PATCH = "patch"
UPDATE = "update"
MOD = "mod"
DEMO = "demo"
TRANSLATION = "translation"
PROTOTYPE = "prototype"


class RomFile(BaseModel):
__tablename__ = "rom_files"

Expand All @@ -41,6 +53,9 @@ class RomFile(BaseModel):
crc_hash: Mapped[str | None] = mapped_column(String(100))
md5_hash: Mapped[str | None] = mapped_column(String(100))
sha1_hash: Mapped[str | None] = mapped_column(String(100))
category: Mapped[RomFileCategory | None] = mapped_column(
Enum(RomFileCategory), default=None
)

rom: Mapped[Rom] = relationship(lazy="joined")

Expand Down
1 change: 1 addition & 0 deletions frontend/src/__generated__/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions frontend/src/__generated__/models/RomFileCategory.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions frontend/src/__generated__/models/RomFileSchema.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 346bbb2

Please sign in to comment.