Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable12] Don't lie about preview types #7745

Merged
merged 2 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,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 @@ -165,7 +169,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 @@ -193,14 +205,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 @@ -332,7 +347,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 @@ -348,12 +363,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 @@ -373,4 +389,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 @@ -317,6 +317,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