Add category column to rom file

This commit is contained in:
Georges-Antoine Assi
2025-01-16 10:49:47 -05:00
parent 0bb938f912
commit 346bbb2db3
8 changed files with 69 additions and 11 deletions

View File

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

View File

@@ -9,7 +9,7 @@ from endpoints.responses.collection import CollectionSchema
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
@@ -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

View File

@@ -461,6 +461,7 @@ async def _identify_rom(
file_path=file.file_path,
file_size_bytes=file.file_size_bytes,
last_modified=file.last_modified,
category=file.category,
)
)
@@ -468,6 +469,9 @@ async def _identify_rom(
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,

View File

@@ -16,7 +16,7 @@ import zipfile_deflate64 # trunk-ignore(ruff/F401): Patches zipfile to support
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,
@@ -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
@@ -238,16 +242,26 @@ class FSRomsHandler(FSHandler):
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]:
@@ -258,12 +272,10 @@ class FSRomsHandler(FSHandler):
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

View File

@@ -29,6 +29,18 @@ if TYPE_CHECKING:
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"
@@ -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")

View File

@@ -31,6 +31,7 @@ export type { MobyMetadataPlatform } from './models/MobyMetadataPlatform';
export type { OIDCDict } from './models/OIDCDict';
export type { PlatformSchema } from './models/PlatformSchema';
export type { Role } from './models/Role';
export type { RomFileCategory } from './models/RomFileCategory';
export type { RomFileSchema } from './models/RomFileSchema';
export type { RomIGDBMetadata } from './models/RomIGDBMetadata';
export type { RomMobyMetadata } from './models/RomMobyMetadata';

View File

@@ -0,0 +1,6 @@
/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
export type RomFileCategory = 'dlc' | 'hack' | 'manual' | 'patch' | 'update' | 'mod' | 'demo' | 'translation' | 'prototype';

View File

@@ -3,6 +3,8 @@
/* tslint:disable */
/* eslint-disable */
import type { RomFileCategory } from './RomFileCategory';
export type RomFileSchema = {
id: number;
rom_id: number;
@@ -15,5 +17,6 @@ export type RomFileSchema = {
crc_hash: (string | null);
md5_hash: (string | null);
sha1_hash: (string | null);
category: (RomFileCategory | null);
};