Fix duplicate thumbnail creation (#318)
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Tue, 17 May 2022 17:55:10 +0000 (19:55 +0200)
committerGitHub <noreply@github.com>
Tue, 17 May 2022 17:55:10 +0000 (19:55 +0200)
music_assistant/controllers/metadata/__init__.py
music_assistant/helpers/database.py
music_assistant/helpers/images.py

index 097318508f61773bb38df0655c6700bfb7014ffc..11cfa36c08033860990679b073cf9141408fc38d 100755 (executable)
@@ -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
index 8419d7a055149dbfaea4e4aab72f9ae09d5262f0..4721234ca124a06dbe6fdd5fd46b9ee87959df69 100755 (executable)
@@ -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));"""
         )
index 2cce16e4cd1d80fc3b60d829a745950525ec2491..145280d82e7ea01f1012430371a6ecd9b14d95fb 100644 (file)
@@ -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()