/**
  * @test
  */
 public function getMediaTypeReturnsMediaTypeBasedOnFileExtension()
 {
     $resource = new Resource();
     $resource->setFilename('file.jpg');
     $this->assertSame('image/jpeg', $resource->getMediaType());
     $resource = new Resource();
     $resource->setFilename('file.zip');
     $this->assertSame('application/zip', $resource->getMediaType());
     $resource = new Resource();
     $resource->setFilename('file.someunknownextension');
     $this->assertSame('application/octet-stream', $resource->getMediaType());
 }
 /**
  * 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\Flow\Property\PropertyMappingConfigurationInterface $configuration
  * @return \TYPO3\Flow\Resource\Resource|TYPO3\Flow\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\Flow\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\\Flow\\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\Flow\Error\Error(\TYPO3\Flow\Utility\Files::getUploadErrorMessage($source['error']), 1264440823);
             default:
                 $this->systemLogger->log(sprintf('A server error occurred while converting an uploaded resource: "%s"', \TYPO3\Flow\Utility\Files::getUploadErrorMessage($source['error'])), LOG_ERR);
                 return new \TYPO3\Flow\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\Flow\Error\Error('The resource manager could not create a Resource instance.', 1264517906);
     } else {
         $this->convertedResources[$source['tmp_name']] = $resource;
         return $resource;
     }
 }
 /**
  * {@inheritDoc}
  */
 public function setFilename($filename)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setFilename', array($filename));
     return parent::setFilename($filename);
 }
