mirror of
https://github.com/grocy/grocy.git
synced 2025-10-12 16:44:55 +00:00
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 <bernd@berrnd.de>
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
Reference in New Issue
Block a user