[TASK] Check and use current file hash and size on export 61/30061/3
authorMarc Bastian Heinrichs <typo3@mbh-software.de>
Tue, 13 May 2014 14:26:11 +0000 (16:26 +0200)
committerWouter Wolters <typo3@wouterwolters.nl>
Tue, 3 Jun 2014 21:14:36 +0000 (23:14 +0200)
On creating exports the file hash and size is used from the
sys_file table. Since this could be not up-to-date in real live
projects, those files fail to be imported.

To prevent this, use the correct data for the export and show
an error to the user.

Resolves: #58761
Releases: 6.2
Change-Id: I7aacc6f00c52bc01c753af4ee32202a9219c02a1
Reviewed-on: https://review.typo3.org/30061
Reviewed-by: Bernd Niehues
Tested-by: Bernd Niehues
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
typo3/sysext/impexp/Classes/ImportExport.php
typo3/sysext/impexp/Tests/Functional/Export/PagesAndTtContentWithImages/ExportTest.php
typo3/sysext/impexp/Tests/Functional/Fixtures/Database/sys_file_corrupt.xml [new file with mode: 0644]

index c3a58c7..cecea21 100644 (file)
@@ -966,8 +966,22 @@ class ImportExport {
                        $this->error('File ' . $file->getPublicUrl() . ': ' . $e->getMessage());
                        return;
                }
+               $fileUid = $file->getUid();
+               $fileInfo = $file->getStorage()->getFileInfo($file);
+               // we sadly have to cast it to string here, because the size property is also returning a string
+               $fileSize = (string)$fileInfo['size'];
+               if ($fileSize !== $file->getProperty('size')) {
+                       $this->error('File size of ' . $file->getCombinedIdentifier() . ' is not up-to-date in index! File added with current size.');
+                       $this->dat['records']['sys_file:' . $fileUid]['data']['size'] = $fileSize;
+               }
+               $fileSha1 = $file->getStorage()->hashFile($file, 'sha1');
+               if ($fileSha1 !== $file->getProperty('sha1')) {
+                       $this->error('File sha1 hash of ' . $file->getCombinedIdentifier() . ' is not up-to-date in index! File added on current sha1.');
+                       $this->dat['records']['sys_file:' . $fileUid]['data']['sha1'] = $fileSha1;
+               }
+
                $fileRec = array();
-               $fileRec['filesize'] = $file->getProperty('size');
+               $fileRec['filesize'] = $fileSize;
                $fileRec['filename'] = $file->getProperty('name');
                $fileRec['filemtime'] = $file->getProperty('modification_date');
 
@@ -979,7 +993,7 @@ class ImportExport {
 
                // ... and finally add the heavy stuff:
                $fileRec['content'] = $fileContent;
-               $fileRec['content_sha1'] = $file->getProperty('sha1');
+               $fileRec['content_sha1'] = $fileSha1;
 
                $this->dat['files_fal'][$fileId] = $fileRec;
        }
index 21a59c5..3bc722f 100644 (file)
@@ -43,7 +43,6 @@ class ExportTest extends \TYPO3\CMS\Impexp\Tests\Functional\Export\AbstractExpor
                $this->importDataSet(__DIR__ . '/../../Fixtures/Database/pages.xml');
                $this->importDataSet(__DIR__ . '/../../Fixtures/Database/tt_content-with-image.xml');
                $this->importDataSet(__DIR__ . '/../../Fixtures/Database/sys_language.xml');
-               $this->importDataSet(__DIR__ . '/../../Fixtures/Database/sys_file.xml');
                $this->importDataSet(__DIR__ . '/../../Fixtures/Database/sys_file_metadata.xml');
                $this->importDataSet(__DIR__ . '/../../Fixtures/Database/sys_file_reference.xml');
                $this->importDataSet(__DIR__ . '/../../Fixtures/Database/sys_file_storage.xml');
@@ -55,6 +54,41 @@ class ExportTest extends \TYPO3\CMS\Impexp\Tests\Functional\Export\AbstractExpor
         */
        public function exportPagesAndRelatedTtContentWithImages() {
 
+               $this->importDataSet(__DIR__ . '/../../Fixtures/Database/sys_file.xml');
+
+               $this->compileExportPagesAndRelatedTtContentWithImages();
+
+               $out = $this->export->compileMemoryToFileContent('xml');
+
+               $errors = $this->export->printErrorLog();
+               $this->assertSame('', $errors);
+
+               $this->assertXmlStringEqualsXmlFile(__DIR__ . '/../../Fixtures/ImportExportXml/pages-and-ttcontent-with-image.xml', $out);
+       }
+
+       /**
+        * @test
+        */
+       public function exportPagesAndRelatedTtContentWithImagesFromCorruptSysFileRecord() {
+
+               $this->importDataSet(__DIR__ . '/../../Fixtures/Database/sys_file_corrupt.xml');
+
+               $this->compileExportPagesAndRelatedTtContentWithImages();
+
+               $out = $this->export->compileMemoryToFileContent('xml');
+
+               $expectedErrors = array(
+                       'File size of 1:/user_upload/typo3_image2.jpg is not up-to-date in index! File added with current size.',
+                       'File sha1 hash of 1:/user_upload/typo3_image2.jpg is not up-to-date in index! File added on current sha1.'
+               );
+               $errors = $this->export->errorLog;
+               $this->assertSame($expectedErrors, $errors);
+
+               $this->assertXmlStringEqualsXmlFile(__DIR__ . '/../../Fixtures/ImportExportXml/pages-and-ttcontent-with-image.xml', $out);
+       }
+
+       protected function compileExportPagesAndRelatedTtContentWithImages() {
+
                $this->export->setRecordTypesIncludeFields(
                        array(
                                'pages' => array(
@@ -155,10 +189,6 @@ class ExportTest extends \TYPO3\CMS\Impexp\Tests\Functional\Export\AbstractExpor
 
                $this->export->export_addFilesFromRelations();
                $this->export->export_addFilesFromSysFilesRecords();
-
-               $out = $this->export->compileMemoryToFileContent('xml');
-
-               $this->assertXmlStringEqualsXmlFile(__DIR__ . '/../../Fixtures/ImportExportXml/pages-and-ttcontent-with-image.xml', $out);
        }
 
 }
\ No newline at end of file
diff --git a/typo3/sysext/impexp/Tests/Functional/Fixtures/Database/sys_file_corrupt.xml b/typo3/sysext/impexp/Tests/Functional/Fixtures/Database/sys_file_corrupt.xml
new file mode 100644 (file)
index 0000000..7a9b0d0
--- /dev/null
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<dataset>
+       <sys_file>
+               <uid>1</uid>
+               <pid>0</pid>
+               <storage>1</storage>
+               <type>2</type>
+               <identifier>/user_upload/typo3_image2.jpg</identifier>
+               <identifier_hash>f90bb9a35622f35b5279195e324eddbaec8164b2</identifier_hash>
+               <folder_hash>19669f1e02c2f16705ec7587044c66443be70725</folder_hash>
+               <extension>jpg</extension>
+               <mime_type>image/jpeg</mime_type>
+               <name>typo3_image2.jpg</name>
+               <sha1>Xa9acdf1e105784a57bbffec9520969578287797</sha1>
+               <size>1958</size>
+               <creation_date>1389878273</creation_date>
+               <modification_date>1389878273</modification_date>
+       </sys_file>
+</dataset>
\ No newline at end of file