Skip to content

Commit

Permalink
Merge pull request #796 from rommapp/792-feature-remove-cover-art
Browse files Browse the repository at this point in the history
Improved custom art management
  • Loading branch information
zurdi15 authored Apr 8, 2024
2 parents d9d7eea + dd2f57f commit 508b5db
Show file tree
Hide file tree
Showing 14 changed files with 328 additions and 156 deletions.
41 changes: 27 additions & 14 deletions backend/endpoints/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ async def update_rom(
request: Request,
id: int,
rename_as_igdb: bool = False,
remove_cover: bool = False,
artwork: Optional[UploadFile] = File(None),
) -> RomSchema:
"""Update rom endpoint
Expand Down Expand Up @@ -317,22 +318,34 @@ async def update_rom(
cleaned_data["file_name_no_ext"] = fs_rom_handler.get_file_name_with_no_extension(
fs_safe_file_name
)
cleaned_data.update(
fs_resource_handler.get_rom_cover(
overwrite=True,
platform_fs_slug=platform_fs_slug,
rom_name=cleaned_data["name"],
url_cover=cleaned_data.get("url_cover", ""),

if remove_cover:
cleaned_data.update(
fs_resource_handler.remove_cover(
rom_name=cleaned_data["name"], platform_fs_slug=platform_fs_slug
)
)
else:
cleaned_data.update(
fs_resource_handler.get_rom_cover(
overwrite=True,
platform_fs_slug=platform_fs_slug,
rom_name=cleaned_data["name"],
url_cover=cleaned_data.get("url_cover", ""),
)
)
)

cleaned_data.update(
fs_resource_handler.get_rom_screenshots(
platform_fs_slug=platform_fs_slug,
rom_name=cleaned_data["name"],
url_screenshots=cleaned_data.get("url_screenshots", []),
),
)
if (
cleaned_data["igdb_id"] != db_rom.igdb_id
or cleaned_data["moby_id"] != db_rom.moby_id
):
cleaned_data.update(
fs_resource_handler.get_rom_screenshots(
platform_fs_slug=platform_fs_slug,
rom_name=cleaned_data["name"],
url_screenshots=cleaned_data.get("url_screenshots", []),
),
)

if artwork is not None:
file_ext = artwork.filename.split(".")[-1]
Expand Down
48 changes: 34 additions & 14 deletions backend/handler/fs_handler/fs_resources_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class FSResourceHandler(FSHandler):
def __init__(self) -> None:
pass

def _cover_exists(self, fs_slug: str, rom_name: str, size: CoverSize):
@staticmethod
def _cover_exists(fs_slug: str, rom_name: str, size: CoverSize):
"""Check if rom cover exists in filesystem
Args:
Expand All @@ -39,7 +40,8 @@ def _cover_exists(self, fs_slug: str, rom_name: str, size: CoverSize):
)
)

def resize_cover(self, cover_path: str, size: CoverSize = CoverSize.BIG) -> None:
@staticmethod
def resize_cover(cover_path: str, size: CoverSize = CoverSize.BIG) -> None:
"""Resizes the cover image to the standard size
Args:
Expand All @@ -48,7 +50,6 @@ def resize_cover(self, cover_path: str, size: CoverSize = CoverSize.BIG) -> None
"""
cover = Image.open(cover_path)
if size == CoverSize.BIG and cover.size[1] > DEFAULT_HEIGHT_COVER_L:

big_dimensions = (DEFAULT_WIDTH_COVER_L, DEFAULT_HEIGHT_COVER_L)
background = Image.new("RGBA", big_dimensions, (0, 0, 0, 0))
cover.thumbnail(big_dimensions)
Expand Down Expand Up @@ -86,7 +87,7 @@ def _store_cover(
"""
cover_file = f"{size.value}.png"
cover_path = f"{RESOURCES_BASE_PATH}/{fs_slug}/{rom_name}/cover"

try:
res = requests.get(
url_cover.replace("t_thumb", f"t_cover_{size.value}"),
Expand All @@ -99,14 +100,15 @@ def _store_cover(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Can't connect to IGDB, check your internet connection.",
)

if res.status_code == 200:
Path(cover_path).mkdir(parents=True, exist_ok=True)
with open(f"{cover_path}/{cover_file}", "wb") as f:
shutil.copyfileobj(res.raw, f)
self.resize_cover(f"{cover_path}/{cover_file}", size)

def _get_cover_path(self, fs_slug: str, rom_name: str, size: CoverSize):
@staticmethod
def _get_cover_path(fs_slug: str, rom_name: str, size: CoverSize):
"""Returns rom cover filesystem path adapted to frontend folder structure
Args:
Expand Down Expand Up @@ -147,18 +149,35 @@ def get_rom_cover(
"path_cover_l": path_cover_l,
}

def build_artwork_path(self, rom_name: str, fs_slug: str, file_ext: str):
@staticmethod
def remove_cover(
rom_name: str,
platform_fs_slug: str,
):
try:
shutil.rmtree(
os.path.join(RESOURCES_BASE_PATH, platform_fs_slug, rom_name, "cover")
)
except FileNotFoundError:
log.warning(f"Couldn't remove {rom_name} cover")
return {"path_cover_s": "", "path_cover_l": ""}

@staticmethod
def build_artwork_path(rom_name: str, platform_fs_slug: str, file_ext: str):
q_rom_name = quote(rom_name)

path_cover_l = f"{fs_slug}/{q_rom_name}/cover/{CoverSize.BIG.value}.{file_ext}"
path_cover_l = (
f"{platform_fs_slug}/{q_rom_name}/cover/{CoverSize.BIG.value}.{file_ext}"
)
path_cover_s = (
f"{fs_slug}/{q_rom_name}/cover/{CoverSize.SMALL.value}.{file_ext}"
f"{platform_fs_slug}/{q_rom_name}/cover/{CoverSize.SMALL.value}.{file_ext}"
)
artwork_path = f"{RESOURCES_BASE_PATH}/{fs_slug}/{rom_name}/cover"
artwork_path = f"{RESOURCES_BASE_PATH}/{platform_fs_slug}/{rom_name}/cover"
Path(artwork_path).mkdir(parents=True, exist_ok=True)
return path_cover_l, path_cover_s, artwork_path

def _store_screenshot(self, fs_slug: str, rom_name: str, url: str, idx: int):
@staticmethod
def _store_screenshot(fs_slug: str, rom_name: str, url: str, idx: int):
"""Store roms resources in filesystem
Args:
Expand All @@ -168,7 +187,7 @@ def _store_screenshot(self, fs_slug: str, rom_name: str, url: str, idx: int):
"""
screenshot_file = f"{idx}.jpg"
screenshot_path = f"{RESOURCES_BASE_PATH}/{fs_slug}/{rom_name}/screenshots"

try:
res = requests.get(url, stream=True, timeout=120)
except requests.exceptions.ConnectionError:
Expand All @@ -177,7 +196,7 @@ def _store_screenshot(self, fs_slug: str, rom_name: str, url: str, idx: int):
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Can't connect to IGDB, check your internet connection.",
)

