Skip to content

Commit

Permalink
Fix covers deleted when uploading extension other then PNG
Browse files Browse the repository at this point in the history
  • Loading branch information
gantoine committed Jul 14, 2024
1 parent d8ffc1a commit d05a539
Showing 1 changed file with 17 additions and 10 deletions.
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

0 comments on commit d05a539

Please sign in to comment.