From ebc1e8b5c318c22bccfba244e5f61ed7d8e660ae Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 16 Feb 2024 18:46:12 +0000 Subject: [PATCH] Handle unhandled mime types --- src/Model/Photo.php | 2 +- src/Module/Photo.php | 21 +++++++++++++-------- src/Object/Image.php | 3 +++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Model/Photo.php b/src/Model/Photo.php index ba03ea6533..360ee16220 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -621,7 +621,7 @@ class Photo Logger::info('Avatar exceeds image limit', ['uid' => $uid, 'cid' => $cid, 'maximagesize' => $maximagesize, 'size' => $filesize, 'type' => $image->getType()]); if ($image->getImageType() == IMAGETYPE_GIF) { $image->toStatic(); - $image = new Image($image->asString(), 'image/png'); + $image = new Image($image->asString(), image_type_to_mime_type(IMAGETYPE_PNG)); $filesize = strlen($image->asString()); Logger::info('Converted gif to a static png', ['uid' => $uid, 'cid' => $cid, 'size' => $filesize, 'type' => $image->getType()]); diff --git a/src/Module/Photo.php b/src/Module/Photo.php index 9cfe5b6ef7..782b982daa 100644 --- a/src/Module/Photo.php +++ b/src/Module/Photo.php @@ -167,14 +167,16 @@ class Photo extends BaseApi $stamp = microtime(true); if (empty($request['blur']) || empty($photo['blurhash'])) { - $imgdata = MPhoto::getImageDataForPhoto($photo); + $imgdata = MPhoto::getImageDataForPhoto($photo); + $mimetype = $photo['type']; } if (empty($imgdata) && empty($photo['blurhash'])) { throw new HTTPException\NotFoundException(); } elseif (empty($imgdata) && !empty($photo['blurhash'])) { - $image = New Image('', 'image/png'); + $image = New Image('', image_type_to_mime_type(IMAGETYPE_WEBP)); $image->getFromBlurHash($photo['blurhash'], $photo['width'], $photo['height']); - $imgdata = $image->asString(); + $imgdata = $image->asString(); + $mimetype = $image->getType(); } // The mimetype for an external or system resource can only be known reliably after it had been fetched @@ -201,18 +203,21 @@ class Photo extends BaseApi if (!empty($request['static'])) { $img = new Image($imgdata, $photo['type'], $photo['filename']); $img->toStatic(); - $imgdata = $img->asString(); + $imgdata = $img->asString(); + $mimetype = $img->getType(); } // if customsize is set and image is not a gif, resize it if ($photo['type'] !== 'image/gif' && $customsize > 0 && $customsize <= Proxy::PIXEL_THUMB && $square_resize) { $img = new Image($imgdata, $photo['type'], $photo['filename']); $img->scaleToSquare($customsize); - $imgdata = $img->asString(); + $imgdata = $img->asString(); + $mimetype = $img->getType(); } elseif ($photo['type'] !== 'image/gif' && $customsize > 0) { $img = new Image($imgdata, $photo['type'], $photo['filename']); $img->scaleDown($customsize); - $imgdata = $img->asString(); + $imgdata = $img->asString(); + $mimetype = $img->getType(); } if (function_exists('header_remove')) { @@ -220,7 +225,7 @@ class Photo extends BaseApi header_remove('pragma'); } - header('Content-type: ' . $photo['type']); + header('Content-type: ' . $mimetype); $stamp = microtime(true); if (!$cacheable) { @@ -391,7 +396,7 @@ class Photo extends BaseApi } } if (empty($mimetext) && !empty($contact['blurhash'])) { - $image = New Image('', 'image/png'); + $image = New Image('', image_type_to_mime_type(IMAGETYPE_WEBP)); $image->getFromBlurHash($contact['blurhash'], $customsize, $customsize); return MPhoto::createPhotoForImageData($image->asString()); } elseif (empty($mimetext)) { diff --git a/src/Object/Image.php b/src/Object/Image.php index dd55f331f7..76e7c7fe55 100644 --- a/src/Object/Image.php +++ b/src/Object/Image.php @@ -66,6 +66,9 @@ class Image if (Images::isSupportedMimeType($type)) { $this->imageType = Images::getImageTypeByMimeType($type); + } elseif (($type == '') || substr($type, 0, 6) != 'image/' || substr($type, 0, 12) != ' application/') { + $this->imageType = IMAGETYPE_WEBP; + DI::logger()->debug('Unhandled image mime type, use WebP instead', ['type' => $type, 'filename' => $filename, 'size' => strlen($data)]); } else { DI::logger()->debug('Unhandled mime type', ['type' => $type, 'filename' => $filename, 'size' => strlen($data)]); $this->valid = false;