if res.status_code == 200:
Path(screenshot_path).mkdir(parents=True, exist_ok=True)
with open(f"{screenshot_path}/{screenshot_file}", "wb") as f:
Expand All @@ -188,7 +207,8 @@ def _store_screenshot(self, fs_slug: str, rom_name: str, url: str, idx: int):
f"Failure writing screenshot {url} to file (ProtocolError)"
)

def _get_screenshot_path(self, fs_slug: str, rom_name: str, idx: str):
@staticmethod
def _get_screenshot_path(fs_slug: str, rom_name: str, idx: str):
"""Returns rom cover filesystem path adapted to frontend folder structure
Args:
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/Details/BackgroundHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const theme = useTheme();
const props = defineProps<{ rom: RomSchema }>();
const imgSrc =
!props.rom.igdb_id && !props.rom.has_cover
!props.rom.igdb_id && !props.rom.moby_id && !props.rom.has_cover
? `/assets/default/cover/big_${theme.global.name.value}_unmatched.png`
: !props.rom.has_cover
? `/assets/default/cover/big_${theme.global.name.value}_missing_cover.png`
: `/assets/romm/resources/${props.rom.path_cover_s}`;
const imgSrcLazy =
!props.rom.igdb_id && !props.rom.has_cover
!props.rom.igdb_id && !props.rom.moby_id && !props.rom.has_cover
? `/assets/default/cover/small_${theme.global.name.value}_unmatched.png`
: !props.rom.has_cover
? `/assets/default/cover/small_${theme.global.name.value}_missing_cover.png`
Expand Down
28 changes: 7 additions & 21 deletions frontend/src/components/Details/Cover.vue
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
<script setup lang="ts">
import storeDownload from "@/stores/download";
import type { Rom } from "@/stores/roms";
import { useTheme } from "vuetify";
const theme = useTheme();
defineProps<{ romId: number; src: string; lazySrc: string }>();
const downloadStore = storeDownload();
defineProps<{ rom: Rom }>();
</script>
<template>
<v-card
elevation="2"
:loading="downloadStore.value.includes(rom.id) ? 'romm-accent-1' : false"
:loading="downloadStore.value.includes(romId) ? 'romm-accent-1' : false"
>
<v-img
:value="rom.id"
:key="rom.id"
:src="
!rom.igdb_id && !rom.has_cover
? `/assets/default/cover/big_${theme.global.name.value}_unmatched.png`
: !rom.has_cover
? `/assets/default/cover/big_${theme.global.name.value}_missing_cover.png`
: `/assets/romm/resources/${rom.path_cover_l}`
"
:lazy-src="
!rom.igdb_id && !rom.has_cover
? `/assets/default/cover/small_${theme.global.name.value}_unmatched.png`
: !rom.has_cover
? `/assets/default/cover/small_${theme.global.name.value}_missing_cover.png`
: `/assets/romm/resources/${rom.path_cover_s}`
"
:value="romId"
:key="romId"
:src="src"
:lazy-src="lazySrc"
:aspect-ratio="3 / 4"
>
<slot name="editable"></slot>
<template v-slot:placeholder>
<div class="d-flex align-center justify-center fill-height">
<v-progress-circular
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Details/SourceTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defineProps<{ rom: Rom }>();
<template>
<div rounded="0" class="table">
<v-row
v-if="rom.igdb_id"
v-if="rom.igdb_id || rom.moby_id"
class="align-center justify-center pa-2"
no-gutters
>
Expand Down
Loading

0 comments on commit 508b5db

Please sign in to comment.