Skip to content

Commit

Permalink
Merge pull request #993 from rommapp/fix/download-removed-cover
Browse files Browse the repository at this point in the history
Fixed re-download lost covers
  • Loading branch information
zurdi15 authored Jul 11, 2024
2 parents 6d6cb35 + 884e7a3 commit 9991aaa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
7 changes: 6 additions & 1 deletion backend/endpoints/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from fastapi import APIRouter, Request, UploadFile
from handler.database import db_collection_handler
from handler.filesystem import fs_resource_handler
from handler.filesystem.base_handler import CoverSize
from logger.logger import log
from models.collection import Collection
from sqlalchemy.inspection import inspect
Expand Down Expand Up @@ -189,7 +190,11 @@ async def update_collection(
artwork_l.write(artwork_file)
cleaned_data.update({"url_cover": ""})
else:
if data.get("url_cover", "") != collection.url_cover:
if data.get(
"url_cover", ""
) != collection.url_cover or not fs_resource_handler.cover_exists(
collection, CoverSize.BIG
):
cleaned_data.update(
{"url_cover": data.get("url_cover", collection.url_cover)}
)
Expand Down
7 changes: 6 additions & 1 deletion backend/endpoints/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from fastapi.responses import FileResponse
from handler.database import db_platform_handler, db_rom_handler
from handler.filesystem import fs_resource_handler, fs_rom_handler
from handler.filesystem.base_handler import CoverSize
from handler.metadata import meta_igdb_handler, meta_moby_handler
from logger.logger import log
from stream_zip import ZIP_AUTO, stream_zip # type: ignore[import]
Expand Down Expand Up @@ -391,7 +392,11 @@ async def update_rom(
artwork_l.write(artwork_file)
cleaned_data.update({"url_cover": ""})
else:
if data.get("url_cover", "") != rom.url_cover:
if data.get(
"url_cover", ""
) != rom.url_cover or not fs_resource_handler.cover_exists(
rom, CoverSize.BIG
):
cleaned_data.update({"url_cover": data.get("url_cover", rom.url_cover)})
path_cover_s, path_cover_l = fs_resource_handler.get_cover(
overwrite=True,
Expand Down
20 changes: 10 additions & 10 deletions backend/handler/filesystem/resources_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self) -> None:
pass

@staticmethod
def _cover_exists(rom: Rom, size: CoverSize):
def cover_exists(entity: Rom | Collection, size: CoverSize):
"""Check if rom cover exists in filesystem
Args:
Expand All @@ -31,7 +31,7 @@ def _cover_exists(rom: Rom, size: CoverSize):
"""
return bool(
os.path.exists(
f"{RESOURCES_BASE_PATH}/{rom.fs_resources_path}/cover/{size.value}.png"
f"{RESOURCES_BASE_PATH}/{entity.fs_resources_path}/cover/{size.value}.png"
)
)

Expand All @@ -49,7 +49,7 @@ def resize_cover_to_small(cover_path: str):
small_img = cover.resize(small_size)
small_img.save(cover_path)

def _store_cover(self, rom: Rom, url_cover: str, size: CoverSize):
def _store_cover(self, entity: Rom | Collection, url_cover: str, size: CoverSize):
"""Store roms resources in filesystem
Args:
Expand All @@ -59,7 +59,7 @@ def _store_cover(self, rom: Rom, url_cover: str, size: CoverSize):
size: size of the cover
"""
cover_file = f"{size.value}.png"
cover_path = f"{RESOURCES_BASE_PATH}/{rom.fs_resources_path}/cover"
cover_path = f"{RESOURCES_BASE_PATH}/{entity.fs_resources_path}/cover"

try:
res = requests.get(
Expand All @@ -81,35 +81,35 @@ def _store_cover(self, rom: Rom, url_cover: str, size: CoverSize):
self.resize_cover_to_small(f"{cover_path}/{cover_file}")

@staticmethod
def _get_cover_path(rom: Rom, size: CoverSize):
def _get_cover_path(entity: Rom | Collection, size: CoverSize):
"""Returns rom cover filesystem path adapted to frontend folder structure
Args:
fs_slug: short name of the platform
file_name: name of rom file
size: size of the cover
"""
return f"{rom.fs_resources_path}/cover/{size.value}.png"
return f"{entity.fs_resources_path}/cover/{size.value}.png"

def get_cover(
self, entity: Rom | Collection | None, overwrite: bool, url_cover: str = ""
) -> tuple[str, str]:
if not entity:
return "", ""

if (overwrite or not self._cover_exists(entity, CoverSize.SMALL)) and url_cover:
if (overwrite or not self.cover_exists(entity, CoverSize.SMALL)) and url_cover:
self._store_cover(entity, url_cover, CoverSize.SMALL)
path_cover_s = (
self._get_cover_path(entity, CoverSize.SMALL)
if self._cover_exists(entity, CoverSize.SMALL)
if self.cover_exists(entity, CoverSize.SMALL)
else ""
)

if (overwrite or not self._cover_exists(entity, CoverSize.BIG)) and url_cover:
if (overwrite or not self.cover_exists(entity, CoverSize.BIG)) and url_cover:
self._store_cover(entity, url_cover, CoverSize.BIG)
path_cover_l = (
self._get_cover_path(entity, CoverSize.BIG)
if self._cover_exists(entity, CoverSize.BIG)
if self.cover_exists(entity, CoverSize.BIG)
else ""
)

Expand Down

0 comments on commit 9991aaa

Please sign in to comment.