Make the AbstractAttachmentImporter calculate file metadata itself
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 28 Jun 2019 14:09:05 +0000 (16:09 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 28 Jun 2019 14:12:30 +0000 (16:12 +0200)
wcfsetup/install/files/lib/system/importer/AbstractAttachmentImporter.class.php

index eaf115d732e8d043759c5a260a9f9ad1a090554f..c8370d3973ac8fc9a52d2186ebde4ea2d2025a8b 100644 (file)
@@ -3,11 +3,12 @@ namespace wcf\system\importer;
 use wcf\data\attachment\Attachment;
 use wcf\data\attachment\AttachmentEditor;
 use wcf\system\exception\SystemException;
+use wcf\util\FileUtil;
 
 /**
  * Imports attachments.
  * 
- * @author     Marcel Werk
+ * @author     Tim Duesterhus, Marcel Werk
  * @copyright  2001-2019 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    WoltLabSuite\Core\System\Importer
@@ -29,18 +30,25 @@ class AbstractAttachmentImporter extends AbstractImporter {
         */
        public function import($oldID, array $data, array $additionalData = []) {
                // check file location
-               if (!@file_exists($additionalData['fileLocation'])) return 0;
+               if (!is_readable($additionalData['fileLocation'])) return 0;
                
-               // get file hash
-               if (empty($data['fileHash'])) $data['fileHash'] = sha1_file($additionalData['fileLocation']);
+               // Extract metadata from the file ourselves, because the
+               // information pulled from the source database might not
+               // be reliable.
+               $data['fileHash'] = sha1_file($additionalData['fileLocation']);
+               $data['filesize'] = filesize($additionalData['fileLocation']);
+               $data['fileType'] = FileUtil::getMimeType($additionalData['fileLocation']);
                
-               // get image size
-               if (!empty($data['isImage'])) {
-                       $imageData = @getimagesize($additionalData['fileLocation']);
-                       if ($imageData !== false) {
-                               $data['width'] = $imageData[0];
-                               $data['height'] = $imageData[1];
-                       }
+               $imageData = @getimagesize($additionalData['fileLocation']);
+               if ($imageData !== false) {
+                       $data['isImage'] = 1;
+                       $data['width'] = $imageData[0];
+                       $data['height'] = $imageData[1];
+               }
+               else {
+                       $data['isImage'] = 0;
+                       $data['width'] = 0;
+                       $data['height'] = 0;
                }
                
                // get user id