From 8cc5cbb2d452685136a57256075aa44469ed2563 Mon Sep 17 00:00:00 2001 From: Michael Williams Date: Thu, 21 Mar 2013 00:12:51 -0700 Subject: [PATCH] Initial support for creating a variant from a parent variant which is needed when we generate new variants in the context after we process the initial variant tree. This is very early support and does not deal much with error check and fallbacks such as what happens when the parent source is not available. We also do not use media hints for different things such as the persistence adapter --- lib/Oryzone/MediaStorage/MediaStorage.php | 33 +++++++++++++++++++ .../MediaStorage/Provider/ImageProvider.php | 27 +++++++++++++++ .../Provider/ProviderInterface.php | 2 ++ 3 files changed, 62 insertions(+) diff --git a/lib/Oryzone/MediaStorage/MediaStorage.php b/lib/Oryzone/MediaStorage/MediaStorage.php index ac7d9b6..40e22ea 100644 --- a/lib/Oryzone/MediaStorage/MediaStorage.php +++ b/lib/Oryzone/MediaStorage/MediaStorage.php @@ -483,6 +483,39 @@ public function render(MediaInterface $media, $variant = NULL, $options = array( $variant = $this->defaultVariant; } + if (!$media->hasVariant($variant)) { + // If this media does not have a variant based on its parent variant, if it has a parent + $filesystem = $this->getFilesystem($context->getFilesystemName()); + $namingStrategy = $this->getNamingStrategy($context->getNamingStrategyName()); + $context = $this->getContext($media->getContext()); + $provider = $this->getProvider($context->getProviderName(), $context->getProviderOptions()); + $variantsTree = $context->buildVariantTree(); + + /** @var $node VariantNode */ + $node = $variantsTree->getNode($variant); + $childVariant = $node->getContent(); + while ($node->getParent()) { // advance to parent node + $node = $node->getParent(); + } + + // Get parents variant information to base this new variant off + $parentVariant = $media->getVariantInstance($node->getContent()->getName()); + // @todo + if (!$filesystem->has($parentVariant->getFilename())) { + throw new \InvalidArgumentException(); + } + + $filesystem->get($parentVariant->getFilename()); + $result = $provider->processFromParent($media, $childVariant, $parentVariant, $filesystem); + $name = $namingStrategy->generateName($media, $childVariant, $filesystem); + $this->saveFileToFilesystem($result, $name, $filesystem, $childVariant); + + //updates the variant in the media (to store the new values) + $media->addVariant($childVariant); + + $this->persistenceAdapter->update($media); + } + $provider = $this->getProvider($context->getProviderName(), $context->getProviderOptions()); $variantInstance = $media->getVariantInstance($variant); diff --git a/lib/Oryzone/MediaStorage/Provider/ImageProvider.php b/lib/Oryzone/MediaStorage/Provider/ImageProvider.php index 12440da..8019d22 100644 --- a/lib/Oryzone/MediaStorage/Provider/ImageProvider.php +++ b/lib/Oryzone/MediaStorage/Provider/ImageProvider.php @@ -11,6 +11,10 @@ namespace Oryzone\MediaStorage\Provider; +use Gaufrette\Filesystem, + Gaufrette\Stream\Local, + Gaufrette\StreamMode; + use Oryzone\MediaStorage\Model\MediaInterface, Oryzone\MediaStorage\Variant\VariantInterface, Oryzone\MediaStorage\Exception\ProviderProcessException, @@ -169,6 +173,29 @@ public function validateContent($content) return false; } + public function processFromParent($media, VariantInterface $variant, VariantInterface $parentVariant, Filesystem $filesystem) + { + $tempName = $this->tempDir.'/'.$parentVariant->getFilename(); + + $src = $filesystem->createStream($parentVariant->getFilename()); + $dst = new Local($tempName); + + $src->open(new StreamMode('rb+')); + $dst->open(new StreamMode('wb+')); + + while (!$src->eof()) { + $data = $src->read(100000); + $written = $dst->write($data); + } + $dst->close(); + $src->close(); + + $this->addTempFile($tempName); + + // We now have a local copy of file + return $this->process($media, $variant, new \SplFileInfo($tempName)); + } + /** * {@inheritDoc} */ diff --git a/lib/Oryzone/MediaStorage/Provider/ProviderInterface.php b/lib/Oryzone/MediaStorage/Provider/ProviderInterface.php index 61df4a5..7b94c08 100644 --- a/lib/Oryzone/MediaStorage/Provider/ProviderInterface.php +++ b/lib/Oryzone/MediaStorage/Provider/ProviderInterface.php @@ -95,6 +95,8 @@ public function prepare(MediaInterface $media, ContextInterface $context); */ public function process(MediaInterface $media, VariantInterface $variant, \SplFileInfo $source = NULL); + public function processFromParent($media, VariantInterface $variant, VariantInterface $parentVariant, Filesystem $filesystem); + /** * Renders a variant to HTML code. Useful for twig (or other template engines) integrations *