Skip to content

Commit

Permalink
Merge pull request #7692 from nextcloud/dont_lie_preview_mimetype
Browse files Browse the repository at this point in the history
Don't lie about the preview mimetype
  • Loading branch information
rullzer authored Jan 8, 2018
2 parents 9c3ac19 + faa68b2 commit 7d87e83
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
52 changes: 43 additions & 9 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,13 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,

// Try to get a cached preview. Else generate (and store) one
try {
$file = $this->getCachedPreview($previewFolder, $width, $height, $crop);
} catch (NotFoundException $e) {
$file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight);
try {
$file = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType());
} catch (NotFoundException $e) {
$file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight);
}
} catch (\InvalidArgumentException $e) {
throw new NotFoundException();
}

return $file;
Expand Down Expand Up @@ -173,7 +177,15 @@ private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeTy
continue;
}

$path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.png';
// Try to get the extention.
try {
$ext = $this->getExtention($preview->dataMimeType());
} catch (\InvalidArgumentException $e) {
// Just continue to the next iteration if this preview doesn't have a valid mimetype
continue;
}

$path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext;
try {
$file = $previewFolder->newFile($path);
$file->putContent($preview->data());
Expand Down Expand Up @@ -201,14 +213,17 @@ private function getPreviewSize(ISimpleFile $file) {
* @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;
}

Expand Down Expand Up @@ -340,7 +355,7 @@ private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxP
}


$path = $this->generatePath($width, $height, $crop);
$path = $this->generatePath($width, $height, $crop, $preview->dataMimeType());
try {
$file = $previewFolder->newFile($path);
$file->putContent($preview->data());
Expand All @@ -356,12 +371,13 @@ private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxP
* @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);
}
Expand All @@ -381,4 +397,22 @@ private function getPreviewFolder(File $file) {

return $folder;
}

/**
* @param string $mimeType
* @return null|string
* @throws \InvalidArgumentException
*/
private function getExtention($mimeType) {
switch ($mimeType) {
case 'image/png':
return 'png';
case 'image/jpeg':
return 'jpg';
case 'image/gif':
return 'gif';
default:
throw new \InvalidArgumentException('Not a valid mimetype');
}
}
}
19 changes: 19 additions & 0 deletions lib/private/legacy/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,25 @@ public function resource() {
return $this->resource;
}

/**
* @return string Returns the mimetype of the data. Returns the empty string
* if the data is not valid.
*/
public function dataMimeType() {
if (!$this->valid()) {
return '';
}

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.
*/
Expand Down
6 changes: 6 additions & 0 deletions lib/public/IImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public function save($filePath = null, $mimeType = null);
*/
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
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/Preview/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public function testGetCachedPreview() {
$maxPreview = $this->createMock(ISimpleFile::class);
$maxPreview->method('getName')
->willReturn('1000-1000-max.png');
$maxPreview->method('getMimeType')
->willReturn('image/png');

$previewFolder->method('getDirectoryListing')
->willReturn([$maxPreview]);
Expand Down Expand Up @@ -170,6 +172,7 @@ public function testGetNewPreview() {
$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) {
Expand All @@ -185,6 +188,7 @@ public function testGetNewPreview() {

$maxPreview = $this->createMock(ISimpleFile::class);
$maxPreview->method('getName')->willReturn('2048-2048-max.png');
$maxPreview->method('getMimeType')->willReturn('image/png');

$previewFile = $this->createMock(ISimpleFile::class);

Expand Down Expand Up @@ -219,6 +223,7 @@ public function testGetNewPreview() {
$image->method('data')
->willReturn('my resized data');
$image->method('valid')->willReturn(true);
$image->method('dataMimeType')->willReturn('image/png');

$previewFile->expects($this->once())
->method('putContent')
Expand Down Expand Up @@ -362,6 +367,8 @@ public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expec
$maxPreview = $this->createMock(ISimpleFile::class);
$maxPreview->method('getName')
->willReturn($maxX . '-' . $maxY . '-max.png');
$maxPreview->method('getMimeType')
->willReturn('image/png');

$previewFolder->method('getDirectoryListing')
->willReturn([$maxPreview]);
Expand All @@ -382,6 +389,7 @@ public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expec
$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')
Expand Down

0 comments on commit 7d87e83

Please sign in to comment.