/**
  * The actual text extraction.
  *
  * @param FileInterface $file
  * @return string
  */
 public function extractText(FileInterface $file)
 {
     $localTempFile = $file->getForLocalProcessing(false);
     // extract text
     $content = file_get_contents($localTempFile);
     // In case of remote storage, the temporary copy of the
     // original file in typo3temp must be removed
     // Simply compare the filenames, because the filename is so unique that
     // it is nearly impossible to have a file with this name in a storage
     if (PathUtility::basename($localTempFile) !== $file->getName()) {
         unlink($localTempFile);
     }
     return $content;
 }
Exemple #2
0
 /**
  * Removes a temporary file.
  *
  * When working with a file, the actual file might be on a remote storage.
  * To work with it it gets copied to local storage, those temporary local
  * copies need to be removed when they're not needed anymore.
  *
  * @param string $localTempFilePath Path to the local file copy
  * @param \TYPO3\CMS\Core\Resource\FileInterface $sourceFile Original file
  */
 protected function cleanupTempFile($localTempFilePath, FileInterface $sourceFile)
 {
     if (PathUtility::basename($localTempFilePath) !== $sourceFile->getName()) {
         unlink($localTempFilePath);
     }
 }
 /**
  * Previously in \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::func_rename()
  *
  * @param FileInterface $file
  * @param string $targetFileName
  *
  * @throws Exception\InsufficientFileWritePermissionsException
  * @throws Exception\InsufficientFileReadPermissionsException
  * @throws Exception\InsufficientUserPermissionsException
  * @return FileInterface
  */
 public function renameFile($file, $targetFileName)
 {
     // @todo add $conflictMode setting
     // The name should be different from the current.
     if ($file->getName() === $targetFileName) {
         return $file;
     }
     $sanitizedTargetFileName = $this->driver->sanitizeFileName($targetFileName);
     $this->assureFileRenamePermissions($file, $sanitizedTargetFileName);
     $this->emitPreFileRenameSignal($file, $sanitizedTargetFileName);
     // Call driver method to rename the file and update the index entry
     try {
         $newIdentifier = $this->driver->renameFile($file->getIdentifier(), $sanitizedTargetFileName);
         if ($file instanceof File) {
             $file->updateProperties(array('identifier' => $newIdentifier));
         }
         $this->getIndexer()->updateIndexEntry($file);
     } catch (\RuntimeException $e) {
     }
     $this->emitPostFileRenameSignal($file, $sanitizedTargetFileName);
     return $file;
 }
 /**
  * Creates a magic image
  *
  * @param \TYPO3\CMS\Core\Resource\FileInterface $imageFileObject: the original image file
  * @param array $fileConfiguration (width, height, maxW, maxH)
  * @param string $targetFolderCombinedIdentifier: target folder combined identifier
  * @return \TYPO3\CMS\Core\Resource\FileInterface
  */
 public function createMagicImage(\TYPO3\CMS\Core\Resource\FileInterface $imageFileObject, array $fileConfiguration, $targetFolderCombinedIdentifier)
 {
     $magicImage = NULL;
     // Get file for processing
     $imageFilePath = $imageFileObject->getForLocalProcessing(TRUE);
     // Process dimensions
     $maxWidth = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($fileConfiguration['width'], 0, $fileConfiguration['maxW']);
     $maxHeight = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($fileConfiguration['height'], 0, $fileConfiguration['maxH']);
     if (!$maxWidth) {
         $maxWidth = $fileConfiguration['maxW'];
     }
     if (!$maxHeight) {
         $maxHeight = $fileConfiguration['maxH'];
     }
     // Create the magic image
     $magicImageInfo = $this->getImageObject()->imageMagickConvert($imageFilePath, 'WEB', $maxWidth . 'm', $maxHeight . 'm');
     if ($magicImageInfo[3]) {
         $targetFileName = 'RTEmagicC_' . pathInfo($imageFileObject->getName(), PATHINFO_FILENAME) . '.' . pathinfo($magicImageInfo[3], PATHINFO_EXTENSION);
         $magicFolder = $this->getMagicFolder($targetFolderCombinedIdentifier);
         if ($magicFolder instanceof \TYPO3\CMS\Core\Resource\Folder) {
             $magicImage = $magicFolder->addFile($magicImageInfo[3], $targetFileName, 'changeName');
         }
     }
     return $magicImage;
 }
 /**
  * @return string
  */
 public function getName()
 {
     return $this->resource->getName();
 }
 /**
  * This method actually does the processing of files locally
  *
  * takes the original file (on remote storages this will be fetched from the remote server)
  * does the IM magic on the local server by creating a temporary typo3temp/ file
  * copies the typo3temp/ file to the processingfolder of the target storage
  * removes the typo3temp/ file
  *
  * @param \TYPO3\CMS\Core\Resource\ProcessedFile $processedFile
  * @param \TYPO3\CMS\Core\Resource\FileInterface $file
  * @param array $configuration
  * @return void
  */
 protected function processImagePreview(\TYPO3\CMS\Core\Resource\ProcessedFile $processedFile, \TYPO3\CMS\Core\Resource\FileInterface $file, array $configuration)
 {
     // Merge custom configuration with default configuration
     $configuration = array_merge(array('width' => 64, 'height' => 64), $configuration);
     $configuration['width'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($configuration['width'], 1, 1000);
     $configuration['height'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($configuration['height'], 1, 1000);
     $originalFileName = $file->getForLocalProcessing(FALSE);
     // Create a temporary file in typo3temp/
     if ($file->getExtension() === 'jpg') {
         $targetFileExtension = '.jpg';
     } else {
         $targetFileExtension = '.png';
     }
     $targetFolder = $this->storage->getProcessingFolder();
     $targetFileName = 'preview_' . $processedFile->calculateChecksum() . $targetFileExtension;
     // Do the actual processing
     if (!$targetFolder->hasFile($targetFileName)) {
         // Create the thumb filename in typo3temp/preview_....jpg
         $temporaryFileName = \TYPO3\CMS\Core\Utility\GeneralUtility::tempnam('preview_') . $targetFileExtension;
         // Check file extension
         if ($file->getType() != $file::FILETYPE_IMAGE && !\TYPO3\CMS\Core\Utility\GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->getExtension())) {
             // Create a default image
             $this->getTemporaryImageWithText($temporaryFileName, 'Not imagefile!', 'No ext!', $file->getName());
         } else {
             // Create the temporary file
             if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im']) {
                 $parameters = '-sample ' . $configuration['width'] . 'x' . $configuration['height'] . ' ' . $this->wrapFileName($originalFileName) . '[0] ' . $this->wrapFileName($temporaryFileName);
                 $cmd = \TYPO3\CMS\Core\Utility\GeneralUtility::imageMagickCommand('convert', $parameters);
                 \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd);
                 if (!file_exists($temporaryFileName)) {
                     // Create a error gif
                     $this->getTemporaryImageWithText($temporaryFileName, 'No thumb', 'generated!', $file->getName());
                 }
             }
         }
         // Temporary image could have been created
         if (file_exists($temporaryFileName)) {
             \TYPO3\CMS\Core\Utility\GeneralUtility::fixPermissions($temporaryFileName);
             // Copy the temporary file to the processedFolder
             // this is done here, as the driver can do this without worrying
             // about existing ProcessedFile objects
             // or permissions in the storage
             // for "remote" storages this means "uploading" the file to the storage again
             // for the virtual storage, it is merely a thing of "copying a file from typo3temp/ to typo3temp/_processed_"
             $this->driver->addFile($temporaryFileName, $targetFolder, $targetFileName, $processedFile);
             // Remove the temporary file as it's not needed anymore
             \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($temporaryFileName);
             $processedFile->setProcessed(TRUE);
         }
     } else {
         // the file already exists, nothing to do locally, but still mark the file as processed and save the data
         // and update the fields, as they might have not been set
         if ($processedFile->getProperty('identifier') == '') {
             $identifier = $targetFolder->getIdentifier() . $targetFileName;
             $processedFile->updateProperties(array('name' => $targetFileName, 'identifier' => $identifier));
         }
         $processedFile->setProcessed(TRUE);
     }
 }