Inheritance: implements Neos\Flow\ResourceManagement\ResourceMetaDataInterface, implements Neos\Cache\CacheAwareInterface
 /**
  * Map the given resource to a media model class.
  *
  * @param PersistentResource $resource
  * @param array $additionalProperties Optional properties that can be taken into account for deciding the model class. what you get here can depend on the caller, so you should always fallback to something based on the resource.
  * @return string
  */
 public function map(PersistentResource $resource, array $additionalProperties = array())
 {
     $mediaType = MediaTypes::getMediaTypeFromFilename($resource->getFilename());
     foreach ($this->settings['patterns'] as $pattern => $mappingInformation) {
         if (preg_match($pattern, $mediaType)) {
             return $mappingInformation['className'];
         }
     }
     return $this->settings['default'];
 }
 /**
  * @test
  */
 public function getMediaTypeReturnsMediaTypeBasedOnFileExtension()
 {
     $resource = new PersistentResource();
     $resource->setFilename('file.jpg');
     $this->assertSame('image/jpeg', $resource->getMediaType());
     $resource = new PersistentResource();
     $resource->setFilename('file.zip');
     $this->assertSame('application/zip', $resource->getMediaType());
     $resource = new PersistentResource();
     $resource->setFilename('file.someunknownextension');
     $this->assertSame('application/octet-stream', $resource->getMediaType());
 }
