Skip to content

Commit

Permalink
Only return 1 type
Browse files Browse the repository at this point in the history
Throw proper exception if we can't get the mimetype for a preview. Catch
it later on so we can just return a not found for the preview.

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
  • Loading branch information
rullzer committed Jan 7, 2018
1 parent 5b21650 commit faa68b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
22 changes: 17 additions & 5 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, $maxPreview->getMimeType());
} 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,14 @@ private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeTy
continue;
}

$ext = $this->getExtention($preview->dataMimeType());
// 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);
Expand Down Expand Up @@ -390,6 +401,7 @@ private function getPreviewFolder(File $file) {
/**
* @param string $mimeType
* @return null|string
* @throws \InvalidArgumentException
*/
private function getExtention($mimeType) {
switch ($mimeType) {
Expand All @@ -400,7 +412,7 @@ private function getExtention($mimeType) {
case 'image/gif':
return 'gif';
default:
return null;
throw new \InvalidArgumentException('Not a valid mimetype');
}
}
}
5 changes: 3 additions & 2 deletions lib/private/legacy/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,12 @@ public function resource() {
}

/**
* @return null|string Returns the mimetype of the data
* @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 null;
return '';
}

switch ($this->mimeType) {
Expand Down

0 comments on commit faa68b2

Please sign in to comment.