From 23ccd05719b5f887ecb9529bb93f3eb2caf3d027 Mon Sep 17 00:00:00 2001 From: Jordy <33688327+jordy-u@users.noreply.github.com> Date: Sat, 20 Sep 2025 12:12:45 +0200 Subject: [PATCH] Get the image file type from the response content type (#2811) * Get the image file type from the response content type When using the external lookup tool, the file extention in the URL is used for the name of the downloaded image. However, some API's do not use file types in the resource name. If that is the case, the Content-Type of the request will now be used to define the file extention. * Typo: Deleted last empty line on accident. * Typo * Apply code style and simplify this --------- Co-authored-by: Bernd Bestel --- services/StockService.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/services/StockService.php b/services/StockService.php index aba6ef2b..8570b014 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -630,9 +630,18 @@ class StockService extends BaseService { $webClient = new Client(); $response = $webClient->request('GET', $pluginOutput['__image_url'], ['headers' => ['User-Agent' => 'Grocy/' . $this->getApplicationService()->GetInstalledVersion()->Version . ' (https://grocy.info)']]); - $fileName = $pluginOutput['__barcode'] . '.' . pathinfo(parse_url($pluginOutput['__image_url'], PHP_URL_PATH), PATHINFO_EXTENSION); - file_put_contents($this->getFilesService()->GetFilePath('productpictures', $fileName), $response->getBody()); - $productData['picture_file_name'] = $fileName; + $fileName = $pluginOutput['__barcode']; + $fileExtension = pathinfo(parse_url($pluginOutput['__image_url'], PHP_URL_PATH), PATHINFO_EXTENSION); + + // Fallback to Content-Type header if file extension is missing + if (strlen($fileExtension) == 0 && $response->hasHeader('Content-Type')) + { + $fileExtension = explode('+', explode('/', $response->getHeader('Content-Type')[0])[1])[0]; + } + + $filePath = $fileName . '.' . $fileExtension; + file_put_contents($this->getFilesService()->GetFilePath('productpictures', $filePath), $response->getBody()); + $productData['picture_file_name'] = $filePath; } catch (\Exception) {