Support emulator folders

This commit is contained in:
Georges-Antoine Assi
2023-11-26 22:43:03 -05:00
parent d4d929b29b
commit 8422c002c5
8 changed files with 72 additions and 42 deletions

View File

@@ -2,7 +2,7 @@
Revision ID: 0012_asset_files
Revises: 0011_drop_has_cover
Create Date: 2023-11-13 12:25:47.355434
Create Date: 2023-11-26 22:32:20.186371
"""
from alembic import op
@@ -34,6 +34,7 @@ def upgrade() -> None:
)
op.create_table(
"saves",
sa.Column("emulator", sa.String(length=50), nullable=False),
sa.Column("rom_id", sa.Integer(), nullable=False),
sa.Column("platform_slug", sa.String(length=50), nullable=False),
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
@@ -62,6 +63,7 @@ def upgrade() -> None:
)
op.create_table(
"states",
sa.Column("emulator", sa.String(length=50), nullable=False),
sa.Column("rom_id", sa.Integer(), nullable=False),
sa.Column("platform_slug", sa.String(length=50), nullable=False),
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
@@ -76,23 +78,11 @@ def upgrade() -> None:
sa.ForeignKeyConstraint(["rom_id"], ["roms.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
with op.batch_alter_table("roms", schema=None) as batch_op:
batch_op.drop_constraint("fk_platform_roms", type_="foreignkey")
batch_op.create_foreign_key(
None, "platforms", ["platform_slug"], ["slug"], ondelete="CASCADE"
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("roms", schema=None) as batch_op:
batch_op.drop_constraint(None, type_="foreignkey")
batch_op.create_foreign_key(
"fk_platform_roms", "platforms", ["platform_slug"], ["slug"]
)
op.drop_table("states")
op.drop_table("screenshots")
op.drop_table("saves")

View File

@@ -31,11 +31,13 @@ class BaseAsset(BaseModel):
class SaveSchema(BaseAsset):
rom_id: int
platform_slug: str
emulator: str
class StateSchema(BaseAsset):
rom_id: int
platform_slug: str
emulator: str
class ScreenshotSchema(BaseAsset):

View File

@@ -92,14 +92,21 @@ async def scan_platforms(
# Scanning saves
log.info("\t · Saves")
for fs_save_filename in fs_assets["saves"]:
scanned_save = scan_save(scanned_platform, fs_save_filename)
for fs_emulator, fs_save_filename in fs_assets["saves"]:
scanned_save = scan_save(
platform=scanned_platform,
emulator=fs_emulator,
file_name=fs_save_filename,
)
save = dbh.get_save_by_filename(scanned_platform.slug, fs_save_filename)
if save:
dbh.update_save(save.id, { "file_size_bytes": scanned_save.file_size_bytes })
dbh.update_save(
save.id, {"file_size_bytes": scanned_save.file_size_bytes}
)
continue
scanned_save.emulator = fs_emulator
scanned_save.platform_slug = scanned_platform.slug
rom = dbh.get_rom_by_filename_no_tags(scanned_save.file_name_no_tags)
@@ -109,14 +116,21 @@ async def scan_platforms(
# Scanning states
log.info("\t · States")
for fs_state_filename in fs_assets["states"]:
scanned_state = scan_state(scanned_platform, fs_state_filename)
for fs_emulator, fs_state_filename in fs_assets["states"]:
scanned_state = scan_state(
platform=scanned_platform,
emulator=fs_emulator,
file_name=fs_state_filename,
)
state = dbh.get_state_by_filename(scanned_platform.slug, fs_state_filename)
if state:
dbh.update_state(state.id, { "file_size_bytes": scanned_state.file_size_bytes })
dbh.update_state(
state.id, {"file_size_bytes": scanned_state.file_size_bytes}
)
continue
scanned_state.emulator = fs_emulator
scanned_state.platform_slug = scanned_platform.slug
rom = dbh.get_rom_by_filename_no_tags(scanned_state.file_name_no_tags)
@@ -127,18 +141,22 @@ async def scan_platforms(
# Scanning bios
log.info("\t · Firmware")
for fs_bios_filename in fs_assets["bios"]:
scanned_bios = scan_bios(scanned_platform, fs_bios_filename)
scanned_bios = scan_bios(
platform=scanned_platform, file_name=fs_bios_filename
)
bios = dbh.get_bios_by_filename(scanned_platform.slug, fs_bios_filename)
if bios:
dbh.update_bios(bios.id, { "file_size_bytes": scanned_bios.file_size_bytes })
dbh.update_bios(
bios.id, {"file_size_bytes": scanned_bios.file_size_bytes}
)
continue
scanned_bios.platform_slug = scanned_platform.slug
dbh.add_bios(scanned_bios)
dbh.purge_saves(scanned_platform.slug, fs_assets["saves"])
dbh.purge_states(scanned_platform.slug, fs_assets["states"])
dbh.purge_saves(scanned_platform.slug, [s for e, s in fs_assets["saves"]])
dbh.purge_states(scanned_platform.slug, [s for e, s in fs_assets["states"]])
dbh.purge_bios(scanned_platform.slug, fs_assets["bios"])
dbh.purge_roms(scanned_platform.slug, [rom["file_name"] for rom in fs_roms])
@@ -147,10 +165,12 @@ async def scan_platforms(
fs_screenshots = get_screenshots()
for fs_screenshot_filename in fs_screenshots:
scanned_screenshot = scan_screenshot(fs_screenshot_filename)
screenshot = dbh.get_screenshot_by_filename(fs_screenshot_filename)
if screenshot:
dbh.update_screenshot(screenshot.id, { "file_size_bytes": scanned_screenshot.file_size_bytes })
dbh.update_screenshot(
screenshot.id, {"file_size_bytes": scanned_screenshot.file_size_bytes}
)
continue
rom = dbh.get_rom_by_filename_no_tags(scanned_screenshot.file_name_no_tags)

View File

@@ -29,6 +29,8 @@ class Save(BaseAsset):
__tablename__ = "saves"
__table_args__ = {'extend_existing': True}
emulator = Column(String(length=50), nullable=False)
rom_id = Column(Integer(), ForeignKey("roms.id", ondelete='CASCADE'), nullable=False)
rom = relationship("Rom", lazy="selectin", back_populates="saves")
@@ -39,6 +41,8 @@ class State(BaseAsset):
__tablename__ = "states"
__table_args__ = {'extend_existing': True}
emulator = Column(String(length=50), nullable=False)
rom_id = Column(Integer(), ForeignKey("roms.id", ondelete='CASCADE'), nullable=False)
rom = relationship("Rom", lazy="selectin", back_populates="states")

View File

@@ -1,4 +1,5 @@
import emoji
import os
from typing import Any
from handler import igdbh
@@ -131,16 +132,17 @@ def _scan_asset(file_name: str, path: str):
}
def scan_save(platform: Platform, file_name: str) -> Save:
def scan_save(platform: Platform, emulator: str, file_name: str) -> Save:
saves_path = fs.get_fs_structure(platform.fs_slug, folder=config.SAVES_FOLDER_NAME)
return Save(**_scan_asset(file_name, saves_path))
# Scan asset with the sames path and emulator folder name
return Save(**_scan_asset(file_name, os.path.join(saves_path, emulator)))
def scan_state(platform: Platform, file_name: str) -> State:
def scan_state(platform: Platform, emulator: str, file_name: str) -> State:
states_path = fs.get_fs_structure(
platform.fs_slug, folder=config.STATES_FOLDER_NAME
)
return State(**_scan_asset(file_name, states_path))
return State(**_scan_asset(file_name, os.path.join(states_path, emulator)))
def scan_bios(platform: Platform, file_name: str) -> State:

View File

@@ -194,9 +194,7 @@ def store_default_resources():
# ========= Platforms utils =========
def _exclude_platforms(platforms: list):
return [
platform
for platform in platforms
if platform not in config.EXCLUDED_PLATFORMS
platform for platform in platforms if platform not in config.EXCLUDED_PLATFORMS
]
@@ -305,7 +303,12 @@ def get_assets(fs_slug: str):
fs_bios: list[str] = []
try:
fs_saves = list(os.walk(saves_file_path))[0][2]
emulators = list(os.walk(saves_file_path))[0][1]
for emulator in emulators:
fs_saves += [
(emulator, file)
for file in list(os.walk(f"{saves_file_path}/{emulator}"))[0][2]
]
except IndexError:
pass
@@ -313,7 +316,12 @@ def get_assets(fs_slug: str):
states_file_path = f"{LIBRARY_BASE_PATH}/{states_path}"
try:
fs_states = list(os.walk(states_file_path))[0][2]
emulators = list(os.walk(states_file_path))[0][1]
for emulator in emulators:
fs_states += [
(emulator, file)
for file in list(os.walk(f"{states_file_path}/{emulator}"))[0][2]
]
except IndexError:
pass
@@ -377,15 +385,17 @@ def _file_exists(path: str, file_name: str):
Returns
True if file exists in filesystem else False
"""
return bool(os.path.exists(f'{LIBRARY_BASE_PATH}/{path}/{file_name}'))
return bool(os.path.exists(f"{LIBRARY_BASE_PATH}/{path}/{file_name}"))
def rename_file(fs_slug: str, old_name: str, new_name: str, folder: str = config.ROMS_FOLDER_NAME):
def rename_file(
fs_slug: str, old_name: str, new_name: str, folder: str = config.ROMS_FOLDER_NAME
):
if new_name != old_name:
files_path = get_fs_structure(fs_slug, folder=folder)
if _file_exists(path=files_path, file_name=new_name):
raise RomAlreadyExistsException(new_name)
os.rename(
f"{LIBRARY_BASE_PATH}/{files_path}/{old_name}",
f"{LIBRARY_BASE_PATH}/{files_path}/{new_name}",
@@ -399,10 +409,12 @@ def remove_file(fs_slug: str, file_name: str, folder: str = config.ROMS_FOLDER_N
except IsADirectoryError:
shutil.rmtree(f"{LIBRARY_BASE_PATH}/{files_path}/{file_name}")
def build_upload_file_path(fs_slug: str, folder: str = config.ROMS_FOLDER_NAME):
rom_path = get_fs_structure(fs_slug, folder=folder)
return f"{LIBRARY_BASE_PATH}/{rom_path}"
def build_artwork_path(rom_name: str, fs_slug: str, file_ext: str):
q_rom_name = quote(rom_name)
strtime = str(datetime.datetime.now().timestamp())

View File

@@ -91,7 +91,7 @@ async function uploadSaves() {
v-for="save in rom.saves"
:key="save.id"
:title="save.file_name"
:subtitle="formatBytes(save.file_size_bytes)"
:subtitle="`${save.emulator} - ${formatBytes(save.file_size_bytes)}`"
>
<template v-slot:append>
<v-btn icon :href="save.download_path" download>

View File

@@ -91,7 +91,7 @@ async function uploadStates() {
v-for="state in rom.states"
:key="state.id"
:title="state.file_name"
:subtitle="formatBytes(state.file_size_bytes)"
:subtitle="`${state.emulator} - ${formatBytes(state.file_size_bytes)}`"
>
<template v-slot:append>
<v-btn icon :href="state.download_path" download>