Support data: URI images for external barcode lookup plugins (#2814)

* add support for data: URI images for external barcode lookup plugins

* Adapt existing code style

---------

Co-authored-by: Bernd Bestel <bernd@berrnd.de>
This commit is contained in:
GammaC0de
2025-09-28 21:37:47 +03:00
committed by GitHub
parent 8129b6b60b
commit 1934256f29

View File

@@ -627,10 +627,11 @@ class StockService extends BaseService
if (isset($pluginOutput['__image_url']) && !empty($pluginOutput['__image_url'])) if (isset($pluginOutput['__image_url']) && !empty($pluginOutput['__image_url']))
{ {
try try
{
if (preg_match('/^https?:\/\//', $pluginOutput['__image_url']))
{ {
$webClient = new Client(); $webClient = new Client();
$response = $webClient->request('GET', $pluginOutput['__image_url'], ['headers' => ['User-Agent' => 'Grocy/' . $this->getApplicationService()->GetInstalledVersion()->Version . ' (https://grocy.info)']]); $response = $webClient->request('GET', $pluginOutput['__image_url'], ['headers' => ['User-Agent' => 'Grocy/' . $this->getApplicationService()->GetInstalledVersion()->Version . ' (https://grocy.info)']]);
$fileName = $pluginOutput['__barcode'];
$fileExtension = pathinfo(parse_url($pluginOutput['__image_url'], PHP_URL_PATH), PATHINFO_EXTENSION); $fileExtension = pathinfo(parse_url($pluginOutput['__image_url'], PHP_URL_PATH), PATHINFO_EXTENSION);
// Fallback to Content-Type header if file extension is missing // Fallback to Content-Type header if file extension is missing
@@ -639,9 +640,23 @@ class StockService extends BaseService
$fileExtension = explode('+', explode('/', $response->getHeader('Content-Type')[0])[1])[0]; $fileExtension = explode('+', explode('/', $response->getHeader('Content-Type')[0])[1])[0];
} }
$filePath = $fileName . '.' . $fileExtension; $imageData = $response->getBody();
file_put_contents($this->getFilesService()->GetFilePath('productpictures', $filePath), $response->getBody()); }
$productData['picture_file_name'] = $filePath; elseif (preg_match('/data:image\/(\w+?);base64,([A-Za-z0-9+\/]*={0,2})$/', $pluginOutput['__image_url'], $matches))
{
$fileExtension = $matches[1];
if (!($imageData = base64_decode($matches[2])))
{
unset($imageData);
}
}
if (!empty($fileExtension) && !empty($imageData))
{
$fileName = $pluginOutput['__barcode'] . '.' . $fileExtension;
file_put_contents($this->getFilesService()->GetFilePath('productpictures', $fileName), $imageData);
$productData['picture_file_name'] = $fileName;
}
} }
catch (\Exception) catch (\Exception)
{ {