Skip to content

Instantly share code, notes, and snippets.

@DanieleGBX
Created August 24, 2018 20:54
Show Gist options
  • Save DanieleGBX/ebce034a0371067474f3605d6419ecf2 to your computer and use it in GitHub Desktop.
Save DanieleGBX/ebce034a0371067474f3605d6419ecf2 to your computer and use it in GitHub Desktop.
sonata media extension
<?php
use Gaufrette\Adapter\AwsS3;
class AwsS3Extended extends AwsS3
{
public function getPreSignedUrl(string $key, \DateTimeInterface $expiration)
{
$castedExpiration = new \DateTime(null, $expiration->getTimezone());
$castedExpiration->setTimestamp($expiration->getTimestamp());
return (string) $this->service->createPresignedRequest(
$this->service->getCommand('GetObject', [
'Bucket' => $this->bucket,
'Key' => $key,
]),
$castedExpiration
)->getUri();
}
public function getPathForKey($key)
{
return $this->computePath($key);
}
}
<?php
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class OverrideServiceCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if ($container->hasDefinition('sonata.media.adapter.filesystem.s3')) {
$definition = $container->getDefinition('sonata.media.adapter.filesystem.s3');
$definition->setClass(AwsS3Extended::class);
}
if ($container->hasDefinition('sonata.media.metadata.proxy')) {
$definition = $container->getDefinition('sonata.media.metadata.proxy');
$definition->setClass(ProxyMetadataBuilderExtended::class);
}
}
}
<?php
use Gaufrette\Adapter\AwsS3;
use Sonata\MediaBundle\Metadata\ProxyMetadataBuilder;
use Sonata\MediaBundle\Model\MediaInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProxyMetadataBuilderExtended extends ProxyMetadataBuilder
{
protected $sharedContainer;
public function __construct(ContainerInterface $container, array $map = null)
{
$this->sharedContainer = $container;
parent::__construct($container, $map);
}
protected function getAmazonBuilder(MediaInterface $media, $filename)
{
$adapter = $this->sharedContainer->get($media->getProviderName())->getFilesystem()->getAdapter();
if ($adapter instanceof AwsS3) {// Needed because the base method doesn't understand exension.
return $this->sharedContainer->get('sonata.media.metadata.amazon')->get($media, $filename);
}
return parent::getAmazonBuilder($media, $filename);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment