Skip to content

Commit

Permalink
fix mime type from watermark
Browse files Browse the repository at this point in the history
  • Loading branch information
4rthem committed Nov 14, 2024
1 parent 1861a0a commit 1752cd0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
5 changes: 5 additions & 0 deletions lib/php/core-bundle/Util/UrlUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
30 changes: 19 additions & 11 deletions lib/php/rendition-factory/src/Context/TransformationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 1752cd0

Please sign in to comment.