[BUGFIX] Wrong generation of metadata in FAL 54/36454/2
authorFrederic Gaus <gaus@flagbit.de>
Tue, 11 Nov 2014 18:35:36 +0000 (19:35 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Fri, 30 Jan 2015 17:06:43 +0000 (18:06 +0100)
This patch fixes the generation of metadata if the metadata
of a file is missing in sys_file_metadata. Without this patch
the metadata is generated without taking care of images and their
width and height. As a result, images are generated with
width=0 height=0. See bugreport for details.

The change also introduces the basic structure to fetch
general info of files via the SplFileInfo objects API.

Resolves: #62829
Releases: master, 6.2
Change-Id: Ie99fc74f3439cef5dbd3702e57596b37390da679
Reviewed-on: http://review.typo3.org/36454
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/core/Classes/Resource/AbstractFile.php
typo3/sysext/core/Classes/Resource/Index/MetaDataRepository.php
typo3/sysext/core/Classes/Type/File/FileInfo.php [new file with mode: 0644]
typo3/sysext/core/Classes/Type/File/ImageInfo.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Type/File/FileInfoTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Type/File/ImageInfoTest.php [new file with mode: 0644]

index 230d7dc..468c950 100644 (file)
@@ -304,7 +304,7 @@ abstract class AbstractFile implements FileInterface {
                                        $this->properties['type'] = self::FILETYPE_UNKNOWN;
                        }
                }
-               return $this->properties['type'];
+               return (int)$this->properties['type'];
        }
 
        /******************
index 02652b9..20a487c 100644 (file)
@@ -17,6 +17,8 @@ namespace TYPO3\CMS\Core\Resource\Index;
 
 use TYPO3\CMS\Core\Resource\File;
 use TYPO3\CMS\Core\SingletonInterface;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Core\Type\File as FileType;
 
 /**
  * Repository Class as an abstraction layer to sys_file_metadata
@@ -54,7 +56,29 @@ class MetaDataRepository implements SingletonInterface {
         * @return array
         */
        public function findByFile(File $file) {
-               return $this->findByFileUid($file->getUid());
+               $record = $this->findByFileUid($file->getUid());
+
+               // It could be possible that the meta information is freshly
+               // created and inserted into the database. If this is the case
+               // we have to take care about correct meta information for width and
+               // height in case of an image.
+               if (!empty($record['newlyCreated'])) {
+                       if ($file->getType() === File::FILETYPE_IMAGE && $file->getStorage()->getDriverType() === 'Local') {
+                               $fileNameAndPath = $file->getForLocalProcessing(FALSE);
+
+                               $imageInfo = GeneralUtility::makeInstance(FileType\ImageInfo::class, $fileNameAndPath);
+
+                               $additionalMetaInformation = array(
+                                       'width' => $imageInfo->getWidth(),
+                                       'height' => $imageInfo->getHeight(),
+                               );
+
+                               $this->update($file->getUid(), $additionalMetaInformation);
+                       }
+                       $record = $this->findByFileUid($file->getUid());
+               }
+
+               return $record;
        }
 
        /**
@@ -108,6 +132,7 @@ class MetaDataRepository implements SingletonInterface {
                $this->getDatabaseConnection()->exec_INSERTquery($this->tableName, $emptyRecord);
                $record = $emptyRecord;
                $record['uid'] = $this->getDatabaseConnection()->sql_insert_id();
+               $record['newlyCreated']  = TRUE;
 
                $this->emitRecordCreatedSignal($record);
 
diff --git a/typo3/sysext/core/Classes/Type/File/FileInfo.php b/typo3/sysext/core/Classes/Type/File/FileInfo.php
new file mode 100644 (file)
index 0000000..91a5081
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+namespace TYPO3\CMS\Core\Type\File;
+
+/**
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Type\TypeInterface;
+
+/**
+ * A SPL FileInfo class providing general information related to a file.
+ */
+class FileInfo extends \SplFileInfo implements TypeInterface {
+
+       /**
+        * Return the mime type of a file.
+        *
+        * @return string|FALSE
+        */
+       public function getMimeType(){
+
+               // @todo will be implemented in issue #60019
+       }
+
+}
diff --git a/typo3/sysext/core/Classes/Type/File/ImageInfo.php b/typo3/sysext/core/Classes/Type/File/ImageInfo.php
new file mode 100644 (file)
index 0000000..9f0f9c6
--- /dev/null
@@ -0,0 +1,76 @@
+<?php
+namespace TYPO3\CMS\Core\Type\File;
+
+/**
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * A SPL FileInfo class providing information related to an image.
+ */
+class ImageInfo extends FileInfo {
+
+       /**
+        * @var array
+        */
+       protected $imageSizes;
+
+       /**
+        * Returns the width of the Image.
+        *
+        * @return int
+        */
+       public function getWidth() {
+               $imageSizes = $this->getImageSizes();
+               return $imageSizes[0];
+       }
+
+       /**
+        * Returns the height of the Image.
+        *
+        * @return int
+        */
+       public function getHeight() {
+               $imageSizes = $this->getImageSizes();
+               return $imageSizes[1];
+       }
+
+       /**
+        * @return array
+        */
+       protected function getImageSizes() {
+               if (is_null($this->imageSizes)) {
+                       $this->imageSizes = getimagesize($this->getPathname());
+
+                       // In case the image size could not be retrieved, log the incident as a warning.
+                       if ($this->imageSizes === FALSE) {
+                               $this->getLogger()->warning('I could not retrieve the image size for file ' . $this->getPathname());
+                       }
+               }
+               return $this->imageSizes;
+       }
+
+       /**
+        * @return \TYPO3\CMS\Core\Log\Logger
+        */
+       protected function getLogger(){
+
+               /** @var $loggerManager \TYPO3\CMS\Core\Log\LogManager */
+               $loggerManager = GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager');
+
+               /** @var $logger \TYPO3\CMS\Core\Log\Logger */
+               return $loggerManager->getLogger(get_class($this));
+       }
+
+}
diff --git a/typo3/sysext/core/Tests/Unit/Type/File/FileInfoTest.php b/typo3/sysext/core/Tests/Unit/Type/File/FileInfoTest.php
new file mode 100644 (file)
index 0000000..2d67cb6
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Type\File;
+
+/**
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Tests\UnitTestCase;
+
+/**
+ * Test case
+ */
+class FileInfoTest extends UnitTestCase {
+
+       /**
+        * @test
+        */
+       public function classFileInfoCanBeInstantiated() {
+               $className = 'TYPO3\CMS\Core\Type\File\FileInfo';
+               $classInstance = new \TYPO3\CMS\Core\Type\File\FileInfo('FooFileName');
+               $this->assertInstanceOf($className, $classInstance);
+       }
+
+}
diff --git a/typo3/sysext/core/Tests/Unit/Type/File/ImageInfoTest.php b/typo3/sysext/core/Tests/Unit/Type/File/ImageInfoTest.php
new file mode 100644 (file)
index 0000000..e3874e3
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Type\File;
+
+/**
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Tests\UnitTestCase;
+
+/**
+ * Test case
+ */
+class ImageInfoTest extends UnitTestCase {
+
+       /**
+        * @test
+        */
+       public function classImageInfoCanBeInstantiated() {
+               $className = 'TYPO3\CMS\Core\Type\File\ImageInfo';
+               $classInstance = new \TYPO3\CMS\Core\Type\File\ImageInfo('FooFileName');
+               $this->assertInstanceOf($className, $classInstance);
+       }
+
+}