Don't lie about the preview mimetype

For legacy reasons we stored all the previews with a png extention.
However we did not put png data in them all the time.

This caused the preview endpoints to always report that a preview is a
png file. Which was a lie.

Since we abstract away from the storage etc in the previewmanager. There
is no need anymore to store them as .png files and instead we can use
the actual file extention.

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2018-01-04 10:00:07 +01:00
parent f25e51c369
commit 5b21650097
No known key found for this signature in database
GPG Key ID: F941078878347C0C
4 changed files with 61 additions and 7 deletions

View File

@ -128,7 +128,7 @@ class Generator {
// Try to get a cached preview. Else generate (and store) one // Try to get a cached preview. Else generate (and store) one
try { try {
$file = $this->getCachedPreview($previewFolder, $width, $height, $crop); $file = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType());
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
$file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight); $file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight);
} }
@ -173,7 +173,8 @@ class Generator {
continue; 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 { try {
$file = $previewFolder->newFile($path); $file = $previewFolder->newFile($path);
$file->putContent($preview->data()); $file->putContent($preview->data());
@ -201,14 +202,17 @@ class Generator {
* @param int $width * @param int $width
* @param int $height * @param int $height
* @param bool $crop * @param bool $crop
* @param string $mimeType
* @return string * @return string
*/ */
private function generatePath($width, $height, $crop) { private function generatePath($width, $height, $crop, $mimeType) {
$path = (string)$width . '-' . (string)$height; $path = (string)$width . '-' . (string)$height;
if ($crop) { if ($crop) {
$path .= '-crop'; $path .= '-crop';
} }
$path .= '.png';
$ext = $this->getExtention($mimeType);
$path .= '.' . $ext;
return $path; return $path;
} }
@ -340,7 +344,7 @@ class Generator {
} }
$path = $this->generatePath($width, $height, $crop); $path = $this->generatePath($width, $height, $crop, $preview->dataMimeType());
try { try {
$file = $previewFolder->newFile($path); $file = $previewFolder->newFile($path);
$file->putContent($preview->data()); $file->putContent($preview->data());
@ -356,12 +360,13 @@ class Generator {
* @param int $width * @param int $width
* @param int $height * @param int $height
* @param bool $crop * @param bool $crop
* @param string $mimeType
* @return ISimpleFile * @return ISimpleFile
* *
* @throws NotFoundException * @throws NotFoundException
*/ */
private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop) { private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType) {
$path = $this->generatePath($width, $height, $crop); $path = $this->generatePath($width, $height, $crop, $mimeType);
return $previewFolder->getFile($path); return $previewFolder->getFile($path);
} }
@ -381,4 +386,21 @@ class Generator {
return $folder; 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;
}
}
} }

View File

@ -304,6 +304,24 @@ class OC_Image implements \OCP\IImage {
return $this->resource; 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. * @return null|string Returns the raw image data.
*/ */

View File

@ -102,6 +102,12 @@ interface IImage {
*/ */
public function resource(); public function resource();
/**
* @return string Returns the raw data mimetype
* @since 13.0.0
*/
public function dataMimeType();
/** /**
* @return string Returns the raw image data. * @return string Returns the raw image data.
* @since 8.1.0 * @since 8.1.0

View File

@ -93,6 +93,8 @@ class GeneratorTest extends \Test\TestCase {
$maxPreview = $this->createMock(ISimpleFile::class); $maxPreview = $this->createMock(ISimpleFile::class);
$maxPreview->method('getName') $maxPreview->method('getName')
->willReturn('1000-1000-max.png'); ->willReturn('1000-1000-max.png');
$maxPreview->method('getMimeType')
->willReturn('image/png');
$previewFolder->method('getDirectoryListing') $previewFolder->method('getDirectoryListing')
->willReturn([$maxPreview]); ->willReturn([$maxPreview]);
@ -170,6 +172,7 @@ class GeneratorTest extends \Test\TestCase {
$image->method('width')->willReturn(2048); $image->method('width')->willReturn(2048);
$image->method('height')->willReturn(2048); $image->method('height')->willReturn(2048);
$image->method('valid')->willReturn(true); $image->method('valid')->willReturn(true);
$image->method('dataMimeType')->willReturn('image/png');
$this->helper->method('getThumbnail') $this->helper->method('getThumbnail')
->will($this->returnCallback(function ($provider, $file, $x, $y) use ($invalidProvider, $validProvider, $image) { ->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 = $this->createMock(ISimpleFile::class);
$maxPreview->method('getName')->willReturn('2048-2048-max.png'); $maxPreview->method('getName')->willReturn('2048-2048-max.png');
$maxPreview->method('getMimeType')->willReturn('image/png');
$previewFile = $this->createMock(ISimpleFile::class); $previewFile = $this->createMock(ISimpleFile::class);
@ -219,6 +223,7 @@ class GeneratorTest extends \Test\TestCase {
$image->method('data') $image->method('data')
->willReturn('my resized data'); ->willReturn('my resized data');
$image->method('valid')->willReturn(true); $image->method('valid')->willReturn(true);
$image->method('dataMimeType')->willReturn('image/png');
$previewFile->expects($this->once()) $previewFile->expects($this->once())
->method('putContent') ->method('putContent')
@ -362,6 +367,8 @@ class GeneratorTest extends \Test\TestCase {
$maxPreview = $this->createMock(ISimpleFile::class); $maxPreview = $this->createMock(ISimpleFile::class);
$maxPreview->method('getName') $maxPreview->method('getName')
->willReturn($maxX . '-' . $maxY . '-max.png'); ->willReturn($maxX . '-' . $maxY . '-max.png');
$maxPreview->method('getMimeType')
->willReturn('image/png');
$previewFolder->method('getDirectoryListing') $previewFolder->method('getDirectoryListing')
->willReturn([$maxPreview]); ->willReturn([$maxPreview]);
@ -382,6 +389,7 @@ class GeneratorTest extends \Test\TestCase {
$image->method('height')->willReturn($maxY); $image->method('height')->willReturn($maxY);
$image->method('width')->willReturn($maxX); $image->method('width')->willReturn($maxX);
$image->method('valid')->willReturn(true); $image->method('valid')->willReturn(true);
$image->method('dataMimeType')->willReturn('image/png');
$preview = $this->createMock(ISimpleFile::class); $preview = $this->createMock(ISimpleFile::class);
$previewFolder->method('newFile') $previewFolder->method('newFile')