Beispiel #3
0
 /**
  * The given $value is valid if it is an \Neos\Flow\ResourceManagement\PersistentResource of the configured resolution
  * Note: a value of NULL or empty string ('') is considered valid
  *
  * @param \Neos\Flow\ResourceManagement\PersistentResource $resource The resource object that should be validated
  * @return void
  * @api
  */
 protected function isValid($resource)
 {
     if (!$resource instanceof \Neos\Flow\ResourceManagement\PersistentResource) {
         $this->addError('The given value was not a PersistentResource instance.', 1327865587);
         return;
     }
     $fileExtension = $resource->getFileExtension();
     if ($fileExtension === null || $fileExtension === '') {
         $this->addError('The file has no file extension.', 1327865808);
         return;
     }
     if (!in_array($fileExtension, $this->options['allowedExtensions'])) {
         $this->addError('The file extension "%s" is not allowed.', 1327865764, array($resource->getFileExtension()));
         return;
     }
 }
 /**
  * Finds other resources which are referring to the same resource data and filename
  *
  * @param PersistentResource $resource The resource used for finding similar resources
  * @return QueryResultInterface The result, including the given resource
  */
 public function findSimilarResources(PersistentResource $resource)
 {
     $query = $this->createQuery();
     $query->matching($query->logicalAnd($query->equals('sha1', $resource->getSha1()), $query->equals('filename', $resource->getFilename())));
     return $query->execute();
 }
 /**
  * Publishes the given persistent resource from the given storage
  *
  * @param PersistentResource $resource The resource to publish
  * @param CollectionInterface $collection The collection the given resource belongs to
  * @return void
  */
 public function publishResource(PersistentResource $resource, CollectionInterface $collection)
 {
     $sourceStream = $resource->getStream();
     if ($sourceStream === false) {
         $this->handleMissingData($resource, $collection);
         return;
     }
     $this->publishFile($sourceStream, $this->getRelativePublicationPathAndFilename($resource));
     fclose($sourceStream);
 }
 /**
  * Get the size of a Flow PersistentResource that contains an image file.
  *
  * @param PersistentResource $resource
  * @return array width and height as keys
  * @throws ImageFileException
  */
 public function getImageSize(PersistentResource $resource)
 {
     $cacheIdentifier = $resource->getCacheEntryIdentifier();
     $imageSize = $this->imageSizeCache->get($cacheIdentifier);
     if ($imageSize !== false) {
         return $imageSize;
     }
     // TODO: Special handling for SVG should be refactored at a later point.
     if ($resource->getMediaType() === 'image/svg+xml') {
         $imageSize = ['width' => null, 'height' => null];
     } else {
         try {
             $imagineImage = $this->imagineService->read($resource->getStream());
             $sizeBox = $imagineImage->getSize();
             $imageSize = array('width' => $sizeBox->getWidth(), 'height' => $sizeBox->getHeight());
         } catch (\Exception $e) {
             throw new ImageFileException(sprintf('The given resource was not an image file your choosen driver can open. The original error was: %s', $e->getMessage()), 1336662898);
         }
     }
     $this->imageSizeCache->set($cacheIdentifier, $imageSize);
     return $imageSize;
 }
 /**
  * Returns the web accessible URI for the given resource object
  *
  * @param PersistentResource $resource The resource object
  * @return string|boolean A URI as a string or FALSE if the collection of the resource is not found
  * @api
  */
 public function getPublicPersistentResourceUri(PersistentResource $resource)
 {
     $this->initialize();
     if (!isset($this->collections[$resource->getCollectionName()])) {
         return false;
     }
     /** @var TargetInterface $target */
     $target = $this->collections[$resource->getCollectionName()]->getTarget();
     return $target->getPublicPersistentResourceUri($resource);
 }
 /**
  * Returns the IANA media type of this asset
  *
  * @return string
  */
 public function getMediaType()
 {
     return $this->resource->getMediaType();
 }
 /**
  * Update the resource on an asset.
  *
  * @param AssetInterface $asset
  * @param PersistentResource $resource
  * @param array $options
  * @throws InvalidArgumentValueException
  * @return void
  */
 public function updateAssetResourceAction(AssetInterface $asset, PersistentResource $resource, array $options = [])
 {
     $sourceMediaType = MediaTypes::parseMediaType($asset->getMediaType());
     $replacementMediaType = MediaTypes::parseMediaType($resource->getMediaType());
     // Prevent replacement of image, audio and video by a different mimetype because of possible rendering issues.
     if (in_array($sourceMediaType['type'], ['image', 'audio', 'video']) && $sourceMediaType['type'] !== $replacementMediaType['type']) {
         $this->addFlashMessage('Resources of type "%s" can only be replaced by a similar resource. Got type "%s"', '', Message::SEVERITY_WARNING, [$sourceMediaType['type'], $resource->getMediaType()], 1462308179);
         $this->redirect('index');
     }
     parent::updateAssetResourceAction($asset, $resource, $options);
 }
 /**
  * Imports the given temporary file into the storage and creates the new resource object.
  *
  * Note: the temporary file is (re-)moved by this method.
  *
  * @param string $temporaryPathAndFileName
  * @param string $collectionName
  * @return PersistentResource
  * @throws StorageException
  */
 protected function importTemporaryFile($temporaryPathAndFileName, $collectionName)
 {
     $this->fixFilePermissions($temporaryPathAndFileName);
     $sha1Hash = sha1_file($temporaryPathAndFileName);
     $targetPathAndFilename = $this->getStoragePathAndFilenameByHash($sha1Hash);
     if (!is_file($targetPathAndFilename)) {
         $this->moveTemporaryFileToFinalDestination($temporaryPathAndFileName, $targetPathAndFilename);
     } else {
         unlink($temporaryPathAndFileName);
     }
     $resource = new PersistentResource();
     $resource->setFileSize(filesize($targetPathAndFilename));
     $resource->setCollectionName($collectionName);
     $resource->setSha1($sha1Hash);
     $resource->setMd5(md5_file($targetPathAndFilename));
     return $resource;
 }
 /**
  * Returns a stream handle which can be used internally to open / copy the given resource
  * stored in this storage.
  *
  * @param PersistentResource $resource The resource stored in this storage
  * @return resource | boolean The resource stream or FALSE if the stream could not be obtained
  */
 public function getStreamByResource(PersistentResource $resource)
 {
     $pathAndFilename = $this->getStoragePathAndFilenameByHash($resource->getSha1());
     return file_exists($pathAndFilename) ? fopen($pathAndFilename, 'rb') : false;
 }