Example #1
9
 /**
  * Imports the given temporary file into the storage and creates the new resource object.
  *
  * @param string $temporaryPathAndFilename Path and filename leading to the temporary file
  * @param string $collectionName Name of the collection to import into
  * @return Resource The imported resource
  */
 protected function importTemporaryFile($temporaryPathAndFilename, $collectionName)
 {
     $sha1Hash = sha1_file($temporaryPathAndFilename);
     $md5Hash = md5_file($temporaryPathAndFilename);
     $resource = new Resource();
     $resource->setFileSize(filesize($temporaryPathAndFilename));
     $resource->setCollectionName($collectionName);
     $resource->setSha1($sha1Hash);
     $resource->setMd5($md5Hash);
     $objectName = $this->keyPrefix . $sha1Hash;
     $options = array('Bucket' => $this->bucketName, 'Body' => fopen($temporaryPathAndFilename, 'rb'), 'ContentLength' => $resource->getFileSize(), 'ContentType' => $resource->getMediaType(), 'Key' => $objectName);
     if (!$this->s3Client->doesObjectExist($this->bucketName, $this->keyPrefix . $sha1Hash)) {
         $this->s3Client->putObject($options);
         $this->systemLogger->log(sprintf('Successfully imported resource as object "%s" into bucket "%s" with MD5 hash "%s"', $objectName, $this->bucketName, $resource->getMd5() ?: 'unknown'), LOG_INFO);
     } else {
         $this->systemLogger->log(sprintf('Did not import resource as object "%s" into bucket "%s" because that object already existed.', $objectName, $this->bucketName), LOG_INFO);
     }
     return $resource;
 }
 /**
  * @param FlowResource $flowResource
  * @return array
  */
 protected function findSuitableExtractorAdaptersForResource(FlowResource $flowResource)
 {
     $extractorAdapters = $this->getExtractorAdapters();
     $mediaType = $flowResource->getMediaType();
     $suitableAdapterClasses = [];
     foreach ($extractorAdapters as $extractorAdapterClass => $compatibleMediaTypes) {
         if (in_array($mediaType, $compatibleMediaTypes)) {
             $suitableAdapterClasses[] = $extractorAdapterClass;
         }
     }
     return $suitableAdapterClasses;
 }
 /**
  * @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());
 }
 /**
  * Update the resource on an asset.
  *
  * @param AssetInterface $asset
  * @param FlowResource $resource
  * @param array $options
  * @throws InvalidArgumentValueException
  * @return void
  */
 public function updateAssetResourceAction(AssetInterface $asset, FlowResource $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);
 }
 /**
  * Returns the IANA media type of this asset
  *
  * @return string
  */
 public function getMediaType()
 {
     return $this->resource->getMediaType();
 }
 /**
  * {@inheritDoc}
  */
 public function getMediaType()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'getMediaType', array());
     return parent::getMediaType();
 }
 /**
  * Get the size of a Flow Resource object that contains an image file.
  *
  * @param FlowResource $resource
  * @return array width and height as keys
  * @throws ImageFileException
  */
 public function getImageSize(FlowResource $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;
 }