Skip to content

Commit

Permalink
NTR: fix URL encoding for filenames (#741)
Browse files Browse the repository at this point in the history
Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored May 2, 2024
1 parent f2985bc commit 4d077fd
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/Service/MollieApi/LineItemDataExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public function extractExtraData(OrderLineItemEntity $lineItem): LineItemExtraDa
&& $medias->first() instanceof ProductMediaEntity
&& $medias->first()->getMedia() instanceof MediaEntity
) {
$extraData->setImageUrl($medias->first()->getMedia()->getUrl());
$url = $medias->first()->getMedia()->getUrl();
$url = $this->encodePathAndQuery($url);
$extraData->setImageUrl($url);
}

$seoUrls = $product->getSeoUrls();
Expand All @@ -41,4 +43,38 @@ public function extractExtraData(OrderLineItemEntity $lineItem): LineItemExtraDa

return $extraData;
}

private function encodePathAndQuery(string $fullUrl):string
{
$urlParts = parse_url($fullUrl);

$scheme = isset($urlParts['scheme']) ? $urlParts['scheme'] . '://' : '';

$host = isset($urlParts['host']) ? $urlParts['host'] : '';

$port = isset($urlParts['port']) ? ':' . $urlParts['port'] : '';

$user = isset($urlParts['user']) ? $urlParts['user'] : '';

$pass = isset($urlParts['pass']) ? ':' . $urlParts['pass'] : '';

$pass = ($user || $pass) ? "$pass@" : '';

$path = isset($urlParts['path']) ? $urlParts['path'] : '';

if (mb_strlen($path) > 0) {
$pathParts = explode('/', $path);
array_walk($pathParts, function (&$pathPart) {
$pathPart = rawurlencode($pathPart);
});
$path = implode('/', $pathParts);
}

$query = isset($urlParts['query']) ? '?' . $urlParts['query'] : '';


$fragment = isset($urlParts['fragment']) ? '#' . $urlParts['fragment'] : '';

return trim($scheme.$user.$pass.$host.$port.$path.$query.$fragment);
}
}

0 comments on commit 4d077fd

Please sign in to comment.