[BUGFIX] ImageViewHelper should catch exceptions 96/44796/3
authorAndreas Allacher <andreas@allacher.com>
Thu, 19 Nov 2015 13:27:33 +0000 (14:27 +0100)
committerBenni Mack <benni@typo3.org>
Fri, 4 Dec 2015 08:07:52 +0000 (09:07 +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/44796
Reviewed-by: Michael Oehlhof <typo3@oehlhof.de>
Tested-by: Michael Oehlhof <typo3@oehlhof.de>
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/fluid/Classes/ViewHelpers/ImageViewHelper.php

index 73c5720..6f8e365 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\Core\Resource\FileReference;
 use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
@@ -106,35 +107,40 @@ 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);
-        if ($crop === null) {
-            $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
-        }
-        $processingInstructions = array(
-            'width' => $width,
-            'height' => $height,
-            'minWidth' => $minWidth,
-            'minHeight' => $minHeight,
-            'maxWidth' => $maxWidth,
-            'maxHeight' => $maxHeight,
-            'crop' => $crop,
-        );
-        $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
-        $imageUri = $this->imageService->getImageUri($processedImage, $absolute);
 
-        $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);
+            if ($crop === null) {
+                $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
+            }
+            $processingInstructions = array(
+                'width' => $width,
+                'height' => $height,
+                'minWidth' => $minWidth,
+                'minHeight' => $minHeight,
+                'maxWidth' => $maxWidth,
+                'maxHeight' => $maxHeight,
+                'crop' => $crop,
+            );
+            $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
+            $imageUri = $this->imageService->getImageUri($processedImage, $absolute);
 
-        $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();