/**
  * Converts the given string or array to a ResourcePointer object.
  *
  * If the input format is an array, this method assumes the resource to be a
  * fresh file upload and imports the temporary upload file through the
  * resource manager.
  *
  * @param array $source The upload info (expected keys: error, name, tmp_name)
  * @param string $targetType
  * @param array $convertedChildProperties
  * @param \TYPO3\FLOW3\Property\PropertyMappingConfigurationInterface $configuration
  * @return \TYPO3\Media\Domain\Model\Image An object or an instance of TYPO3\FLOW3\Error\Error if the input format is not supported or could not be converted for other reasons
  * @throws \TYPO3\FLOW3\Property\Exception\TypeConverterException
  */
 public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\FLOW3\Property\PropertyMappingConfigurationInterface $configuration = NULL)
 {
     $resource = $this->resourceManager->importUploadedResource($_FILES['file']);
     if ($resource === FALSE) {
         throw new \TYPO3\FLOW3\Property\Exception\TypeConverterException('Resource could not be converted.', 1316428994);
     }
     $image = new \TYPO3\Media\Domain\Model\Image($resource);
     // TODO: this should maybe be settable
     $image->setTitle('');
     return $image;
 }
Example #2
0
 /**
  * Evaluates the absolute path and filename of the resource file specified
  * by the given path.
  *
  * @param string $requestedPath
  * @param boolean $checkForExistence Whether a (non-hash) path should be checked for existence before being returned
  * @return mixed The full path and filename or FALSE if the file doesn't exist
  * @throws \TYPO3\FLOW3\Resource\Exception
  * @throws \InvalidArgumentException
  */
 protected function evaluateResourcePath($requestedPath, $checkForExistence = TRUE)
 {
     if (substr($requestedPath, 0, strlen(self::SCHEME)) !== self::SCHEME) {
         throw new \InvalidArgumentException('The ' . __CLASS__ . ' only supports the \'' . self::SCHEME . '\' scheme.', 1256052544);
     }
     $uriParts = parse_url($requestedPath);
     if (!is_array($uriParts) || !isset($uriParts['host'])) {
         return FALSE;
     }
     if (strlen($uriParts['host']) === 40) {
         $resourcePath = $this->resourceManager->getPersistentResourcesStorageBaseUri() . $uriParts['host'];
         if ($checkForExistence === FALSE || file_exists($resourcePath)) {
             return $resourcePath;
         } else {
             return FALSE;
         }
     }
     if (!$this->packageManager->isPackageAvailable($uriParts['host'])) {
         throw new \TYPO3\FLOW3\Resource\Exception(sprintf('Invalid resource URI "%s": Package "%s" is not available.', $requestedPath, $uriParts['host']), 1309269952);
     }
     $package = $this->packageManager->getPackage($uriParts['host']);
     $resourcePath = \TYPO3\FLOW3\Utility\Files::concatenatePaths(array($package->getResourcesPath(), $uriParts['path']));
     if ($checkForExistence === FALSE || file_exists($resourcePath)) {
         return $resourcePath;
     }
     return FALSE;
 }
 /**
  * @test
  */
 public function convertFromReturnsAnErrorIfTheUploadedFileCantBeImported()
 {
     $source = array('tmp_name' => 'SomeFilename', 'error' => \UPLOAD_ERR_OK);
     $this->mockResourceManager->expects($this->once())->method('importUploadedResource')->with($source)->will($this->returnValue(FALSE));
     $actualResult = $this->resourceTypeConverter->convertFrom($source, 'TYPO3\\FLOW3\\Resource\\Resource');
     $this->assertInstanceOf('TYPO3\\FLOW3\\Error\\Error', $actualResult);
 }
 /**
  * Handles conversion of our XML format into objects.
  *
  * Note: currently only ImageVariant instances are supported.
  *
  * @param \SimpleXMLElement $xml
  * @return object
  * @throws \TYPO3\TYPO3\Domain\Exception
  */
 protected function xmlToObject(\SimpleXMLElement $xml)
 {
     $object = NULL;
     $className = (string) $xml['__classname'];
     switch ($className) {
         case 'TYPO3\\Media\\Domain\\Model\\ImageVariant':
             $processingInstructions = unserialize(trim((string) $xml->processingInstructions));
             $originalResource = $this->resourceManager->createResourceFromContent(base64_decode(trim((string) $xml->originalImage->resource->content)), (string) $xml->originalImage->resource->filename);
             $object = new \TYPO3\Media\Domain\Model\ImageVariant(new \TYPO3\Media\Domain\Model\Image($originalResource), $processingInstructions);
             break;
         default:
             throw new \TYPO3\TYPO3\Domain\Exception('Unsupported object of type "' . get_class($className) . '" hit during XML import.', 1347144938);
     }
     return $object;
 }
Example #5
0
 /**
  * Converts the given string or array to a ResourcePointer object.
  *
  * If the input format is an array, this method assumes the resource to be a
  * fresh file upload and imports the temporary upload file through the
  * resource manager.
  *
  * @param array $source The upload info (expected keys: error, name, tmp_name)
  * @param string $targetType
  * @param array $convertedChildProperties
  * @param \TYPO3\FLOW3\Property\PropertyMappingConfigurationInterface $configuration
  * @return \TYPO3\FLOW3\Resource\Resource|TYPO3\FLOW3\Error\Error if the input format is not supported or could not be converted for other reasons
  */
 public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\FLOW3\Property\PropertyMappingConfigurationInterface $configuration = NULL)
 {
     if (!isset($source['error']) || $source['error'] === \UPLOAD_ERR_NO_FILE) {
         if (isset($source['submittedFile']) && isset($source['submittedFile']['filename']) && isset($source['submittedFile']['resourcePointer'])) {
             $resourcePointer = $this->persistenceManager->getObjectByIdentifier($source['submittedFile']['resourcePointer'], 'TYPO3\\FLOW3\\Resource\\ResourcePointer');
             if ($resourcePointer) {
                 $resource = new Resource();
                 $resource->setFilename($source['submittedFile']['filename']);
                 $resource->setResourcePointer($resourcePointer);
                 return $resource;
             }
         }
         return NULL;
     }
     if ($source['error'] !== \UPLOAD_ERR_OK) {
         switch ($source['error']) {
             case \UPLOAD_ERR_INI_SIZE:
             case \UPLOAD_ERR_FORM_SIZE:
             case \UPLOAD_ERR_PARTIAL:
                 return new \TYPO3\FLOW3\Error\Error(\TYPO3\FLOW3\Utility\Files::getUploadErrorMessage($source['error']), 1264440823);
             default:
                 $this->systemLogger->log(sprintf('A server error occurred while converting an uploaded resource: "%s"', \TYPO3\FLOW3\Utility\Files::getUploadErrorMessage($source['error'])), LOG_ERR);
                 return new \TYPO3\FLOW3\Error\Error('An error occurred while uploading. Please try again or contact the administrator if the problem remains', 1340193849);
         }
     }
     if (isset($this->convertedResources[$source['tmp_name']])) {
         return $this->convertedResources[$source['tmp_name']];
     }
     $resource = $this->resourceManager->importUploadedResource($source);
     if ($resource === FALSE) {
         return new \TYPO3\FLOW3\Error\Error('The resource manager could not create a Resource instance.', 1264517906);
     } else {
         $this->convertedResources[$source['tmp_name']] = $resource;
         return $resource;
     }
 }