From 1752cd08114a15af03df649f89f2712db0bbe8fb Mon Sep 17 00:00:00 2001 From: Arthur de Moulins Date: Thu, 14 Nov 2024 18:05:39 +0100 Subject: [PATCH] fix mime type from watermark --- lib/php/core-bundle/Util/UrlUtil.php | 5 ++++ .../Context/ReadOnlyTransformationContext.php | 2 +- .../src/Context/TransformationContext.php | 30 ++++++++++++------- .../TransformationContextInterface.php | 2 +- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/php/core-bundle/Util/UrlUtil.php b/lib/php/core-bundle/Util/UrlUtil.php index 194bf5c85..18f92e5d3 100644 --- a/lib/php/core-bundle/Util/UrlUtil.php +++ b/lib/php/core-bundle/Util/UrlUtil.php @@ -24,6 +24,11 @@ public static function removeQueryParamFromUrl(string $url, string $key): string return $baseUrl.'?'.$query; } + public static function getUriWithoutQuery(string $url): string + { + return strtok($url, '?'); + } + public static function extractUrlParameters(string $url): ?array { $parts = parse_url($url); diff --git a/lib/php/rendition-factory/src/Context/ReadOnlyTransformationContext.php b/lib/php/rendition-factory/src/Context/ReadOnlyTransformationContext.php index f2efc96bc..73cb6bf74 100644 --- a/lib/php/rendition-factory/src/Context/ReadOnlyTransformationContext.php +++ b/lib/php/rendition-factory/src/Context/ReadOnlyTransformationContext.php @@ -25,7 +25,7 @@ public function getCacheDir(string $folder): string throw new \InvalidArgumentException('Cannot get cache directory in read-only context'); } - public function guessMimeTypeFromPath(string $path): string + public function guessMimeTypeFromPath(string $path): ?string { return $this->mimeTypeGuesser->guessMimeTypeFromPath($path); } diff --git a/lib/php/rendition-factory/src/Context/TransformationContext.php b/lib/php/rendition-factory/src/Context/TransformationContext.php index 6f2447329..97af1eb42 100644 --- a/lib/php/rendition-factory/src/Context/TransformationContext.php +++ b/lib/php/rendition-factory/src/Context/TransformationContext.php @@ -2,6 +2,7 @@ namespace Alchemy\RenditionFactory\Context; +use Alchemy\CoreBundle\Util\UrlUtil; use Alchemy\RenditionFactory\DTO\Metadata\MetadataContainerInterface; use Alchemy\RenditionFactory\MimeType\MimeTypeGuesser; use Psr\Log\LoggerInterface; @@ -46,14 +47,9 @@ public function getCacheDir(string $folder): string return $cacheDir; } - public function guessMimeTypeFromPath(string $path): string + public function guessMimeTypeFromPath(string $path): ?string { - $mimeType = $this->mimeTypeGuesser->guessMimeTypeFromPath($path); - if (empty($mimeType)) { - throw new \RuntimeException(sprintf('Could not guess mime type for file "%s"', $path)); - } - - return $mimeType; + return $this->mimeTypeGuesser->guessMimeTypeFromPath($path); } public function getExtension(string $mimeType): ?string @@ -64,18 +60,23 @@ public function getExtension(string $mimeType): ?string public function getRemoteFile(string $uri): string { $cacheDir = $this->getCacheDir('remote'); - $mimeType = $this->guessMimeTypeFromPath($uri); - $extension = $this->getExtension($mimeType); + $mimeType = $this->guessMimeTypeFromPath(UrlUtil::getUriWithoutQuery($uri)); + $extension = null !== $mimeType ? $this->getExtension($mimeType) : null; $path = $cacheDir.'/'.md5($uri).($extension ? '.'.$extension : ''); if (!file_exists($path)) { - $this->download($uri, $path); + $contentType = $this->download($uri, $path); + if (null === $mimeType && null !== $contentType) { + $newPath = $path.'.'.$this->getExtension($contentType); + rename($path, $newPath); + $path = $newPath; + } } return $path; } - private function download(string $uri, string $dest): void + private function download(string $uri, string $dest): ?string { $response = $this->client->request('GET', $uri); @@ -84,6 +85,13 @@ private function download(string $uri, string $dest): void fwrite($fileHandler, $chunk->getContent()); } fclose($fileHandler); + + $contentType = $response->getHeaders()['content-type'] ?? null; + if (empty($contentType)) { + return null; + } + + return is_array($contentType) ? $contentType[0] : $contentType; } public function getMetadata(string $name): ?string diff --git a/lib/php/rendition-factory/src/Context/TransformationContextInterface.php b/lib/php/rendition-factory/src/Context/TransformationContextInterface.php index e568b00e1..88278d409 100644 --- a/lib/php/rendition-factory/src/Context/TransformationContextInterface.php +++ b/lib/php/rendition-factory/src/Context/TransformationContextInterface.php @@ -8,7 +8,7 @@ public function createTmpFilePath(?string $extension): string; public function getCacheDir(string $folder): string; - public function guessMimeTypeFromPath(string $path): string; + public function guessMimeTypeFromPath(string $path): ?string; public function getExtension(string $mimeType): ?string;