Example #4
0
 /**
  * Imports a resource (file) as specified in the given upload info array as a
  * persistent resource.
  *
  * On a successful import this method returns a Resource object representing
  * the newly imported persistent resource.
  *
  * @param array $uploadInfo An array detailing the resource to import (expected keys: name, tmp_name)
  * @param string $collectionName Name of the collection this uploaded resource should be part of
  * @return string A resource object representing the imported resource
  * @throws Exception
  * @api
  */
 public function importUploadedResource(array $uploadInfo, $collectionName)
 {
     $pathInfo = pathinfo($uploadInfo['name']);
     $originalFilename = $pathInfo['basename'];
     $sourcePathAndFilename = $uploadInfo['tmp_name'];
     if (!file_exists($sourcePathAndFilename)) {
         throw new Exception(sprintf('The temporary file "%s" of the file upload does not exist (anymore).', $sourcePathAndFilename), 1428909075);
     }
     $newSourcePathAndFilename = $this->environment->getPathToTemporaryDirectory() . 'Flownative_Aws_S3_' . uniqid() . '.tmp';
     if (move_uploaded_file($sourcePathAndFilename, $newSourcePathAndFilename) === false) {
         throw new Exception(sprintf('The uploaded file "%s" could not be moved to the temporary location "%s".', $sourcePathAndFilename, $newSourcePathAndFilename), 1428909076);
     }
     $sha1Hash = sha1_file($newSourcePathAndFilename);
     $md5Hash = md5_file($newSourcePathAndFilename);
     $resource = new Resource();
     $resource->setFilename($originalFilename);
     $resource->setCollectionName($collectionName);
     $resource->setFileSize(filesize($newSourcePathAndFilename));
     $resource->setSha1($sha1Hash);
     $resource->setMd5($md5Hash);
     $this->s3Client->putObject(array('Bucket' => $this->bucketName, 'Body' => fopen($newSourcePathAndFilename, 'rb'), 'ContentLength' => $resource->getFileSize(), 'ContentType' => $resource->getMediaType(), 'Key' => $this->keyPrefix . $sha1Hash));
     return $resource;
 }
 /**
  * @param array $source
  * @param PropertyMappingConfigurationInterface $configuration
  * @return Resource|Error
  */
 protected function handleHashAndData(array $source, PropertyMappingConfigurationInterface $configuration = NULL)
 {
     $hash = NULL;
     $resource = FALSE;
     $givenResourceIdentity = NULL;
     if (isset($source['__identity'])) {
         $givenResourceIdentity = $source['__identity'];
         unset($source['__identity']);
         $resource = $this->persistenceManager->getObjectByIdentifier($givenResourceIdentity, 'TYPO3\\Flow\\Resource\\Resource');
         if ($resource instanceof \TYPO3\Flow\Resource\Resource) {
             return $resource;
         }
         if ($configuration->getConfigurationValue('TYPO3\\Flow\\Resource\\ResourceTypeConverter', self::CONFIGURATION_IDENTITY_CREATION_ALLOWED) !== TRUE) {
             throw new \TYPO3\Flow\Property\Exception\InvalidPropertyMappingConfigurationException('Creation of resource objects with identity not allowed. To enable this, you need to set the PropertyMappingConfiguration Value "CONFIGURATION_IDENTITY_CREATION_ALLOWED" to TRUE');
         }
     }
     if (isset($source['hash']) && preg_match('/[0-9a-f]{40}/', $source['hash'])) {
         $hash = $source['hash'];
     }
     if ($hash !== NULL) {
         $resourcePointer = $this->persistenceManager->getObjectByIdentifier($hash, 'TYPO3\\Flow\\Resource\\ResourcePointer');
         if ($resourcePointer) {
             $resource = new Resource();
             $resource->setFilename($source['filename']);
             $resource->setResourcePointer($resourcePointer);
         }
     }
     if ($resource === NULL) {
         if (isset($source['data'])) {
             $resource = $this->resourceManager->createResourceFromContent($source['data'], $source['filename']);
         } elseif ($hash !== NULL) {
             $resource = $this->resourceManager->importResource($configuration->getConfigurationValue('TYPO3\\Flow\\Resource\\ResourceTypeConverter', self::CONFIGURATION_RESOURCE_LOAD_PATH) . '/' . $hash);
             if (is_array($source) && isset($source['filename'])) {
                 $resource->setFilename($source['filename']);
             }
         }
     }
     if ($resource instanceof \TYPO3\Flow\Resource\Resource) {
         if ($givenResourceIdentity !== NULL) {
             $this->setIdentity($resource, $givenResourceIdentity);
         }
         return $resource;
     } else {
         return new Error('The resource manager could not create a Resource instance.', 1404312901);
     }
 }
 /**
  * Imports a resource (file) as specified in the given upload info array as a
  * persistent resource.
  *
  * On a successful import this method returns a Resource object representing
  * the newly imported persistent resource.
  *
  * @param array $uploadInfo An array detailing the resource to import (expected keys: name, tmp_name)
  * @param string $collectionName Name of the collection this uploaded resource should be part of
  * @return string A resource object representing the imported resource
  * @throws Exception
  * @api
  */
 public function importUploadedResource(array $uploadInfo, $collectionName)
 {
     if ($this->debug) {
         $this->systemLogger->log('storage ' . $this->name . ': importUploadedResource');
     }
     $pathInfo = pathinfo($uploadInfo['name']);
     $originalFilename = $pathInfo['basename'];
     $sourcePathAndFilename = $uploadInfo['tmp_name'];
     if (!file_exists($sourcePathAndFilename)) {
         throw new Exception(sprintf('The temporary file "%s" of the file upload does not exist (anymore).', $sourcePathAndFilename), 1375267007);
     }
     $newSourcePathAndFilename = $this->environment->getPathToTemporaryDirectory() . 'Sbruggmann_FlowKeyCDN_' . uniqid() . '.tmp';
     if (move_uploaded_file($sourcePathAndFilename, $newSourcePathAndFilename) === FALSE) {
         throw new Exception(sprintf('The uploaded file "%s" could not be moved to the temporary location "%s".', $sourcePathAndFilename, $newSourcePathAndFilename), 1375267045);
     }
     $sha1Hash = sha1_file($newSourcePathAndFilename);
     $md5Hash = md5_file($newSourcePathAndFilename);
     $resource = new Resource();
     $resource->setFilename($originalFilename);
     $resource->setCollectionName($collectionName);
     $resource->setFileSize(filesize($newSourcePathAndFilename));
     $resource->setSha1($sha1Hash);
     $resource->setMd5($md5Hash);
     $this->uploadFile($newSourcePathAndFilename, $originalFilename);
     return $resource;
 }