/**
  * 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);
     }
 }