Example #1
0
 /**
  * Creates or updates a file index entry from a file object.
  *
  * @param File $fileObject
  * @param bool $updateObject Set this to FALSE to get the indexed values. You have to take care of updating the object yourself then!
  * @return File|array the indexed $fileObject or an array of indexed properties.
  * @throws \RuntimeException
  */
 public function indexFile(File $fileObject, $updateObject = TRUE)
 {
     $fileObject->setIndexingInProgess(TRUE);
     // Get the file information of this object
     $fileInfo = $this->gatherFileInformation($fileObject);
     // Signal slot BEFORE the file was indexed
     $this->emitPreFileIndexSignal($fileObject, $fileInfo);
     // If the file is already indexed, then the file information will
     // be updated on the existing record
     if ($fileObject->isIndexed()) {
         $fileInfo['missing'] = 0;
         $fileObject->updateProperties($fileInfo);
         $this->getFileIndexRepository()->update($fileObject);
     } else {
         // Check if a file has been moved outside of FAL -- we have some
         // orphaned index record in this case we could update
         $resultRows = $this->getFileIndexRepository()->findByContentHash($fileInfo['sha1']);
         $otherFiles = array();
         foreach ($resultRows as $row) {
             $otherFiles[] = $this->getFactory()->getFileObject($row['uid'], $row);
         }
         $movedFile = FALSE;
         /** @var $otherFile File */
         foreach ($otherFiles as $otherFile) {
             if (!$otherFile->exists()) {
                 // @todo: create a log entry
                 $movedFile = TRUE;
                 $fileInfo['missing'] = 0;
                 $otherFile->updateProperties($fileInfo);
                 $this->getFileIndexRepository()->update($otherFile);
                 $fileInfo['uid'] = $otherFile->getUid();
                 $fileObject = $otherFile;
                 // Skip the rest of the files here as we might have more files that are missing, but we can only
                 // have one entry. The optimal solution would be to merge these records then, but this requires
                 // some more advanced logic that we currently have not implemented.
                 break;
             }
         }
         // File was not moved, so it is a new index record
         if ($movedFile === FALSE) {
             // Crdate and tstamp should not be present when updating
             // the file object, as they only relate to the index record
             $additionalInfo = array('crdate' => $GLOBALS['EXEC_TIME'], 'tstamp' => $GLOBALS['EXEC_TIME']);
             if (isset($GLOBALS['BE_USER']->user['uid'])) {
                 $additionalInfo['cruser_id'] = (int) $GLOBALS['BE_USER']->user['uid'];
             }
             $indexRecord = array_merge($fileInfo, $additionalInfo);
             $fileObject->updateProperties($indexRecord);
             $this->getFileIndexRepository()->add($fileObject);
         }
     }
     // Check for an error during the execution and throw an exception
     $error = $GLOBALS['TYPO3_DB']->sql_error();
     if ($error) {
         throw new \RuntimeException('Error during file indexing: "' . $error . '"', 1314455642);
     }
     if ($fileInfo['type'] == $fileObject::FILETYPE_IMAGE) {
         $rawFileLocation = $fileObject->getForLocalProcessing(FALSE);
         $metaData = array();
         $imageSize = getimagesize($rawFileLocation);
         if ($imageSize === FALSE) {
             $metaData['width'] = 0;
             $metaData['height'] = 0;
         } else {
             list($metaData['width'], $metaData['height']) = $imageSize;
         }
         $this->getMetaDataRepository()->update($fileObject->getUid(), $metaData);
     }
     // Signal slot AFTER the file was indexed
     $this->emitPostFileIndexSignal($fileObject, $fileInfo);
     $fileObject->setIndexingInProgess(FALSE);
     if ($updateObject) {
         return $fileObject;
     } else {
         return $fileInfo;
     }
 }