From 208b2a00ed39091934c1a5acd318aef9b9507ff5 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Thu, 9 Mar 2023 19:51:15 +0100 Subject: [PATCH] fix embedded images --- music_assistant/server/controllers/metadata.py | 18 +++++++++++------- music_assistant/server/helpers/images.py | 3 +-- music_assistant/server/helpers/tags.py | 18 ++++++++++++------ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/music_assistant/server/controllers/metadata.py b/music_assistant/server/controllers/metadata.py index 120db676..abbcb2bf 100755 --- a/music_assistant/server/controllers/metadata.py +++ b/music_assistant/server/controllers/metadata.py @@ -4,6 +4,7 @@ from __future__ import annotations import asyncio import logging import os +import urllib.parse from base64 import b64encode from random import shuffle from time import time @@ -12,12 +13,7 @@ from typing import TYPE_CHECKING import aiofiles from aiohttp import web -from music_assistant.common.models.enums import ( - ImageType, - MediaType, - ProviderFeature, - ProviderType, -) +from music_assistant.common.models.enums import ImageType, MediaType, ProviderFeature, ProviderType from music_assistant.common.models.media_items import ( Album, Artist, @@ -310,7 +306,15 @@ class MetaDataController: """Handle request for image proxy.""" path = request.query["path"] size = int(request.query.get("size", "0")) - image_data = await self.get_thumbnail(path, size) + if "%" in path: + # assume (double) encoded url, decode it + path = urllib.parse.unquote(path) + + try: + image_data = await self.get_thumbnail(path, size) + except Exception as err: + LOGGER.exception(str(err), exc_info=err) + # we set the cache header to 1 year (forever) # the client can use the checksum value to refresh when content changes return web.Response( diff --git a/music_assistant/server/helpers/images.py b/music_assistant/server/helpers/images.py index 57814363..79279551 100644 --- a/music_assistant/server/helpers/images.py +++ b/music_assistant/server/helpers/images.py @@ -27,8 +27,7 @@ async def get_image_data(mass: MusicAssistant, path: str) -> bytes: continue if not await prov.exists(path): continue - path = await prov.resolve(path) - img_data = await get_embedded_image(path.local_path) + img_data = await get_embedded_image(prov.read_file_content(path)) if img_data: return img_data raise FileNotFoundError(f"Image not found: {path}") diff --git a/music_assistant/server/helpers/tags.py b/music_assistant/server/helpers/tags.py index ec1f731f..0081a7dd 100644 --- a/music_assistant/server/helpers/tags.py +++ b/music_assistant/server/helpers/tags.py @@ -233,6 +233,7 @@ async def parse_tags(input_file: str | AsyncGenerator[bytes, None]) -> AudioTags await proc.write(chunk) except BrokenPipeError: break # race-condition: read enough data for tags + proc.write_eof() proc.attach_task(chunk_feeder()) @@ -272,16 +273,21 @@ async def get_embedded_image(input_file: str | AsyncGenerator[bytes, None]) -> b async with AsyncProcess( args, enable_stdin=file_path == "-", enable_stdout=True, enable_stderr=False ) as proc: - if file_path == "-": - # feed the file contents to the process - async for chunk in input_file: - await proc.write(chunk) - if file_path == "-": # feed the file contents to the process async def chunk_feeder(): + bytes_written = 0 async for chunk in input_file: - await proc.write(chunk) + try: + await proc.write(chunk) + except BrokenPipeError: + break # race-condition: read enough data for tags + + # grabbing the first 5MB is enough to get the embedded image + bytes_written += len(chunk) + if bytes_written > (5 * 1024000): + break + proc.write_eof() proc.attach_task(chunk_feeder()) -- 2.34.1