From 9306e698067ecbad02f49db3e6be72b0572d3ea8 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Tue, 17 May 2022 19:55:10 +0200 Subject: [PATCH] Fix duplicate thumbnail creation (#318) --- music_assistant/controllers/metadata/__init__.py | 6 +++--- music_assistant/helpers/database.py | 10 ++++++++-- music_assistant/helpers/images.py | 5 ++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/music_assistant/controllers/metadata/__init__.py b/music_assistant/controllers/metadata/__init__.py index 09731850..11cfa36c 100755 --- a/music_assistant/controllers/metadata/__init__.py +++ b/music_assistant/controllers/metadata/__init__.py @@ -161,7 +161,7 @@ class MetaDataController: self, media_item: MediaItemType, img_type: ImageType = ImageType.THUMB, - size: Optional[int] = None, + size: int = 0, ) -> bytes | None: """Get image data for given MedaItem.""" img_path = await self.get_image_url_for_item( @@ -179,7 +179,7 @@ class MetaDataController: media_item: MediaItemType, img_type: ImageType = ImageType.THUMB, allow_local: bool = True, - local_as_base64: bool = True, + local_as_base64: bool = False, ) -> str | None: """Get url to image for given media media_item.""" if not media_item: @@ -219,7 +219,7 @@ class MetaDataController: return None async def get_thumbnail( - self, path: str, size: Optional[int] = None, base64: bool = False + self, path: str, size: int = 0, base64: bool = False ) -> bytes | str: """Get/create thumbnail image for path (image url or local path).""" # check if we already have this cached in the db diff --git a/music_assistant/helpers/database.py b/music_assistant/helpers/database.py index 8419d7a0..4721234c 100755 --- a/music_assistant/helpers/database.py +++ b/music_assistant/helpers/database.py @@ -10,7 +10,7 @@ if TYPE_CHECKING: from music_assistant.mass import MusicAssistant -SCHEMA_VERSION = 10 +SCHEMA_VERSION = 11 TABLE_PROV_MAPPINGS = "provider_mappings" TABLE_TRACK_LOUDNESS = "track_loudness" @@ -198,6 +198,12 @@ class Database: # recreate missing tables await self.__create_database_tables(db) + if prev_version < 11: + # fix for duplicate thumbs creation + await db.execute(f"DROP TABLE IF EXISTS {TABLE_THUMBS}") + # recreate missing tables + await self.__create_database_tables(db) + # store current schema version await self.set_setting("version", str(SCHEMA_VERSION), db=db) @@ -305,7 +311,7 @@ class Database: f"""CREATE TABLE IF NOT EXISTS {TABLE_THUMBS}( id INTEGER PRIMARY KEY AUTOINCREMENT, path TEXT NOT NULL, - size INTEGER NULL, + size INTEGER DEFAULT 0, data BLOB, UNIQUE(path, size));""" ) diff --git a/music_assistant/helpers/images.py b/music_assistant/helpers/images.py index 2cce16e4..145280d8 100644 --- a/music_assistant/helpers/images.py +++ b/music_assistant/helpers/images.py @@ -14,8 +14,6 @@ async def create_thumbnail( mass: MusicAssistant, path: str, size: Optional[int] ) -> bytes: """Create thumbnail from image url.""" - if not size: - size = 200 img_data = None if path.startswith("http"): async with mass.http_session.get(path, verify_ssl=False) as response: @@ -41,7 +39,8 @@ async def create_thumbnail( def _create_image(): data = BytesIO(img_data) img = Image.open(data) - img.thumbnail((size, size), Image.ANTIALIAS) + if size: + img.thumbnail((size, size), Image.ANTIALIAS) img.save(data, format="png") return data.getvalue() -- 2.34.1