Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix covers deleted when uploading extension other then PNG #1002

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions backend/handler/filesystem/resources_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
import glob
import shutil
from pathlib import Path

Expand All @@ -19,7 +19,7 @@ def __init__(self) -> None:
pass

@staticmethod
def cover_exists(entity: Rom | Collection, size: CoverSize):
def cover_exists(entity: Rom | Collection, size: CoverSize) -> bool:
"""Check if rom cover exists in filesystem

Args:
Expand All @@ -29,14 +29,13 @@ def cover_exists(entity: Rom | Collection, size: CoverSize):
Returns
True if cover exists in filesystem else False
"""
return bool(
os.path.exists(
f"{RESOURCES_BASE_PATH}/{entity.fs_resources_path}/cover/{size.value}.png"
)
matched_files = glob.glob(
f"{RESOURCES_BASE_PATH}/{entity.fs_resources_path}/cover/{size.value}.*"
)
return len(matched_files) > 0

@staticmethod
def resize_cover_to_small(cover_path: str):
def resize_cover_to_small(cover_path: str) -> None:
"""Path of the cover image to resize"""
cover = Image.open(cover_path)
if cover.height >= 1000:
Expand All @@ -49,7 +48,9 @@ def resize_cover_to_small(cover_path: str):
small_img = cover.resize(small_size)
small_img.save(cover_path)

def _store_cover(self, entity: Rom | Collection, url_cover: str, size: CoverSize):
def _store_cover(
self, entity: Rom | Collection, url_cover: str, size: CoverSize
) -> None:
"""Store roms resources in filesystem

Args:
Expand Down Expand Up @@ -81,15 +82,21 @@ def _store_cover(self, entity: Rom | Collection, url_cover: str, size: CoverSize
self.resize_cover_to_small(f"{cover_path}/{cover_file}")

@staticmethod
def _get_cover_path(entity: Rom | Collection, size: CoverSize):
def _get_cover_path(entity: Rom | Collection, size: CoverSize) -> str:
"""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"{entity.fs_resources_path}/cover/{size.value}.png"
file_path = (
f"{RESOURCES_BASE_PATH}/{entity.fs_resources_path}/cover/{size.value}.*"
)
matched_files = glob.glob(file_path, recursive=True)
return (
matched_files[0].replace(RESOURCES_BASE_PATH, "") if matched_files else ""
)

def get_cover(
self, entity: Rom | Collection | None, overwrite: bool, url_cover: str = ""
Expand Down
Loading