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