[BUGFIX] ImageViewHelper should catch exceptions 22/45122/4
authorBenni Mack <benni@typo3.org>
Fri, 4 Dec 2015 08:13:39 +0000 (09:13 +0100)
committerMorton Jonuschat <m.jonuschat@mojocode.de>
Fri, 4 Dec 2015 12:37:31 +0000 (13:37 +0100)
The ImageViewHelper should catch the following exceptions:
1.) \UnexpectedValueException:
This can happen if a file has been replaced with a folder.

2.) \TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
If a file was deleted (or renamed directly in the file system).
Such a scenario should still result in a proper page.

Change-Id: I73fa4c279d779a524c0a10021df56529dde052fc
Releases: master, 6.2
Resolves: #71686
Reviewed-on: https://review.typo3.org/45122
Reviewed-by: Andreas Allacher <andreas.allacher@gmx.at>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Allacher <andreas.allacher@gmx.at>
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
typo3/sysext/fluid/Classes/ViewHelpers/ImageViewHelper.php
typo3/sysext/fluid/Classes/ViewHelpers/Uri/ImageViewHelper.php

index d6a222c..e1a1dad 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Fluid\ViewHelpers;
  * Public License for more details.                                       *
  *                                                                        */
 
+use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
 use TYPO3\CMS\Core\Resource\FileInterface;
 use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
 
@@ -100,31 +101,36 @@ class ImageViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedV
                if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
                        throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284106);
                }
-               $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
-               $processingInstructions = array(
-                       'width' => $width,
-                       'height' => $height,
-                       'minWidth' => $minWidth,
-                       'minHeight' => $minHeight,
-                       'maxWidth' => $maxWidth,
-                       'maxHeight' => $maxHeight,
-               );
-               $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
-               $imageUri = $this->imageService->getImageUri($processedImage);
 
-               $this->tag->addAttribute('src', $imageUri);
-               $this->tag->addAttribute('width', $processedImage->getProperty('width'));
-               $this->tag->addAttribute('height', $processedImage->getProperty('height'));
+               try {
+                       $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
+                       $processingInstructions = array(
+                               'width' => $width,
+                               'height' => $height,
+                               'minWidth' => $minWidth,
+                               'minHeight' => $minHeight,
+                               'maxWidth' => $maxWidth,
+                               'maxHeight' => $maxHeight,
+                       );
+                       $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
+                       $imageUri = $this->imageService->getImageUri($processedImage);
 
-               $alt = $image->getProperty('alternative');
-               $title = $image->getProperty('title');
+                       $this->tag->addAttribute('src', $imageUri);
+                       $this->tag->addAttribute('width', $processedImage->getProperty('width'));
+                       $this->tag->addAttribute('height', $processedImage->getProperty('height'));
 
-               // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
-               if (empty($this->arguments['alt'])) {
-                       $this->tag->addAttribute('alt', $alt);
-               }
-               if (empty($this->arguments['title']) && $title) {
-                       $this->tag->addAttribute('title', $title);
+                       $alt = $image->getProperty('alternative');
+                       $title = $image->getProperty('title');
+
+                       // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
+                       if (empty($this->arguments['alt'])) {
+                               $this->tag->addAttribute('alt', $alt);
+                       }
+                       if (empty($this->arguments['title']) && $title) {
+                               $this->tag->addAttribute('title', $title);
+                       }
+               } catch (ResourceDoesNotExistException $e) {
+               } catch (\UnexpectedValueException $e) {
                }
 
                return $this->tag->render();
index e416c53..3a7ebad 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Fluid\ViewHelpers\Uri;
  * Public License for more details.                                       *
  *                                                                        */
 
+use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
 use TYPO3\CMS\Core\Resource\FileInterface;
 use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
 
@@ -82,16 +83,21 @@ class ImageViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelpe
                if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
                        throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284105);
                }
-               $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
-               $processingInstructions = array(
-                       'width' => $width,
-                       'height' => $height,
-                       'minWidth' => $minWidth,
-                       'minHeight' => $minHeight,
-                       'maxWidth' => $maxWidth,
-                       'maxHeight' => $maxHeight,
-               );
-               $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
-               return $this->imageService->getImageUri($processedImage);
+               try {
+                       $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
+                       $processingInstructions = array(
+                               'width' => $width,
+                               'height' => $height,
+                               'minWidth' => $minWidth,
+                               'minHeight' => $minHeight,
+                               'maxWidth' => $maxWidth,
+                               'maxHeight' => $maxHeight,
+                       );
+                       $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
+                       return $this->imageService->getImageUri($processedImage);
+               } catch (ResourceDoesNotExistException $e) {
+               } catch (\UnexpectedValueException $e) {
+               }
+               return '';
        }
 }