Catch exceptions thrown when processing attachment files
authorMatthias Schmidt <gravatronics@live.com>
Tue, 31 May 2016 18:56:48 +0000 (20:56 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Tue, 31 May 2016 19:04:43 +0000 (21:04 +0200)
wcfsetup/install/files/lib/data/attachment/AttachmentAction.class.php

index 7e0c53160ed314d8798d7572afa3122409be93b7..82cbf571a278ce2bec2202a14e440e464212da69 100644 (file)
@@ -6,6 +6,7 @@ use wcf\system\attachment\AttachmentHandler;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\event\EventHandler;
 use wcf\system\exception\PermissionDeniedException;
+use wcf\system\exception\SystemException;
 use wcf\system\exception\UserInputException;
 use wcf\system\image\ImageHandler;
 use wcf\system\request\LinkHandler;
@@ -169,70 +170,80 @@ class AttachmentAction extends AbstractDatabaseObjectAction {
                        
                        // move uploaded file
                        if (@move_uploaded_file($file->getLocation(), $attachment->getLocation())) {
-                               if ($attachment->isImage) {
-                                       $thumbnails[] = $attachment;
-                                       
-                                       // rotate image based on the exif data
-                                       $neededMemory = $attachment->width * $attachment->height * ($attachment->fileType == 'image/png' ? 4 : 3) * 2.1;
-                                       if (FileUtil::getMemoryLimit() == -1 || FileUtil::getMemoryLimit() > (memory_get_usage() + $neededMemory)) {
-                                               $exifData = ExifUtil::getExifData($attachment->getLocation());
-                                               if (!empty($exifData)) {
-                                                       $orientation = ExifUtil::getOrientation($exifData);
-                                                       if ($orientation != ExifUtil::ORIENTATION_ORIGINAL) {
-                                                               $adapter = ImageHandler::getInstance()->getAdapter();
-                                                               $adapter->loadFile($attachment->getLocation());
-                                                               
-                                                               $newImage = null;
-                                                               switch ($orientation) {
-                                                                       case ExifUtil::ORIENTATION_180_ROTATE:
-                                                                               $newImage = $adapter->rotate(180);
-                                                                       break;
+                               try {
+                                       if ($attachment->isImage) {
+                                               // rotate image based on the exif data
+                                               $neededMemory = $attachment->width * $attachment->height * ($attachment->fileType == 'image/png' ? 4 : 3) * 2.1;
+                                               if (FileUtil::getMemoryLimit() == -1 || FileUtil::getMemoryLimit() > (memory_get_usage() + $neededMemory)) {
+                                                       $exifData = ExifUtil::getExifData($attachment->getLocation());
+                                                       if (!empty($exifData)) {
+                                                               $orientation = ExifUtil::getOrientation($exifData);
+                                                               if ($orientation != ExifUtil::ORIENTATION_ORIGINAL) {
+                                                                       $adapter = ImageHandler::getInstance()->getAdapter();
+                                                                       $adapter->loadFile($attachment->getLocation());
                                                                        
-                                                                       case ExifUtil::ORIENTATION_90_ROTATE:
-                                                                               $newImage = $adapter->rotate(90);
-                                                                       break;
+                                                                       $newImage = null;
+                                                                       switch ($orientation) {
+                                                                               case ExifUtil::ORIENTATION_180_ROTATE:
+                                                                                       $newImage = $adapter->rotate(180);
+                                                                               break;
+                                                                               
+                                                                               case ExifUtil::ORIENTATION_90_ROTATE:
+                                                                                       $newImage = $adapter->rotate(90);
+                                                                               break;
+                                                                               
+                                                                               case ExifUtil::ORIENTATION_270_ROTATE:
+                                                                                       $newImage = $adapter->rotate(270);
+                                                                               break;
+                                                                               
+                                                                               case ExifUtil::ORIENTATION_HORIZONTAL_FLIP:
+                                                                               case ExifUtil::ORIENTATION_VERTICAL_FLIP:
+                                                                               case ExifUtil::ORIENTATION_VERTICAL_FLIP_270_ROTATE:
+                                                                               case ExifUtil::ORIENTATION_HORIZONTAL_FLIP_270_ROTATE:
+                                                                                       // unsupported
+                                                                               break;
+                                                                       }
                                                                        
-                                                                       case ExifUtil::ORIENTATION_270_ROTATE:
-                                                                               $newImage = $adapter->rotate(270);
-                                                                       break;
+                                                                       if ($newImage !== null) {
+                                                                               $adapter->load($newImage, $adapter->getType());
+                                                                       }
                                                                        
-                                                                       case ExifUtil::ORIENTATION_HORIZONTAL_FLIP:
-                                                                       case ExifUtil::ORIENTATION_VERTICAL_FLIP:
-                                                                       case ExifUtil::ORIENTATION_VERTICAL_FLIP_270_ROTATE:
-                                                                       case ExifUtil::ORIENTATION_HORIZONTAL_FLIP_270_ROTATE:
-                                                                               // unsupported
-                                                                       break;
-                                                               }
-                                                               
-                                                               if ($newImage !== null) {
-                                                                       $adapter->load($newImage, $adapter->getType());
-                                                               }
-                                                               
-                                                               $adapter->writeImage($attachment->getLocation());
-                                                               
-                                                               if ($newImage !== null) {
-                                                                       // update width, height and filesize of the attachment
-                                                                       if ($orientation == ExifUtil::ORIENTATION_90_ROTATE || $orientation == ExifUtil::ORIENTATION_270_ROTATE) {
-                                                                               $attachmentEditor = new AttachmentEditor($attachment);
-                                                                               $attachmentEditor->update(array(
-                                                                                       'height' => $attachment->width,
-                                                                                       'width' => $attachment->height,
-                                                                                       'filesize' => filesize($attachment->getLocation())
-                                                                               ));
+                                                                       $adapter->writeImage($attachment->getLocation());
+                                                                       
+                                                                       if ($newImage !== null) {
+                                                                               // update width, height and filesize of the attachment
+                                                                               if ($orientation == ExifUtil::ORIENTATION_90_ROTATE || $orientation == ExifUtil::ORIENTATION_270_ROTATE) {
+                                                                                       $attachmentEditor = new AttachmentEditor($attachment);
+                                                                                       $attachmentEditor->update(array(
+                                                                                               'height' => $attachment->width,
+                                                                                               'width' => $attachment->height,
+                                                                                               'filesize' => filesize($attachment->getLocation())
+                                                                                       ));
+                                                                               }
                                                                        }
                                                                }
                                                        }
                                                }
+                                               
+                                               $thumbnails[] = $attachment;
                                        }
+                                       else {
+                                               // check whether we can create thumbnails for this file
+                                               $this->eventAttachment = $attachment;
+                                               $this->eventData = array('hasThumbnail' => false);
+                                               EventHandler::getInstance()->fireAction($this, 'checkThumbnail');
+                                               if ($this->eventData['hasThumbnail']) $thumbnails[] = $attachment;
+                                       }
+                                       $attachments[$file->getInternalFileID()] = $attachment;
                                }
-                               else {
-                                       // check whether we can create thumbnails for this file
-                                       $this->eventAttachment = $attachment;
-                                       $this->eventData = array('hasThumbnail' => false);
-                                       EventHandler::getInstance()->fireAction($this, 'checkThumbnail');
-                                       if ($this->eventData['hasThumbnail']) $thumbnails[] = $attachment;
+                               catch (SystemException $e) {
+                                       // processing failed; delete attachment
+                                       $editor = new AttachmentEditor($attachment);
+                                       $editor->delete();
+                                       
+                                       // log error
+                                       $e->getExceptionID();
                                }
-                               $attachments[$file->getInternalFileID()] = $attachment;
                        }
                        else {
                                // moving failed; delete attachment