Skip to content

Commit

Permalink
Guess mime type when reported wrongly
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Feb 19, 2024
1 parent 3c5f387 commit fcdbeed
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"symfony/framework-bundle": "7.0.*",
"symfony/http-client": "7.0.*",
"symfony/messenger": "7.0.*",
"symfony/mime": "7.0.*",
"symfony/property-access": "7.0.*",
"symfony/property-info": "7.0.*",
"symfony/psr-http-message-bridge": "7.0.*",
Expand Down
169 changes: 168 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion src/Service/ImageFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SapientPro\ImageComparator\ImageComparator;
use Symfony\Component\ErrorHandler\Error\FatalError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mime\MimeTypeGuesserInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Throwable;
Expand All @@ -17,6 +18,7 @@ public function __construct(
private CacheItemPoolInterface $cache,
private HttpClientInterface $httpClient,
private ImageComparator $imageComparator,
private MimeTypeGuesserInterface $typeGuesser,
) {
}

Expand All @@ -33,7 +35,13 @@ public function getImageHash(string $url): ?string
$cacheItem->set(null);
} else {
$contentType = $response->getHeaders(false)['content-type'][0] ?? '';
if (str_starts_with($contentType, 'image/')) {
$isImage = str_starts_with($contentType, 'image/');
if (!$isImage) {
$filepath = tempnam(sys_get_temp_dir(), 'lemmy_automod');
file_put_contents($filepath, $response->getContent());
$isImage = str_starts_with($this->typeGuesser->guessMimeType($filepath), 'image/');
}
if ($isImage) {
try {
if ($this->isAnimatedWebP($response->getContent())) {
$image = false;
Expand Down

0 comments on commit fcdbeed

Please sign in to comment.