diff --git a/lib/private/Preview/Generator.php b/lib/private/Preview/Generator.php
index 402732ecda9927a7b9b591682939a676480e02da..91173d41afa12ecb9d23b5447457070b11f022d1 100644
--- a/lib/private/Preview/Generator.php
+++ b/lib/private/Preview/Generator.php
@@ -128,7 +128,7 @@ class Generator {
 
 		// Try to get a cached preview. Else generate (and store) one
 		try {
-			$file = $this->getCachedPreview($previewFolder, $width, $height, $crop);
+			$file = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType());
 		} catch (NotFoundException $e) {
 			$file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight);
 		}
@@ -173,7 +173,8 @@ class Generator {
 					continue;
 				}
 
-				$path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.png';
+				$ext = $this->getExtention($preview->dataMimeType());
+				$path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext;
 				try {
 					$file = $previewFolder->newFile($path);
 					$file->putContent($preview->data());
@@ -201,14 +202,17 @@ class Generator {
 	 * @param int $width
 	 * @param int $height
 	 * @param bool $crop
+	 * @param string $mimeType
 	 * @return string
 	 */
-	private function generatePath($width, $height, $crop) {
+	private function generatePath($width, $height, $crop, $mimeType) {
 		$path = (string)$width . '-' . (string)$height;
 		if ($crop) {
 			$path .= '-crop';
 		}
-		$path .= '.png';
+
+		$ext = $this->getExtention($mimeType);
+		$path .= '.' . $ext;
 		return $path;
 	}
 
@@ -340,7 +344,7 @@ class Generator {
 		}
 
 
-		$path = $this->generatePath($width, $height, $crop);
+		$path = $this->generatePath($width, $height, $crop, $preview->dataMimeType());
 		try {
 			$file = $previewFolder->newFile($path);
 			$file->putContent($preview->data());
@@ -356,12 +360,13 @@ class Generator {
 	 * @param int $width
 	 * @param int $height
 	 * @param bool $crop
+	 * @param string $mimeType
 	 * @return ISimpleFile
 	 *
 	 * @throws NotFoundException
 	 */
-	private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop) {
-		$path = $this->generatePath($width, $height, $crop);
+	private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType) {
+		$path = $this->generatePath($width, $height, $crop, $mimeType);
 
 		return $previewFolder->getFile($path);
 	}
@@ -381,4 +386,21 @@ class Generator {
 
 		return $folder;
 	}
+
+	/**
+	 * @param string $mimeType
+	 * @return null|string
+	 */
+	private function getExtention($mimeType) {
+		switch ($mimeType) {
+			case 'image/png':
+				return 'png';
+			case 'image/jpeg':
+				return 'jpg';
+			case 'image/gif':
+				return 'gif';
+			default:
+				return null;
+		}
+	}
 }
diff --git a/lib/private/legacy/image.php b/lib/private/legacy/image.php
index a7d702ac032d42aa4d89615ec72cecd31668c1e2..925548ef26da3027edf1f90b4aee4546916ea63d 100644
--- a/lib/private/legacy/image.php
+++ b/lib/private/legacy/image.php
@@ -304,6 +304,24 @@ class OC_Image implements \OCP\IImage {
 		return $this->resource;
 	}
 
+	/**
+	 * @return null|string Returns the mimetype of the data
+	 */
+	public function dataMimeType() {
+		if (!$this->valid()) {
+			return null;
+		}
+
+		switch ($this->mimeType) {
+			case 'image/png':
+			case 'image/jpeg':
+			case 'image/gif':
+				return $this->mimeType;
+			default:
+				return 'image/png';
+		}
+	}
+
 	/**
 	 * @return null|string Returns the raw image data.
 	 */
diff --git a/lib/public/IImage.php b/lib/public/IImage.php
index f63a1b8ca608db1ddc3b791c393a2b4f22669677..70e8b3cff751ee9ea14fcadf6c6118eff8e9f0e1 100644
--- a/lib/public/IImage.php
+++ b/lib/public/IImage.php
@@ -102,6 +102,12 @@ interface IImage {
 	 */
 	public function resource();
 
+	/**
+	 * @return string Returns the raw data mimetype
+	 * @since 13.0.0
+	 */
+	public function dataMimeType();
+
 	/**
 	 * @return string Returns the raw image data.
 	 * @since 8.1.0
diff --git a/tests/lib/Preview/GeneratorTest.php b/tests/lib/Preview/GeneratorTest.php
index f1383b0691b4e583a449d01b9f705d094b0cfbba..130cccdf09e2f9b5c1e1b8b93511f9d9646ae65f 100644
--- a/tests/lib/Preview/GeneratorTest.php
+++ b/tests/lib/Preview/GeneratorTest.php
@@ -93,6 +93,8 @@ class GeneratorTest extends \Test\TestCase {
 		$maxPreview = $this->createMock(ISimpleFile::class);
 		$maxPreview->method('getName')
 			->willReturn('1000-1000-max.png');
+		$maxPreview->method('getMimeType')
+			->willReturn('image/png');
 
 		$previewFolder->method('getDirectoryListing')
 			->willReturn([$maxPreview]);
@@ -170,6 +172,7 @@ class GeneratorTest extends \Test\TestCase {
 		$image->method('width')->willReturn(2048);
 		$image->method('height')->willReturn(2048);
 		$image->method('valid')->willReturn(true);
+		$image->method('dataMimeType')->willReturn('image/png');
 
 		$this->helper->method('getThumbnail')
 			->will($this->returnCallback(function ($provider, $file, $x, $y) use ($invalidProvider, $validProvider, $image) {
@@ -185,6 +188,7 @@ class GeneratorTest extends \Test\TestCase {
 
 		$maxPreview = $this->createMock(ISimpleFile::class);
 		$maxPreview->method('getName')->willReturn('2048-2048-max.png');
+		$maxPreview->method('getMimeType')->willReturn('image/png');
 
 		$previewFile = $this->createMock(ISimpleFile::class);
 
@@ -219,6 +223,7 @@ class GeneratorTest extends \Test\TestCase {
 		$image->method('data')
 			->willReturn('my resized data');
 		$image->method('valid')->willReturn(true);
+		$image->method('dataMimeType')->willReturn('image/png');
 
 		$previewFile->expects($this->once())
 			->method('putContent')
@@ -362,6 +367,8 @@ class GeneratorTest extends \Test\TestCase {
 		$maxPreview = $this->createMock(ISimpleFile::class);
 		$maxPreview->method('getName')
 			->willReturn($maxX . '-' . $maxY . '-max.png');
+		$maxPreview->method('getMimeType')
+			->willReturn('image/png');
 
 		$previewFolder->method('getDirectoryListing')
 			->willReturn([$maxPreview]);
@@ -382,6 +389,7 @@ class GeneratorTest extends \Test\TestCase {
 		$image->method('height')->willReturn($maxY);
 		$image->method('width')->willReturn($maxX);
 		$image->method('valid')->willReturn(true);
+		$image->method('dataMimeType')->willReturn('image/png');
 
 		$preview = $this->createMock(ISimpleFile::class);
 		$previewFolder->method('newFile')