Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom image provider - no admin thumb is generated #2459

Open
byhaskell opened this issue Aug 12, 2024 · 0 comments
Open

Custom image provider - no admin thumb is generated #2459

byhaskell opened this issue Aug 12, 2024 · 0 comments

Comments

@byhaskell
Copy link

Environment

Sonata packages

show

Color legend:
- patch or minor release available - update recommended
- major release available - update possible
- up to date version

Direct dependencies required in composer.json:
sonata-project/admin-bundle              4.31.0 4.31.0 The missing Symfony Admin Generator
sonata-project/doctrine-orm-admin-bundle 4.17.1 4.17.1 Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/entity-audit-bundle       1.18.0 1.18.0 Audit for Doctrine Entities
sonata-project/formatter-bundle          5.4.1  5.4.1  Symfony SonataFormatterBundle
sonata-project/intl-bundle               3.2.0  3.2.0  Symfony SonataIntlBundle
sonata-project/media-bundle              4.13.0 4.13.0 Symfony SonataMediaBundle
sonata-project/translation-bundle        3.3.0  3.3.0  SonataTranslationBundle

Transitive dependencies not required in composer.json:
sonata-project/block-bundle              5.1.0  5.1.0  Symfony SonataBlockBundle
sonata-project/doctrine-extensions       2.4.0  2.4.0  Doctrine2 behavioral extensions
sonata-project/exporter                  3.3.0  3.3.0  Lightweight Exporter library
sonata-project/form-extensions           2.4.0  2.4.0  Symfony form extensions
sonata-project/twig-extensions           2.4.0  2.4.0  Sonata twig extensions

Symfony packages

show

Color legend:
- patch or minor release available - update recommended
- major release available - update possible
- up to date version

Direct dependencies required in composer.json:
symfony/asset                      6.4.8  6.4.8  Manages URL generation and versioning of web assets such as CSS stylesheets, JavaScript files and image files
symfony/console                    6.4.10 6.4.10 Eases the creation of beautiful and testable command line interfaces
symfony/debug-bundle               6.4.8  6.4.8  Provides a tight integration of the Symfony VarDumper component and the ServerLogCommand from MonologBridge into the Symfony full-stack framework
symfony/dotenv                     6.4.10 6.4.10 Registers environment variables from a .env file
symfony/flex                       2.4.5  2.4.6  Composer plugin for Symfony
symfony/framework-bundle           6.4.10 6.4.10 Provides a tight integration between Symfony components and the Symfony full-stack framework
symfony/lock                       6.4.8  6.4.8  Creates and manages locks, a mechanism to provide exclusive access to a shared resource
symfony/mailer                     6.4.9  6.4.9  Helps sending emails
symfony/maker-bundle               1.60.0 1.60.0 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/monolog-bundle             3.10.0 3.10.0 Symfony MonologBundle
symfony/proxy-manager-bridge       6.4.8  6.4.8  Provides integration for ProxyManager with various Symfony components
symfony/runtime                    6.4.8  6.4.8  Enables decoupling PHP applications from global state
symfony/stopwatch                  6.4.8  6.4.8  Provides a way to profile code
symfony/twig-bundle                6.4.8  6.4.8  Provides a tight integration of Twig into the Symfony full-stack framework
symfony/uid                        6.4.8  6.4.8  Provides an object-oriented API to generate and represent UIDs
symfony/validator                  6.4.10 6.4.10 Provides tools to validate values
symfony/web-profiler-bundle        6.4.10 6.4.10 Provides a development tool that gives detailed information about the execution of any request
symfony/yaml                       6.4.8  6.4.8  Loads and dumps YAML files

Transitive dependencies not required in composer.json:
symfony/cache                      6.4.10 6.4.10 Provides extended PSR-6, PSR-16 (and tags) implementations
symfony/cache-contracts            3.5.0  3.5.0  Generic abstractions related to caching
symfony/clock                      6.4.8  6.4.8  Decouples applications from the system clock
symfony/config                     6.4.8  6.4.8  Helps you find, load, combine, autofill and validate configuration values of any kind
symfony/dependency-injection       6.4.10 6.4.10 Allows you to standardize and centralize the way objects are constructed in your application
symfony/deprecation-contracts      3.5.0  3.5.0  A generic function and convention to trigger deprecation notices
symfony/doctrine-bridge            6.4.10 6.4.10 Provides integration for Doctrine with various Symfony components
symfony/error-handler              6.4.10 6.4.10 Provides tools to manage errors and ease debugging PHP code
symfony/event-dispatcher           6.4.8  6.4.8  Provides tools that allow your application components to communicate with each other by dispatching events and listening to them
symfony/event-dispatcher-contracts 3.5.0  3.5.0  Generic abstractions related to dispatching event
symfony/expression-language        6.4.8  6.4.8  Provides an engine that can compile and evaluate expressions
symfony/filesystem                 6.4.9  6.4.9  Provides basic utilities for the filesystem
symfony/finder                     6.4.10 6.4.10 Finds files and directories via an intuitive fluent interface
symfony/form                       6.4.10 6.4.10 Allows to easily create, process and reuse HTML forms
symfony/http-client                6.4.10 6.4.10 Provides powerful methods to fetch HTTP resources synchronously or asynchronously
symfony/http-client-contracts      3.5.0  3.5.0  Generic abstractions related to HTTP clients
symfony/http-foundation            6.4.10 6.4.10 Defines an object-oriented layer for the HTTP specification
symfony/http-kernel                6.4.10 6.4.10 Provides a structured process for converting a Request into a Response
symfony/intl                       6.4.8  6.4.8  Provides access to the localization data of the ICU library
symfony/mime                       6.4.9  6.4.9  Allows manipulating MIME messages
symfony/monolog-bridge             6.4.8  6.4.8  Provides integration for Monolog with various Symfony components
symfony/options-resolver           6.4.8  6.4.8  Provides an improved replacement for the array_replace PHP function
symfony/orm-pack                   2.4.1  2.4.1  A pack for the Doctrine ORM
symfony/password-hasher            6.4.8  6.4.8  Provides password hashing utilities
symfony/polyfill-intl-grapheme     1.30.0 1.30.0 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu          1.30.0 1.30.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          1.30.0 1.30.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer   1.30.0 1.30.0 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring          1.30.0 1.30.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php73             1.30.0 1.30.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-php80             1.30.0 1.30.0 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/polyfill-php83             1.30.0 1.30.0 Symfony polyfill backporting some PHP 8.3+ features to lower PHP versions
symfony/polyfill-uuid              1.30.0 1.30.0 Symfony polyfill for uuid functions
symfony/process                    6.4.8  6.4.8  Executes commands in sub-processes
symfony/property-access            6.4.8  6.4.8  Provides functions to read and write from/to an object or array using a simple string notation
symfony/property-info              6.4.10 6.4.10 Extracts information about PHP class' properties using metadata of popular sources
symfony/routing                    6.4.10 6.4.10 Maps an HTTP request to a set of configuration variables
symfony/security-acl               3.3.3  3.3.3  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            6.4.10 6.4.10 Provides a tight integration of the Security component into the Symfony full-stack framework
symfony/security-core              6.4.10 6.4.10 Symfony Security Component - Core Library
symfony/security-csrf              6.4.8  6.4.8  Symfony Security Component - CSRF Library
symfony/security-http              6.4.9  6.4.9  Symfony Security Component - HTTP Integration
symfony/serializer                 6.4.10 6.4.10 Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON.
symfony/service-contracts          3.5.0  3.5.0  Generic abstractions related to writing services
symfony/string                     6.4.10 6.4.10 Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way
symfony/translation                6.4.10 6.4.10 Provides tools to internationalize your application
symfony/translation-contracts      3.5.0  3.5.0  Generic abstractions related to translation
symfony/twig-bridge                6.4.9  6.4.9  Provides integration for Twig with various Symfony components
symfony/var-dumper                 6.4.10 6.4.10 Provides mechanisms for walking through any arbitrary PHP variable
symfony/var-exporter               6.4.9  6.4.9  Allows exporting any serializable PHP data structure to plain PHP code

PHP version

PHP 8.1.29 (cli) (built: Jul  2 2024 03:17:03) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.29, Copyright (c) Zend Technologies

Subject

When creating a custom provider for pictures, an admin thumbnail is not generated.
I will show an expanded file as an example, it was small before and also did not work.
You can check the creation both in the file directory and through the admin: there will be no preview in the media list

Steps to reproduce

Created src/Provider/ImageProvider.php:

<?php
namespace App\Provider;

use Gaufrette\File as GaufretteFile;
use Gaufrette\Filesystem;
use Imagine\Image\ImagineInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\MediaBundle\CDN\CDNInterface;
use Sonata\MediaBundle\Filesystem\Local;
use Sonata\MediaBundle\Generator\GeneratorInterface;
use Sonata\MediaBundle\Metadata\MetadataBuilderInterface;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\BaseProvider;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
use Sonata\MediaBundle\Thumbnail\ThumbnailInterface;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\UploadException;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;

class ImageProvider extends BaseProvider
{
    private $allowedExtensions = [];
    private $allowedMimeTypes = [];
    private $metadata;
    /** @var string */
    private $svgContentType = 'image/svg';

    public function __construct(
        string $name,
        Filesystem $filesystem,
        CDNInterface $cdn,
        GeneratorInterface $pathGenerator,
        ThumbnailInterface $thumbnail,
        array $allowedExtensions,
        array $allowedMimeTypes,
        private ImagineInterface $imagineAdapter,
        ?MetadataBuilderInterface $metadata = null
    ) {
        parent::__construct($name, $filesystem, $cdn, $pathGenerator, $thumbnail);
        $this->allowedExtensions = $allowedExtensions;
        $this->allowedMimeTypes = $allowedMimeTypes;
        $this->metadata = $metadata;
    }

    protected function generateReferenceName(MediaInterface $media): string
    {
        return $this->generateMediaUniqId($media).'.'.$media->getBinaryContent()->guessExtension();
    }

    protected function generateMediaUniqId(MediaInterface $media): string
    {
        return sha1($media->getName().uniqid().random_int(11111, 99999));
    }

    /**
     * {@inheritdoc}
     */
    public function generatePublicUrl(MediaInterface $media, $format): string
    {
        if (MediaProviderInterface::FORMAT_REFERENCE === $format || $media->getContentType() === $this->svgContentType) {
            $path = $this->getReferenceImage($media);
        } else {
            $path = $this->thumbnail->generatePublicUrl($this, $media, $format);
        }

        // if $path is already an url, no further action is required
        if (null !== parse_url($path, \PHP_URL_SCHEME)) {
            return $path;
        }

        return $this->getCdn()->getPath($path, $media->getCdnIsFlushable());
    }

    public function generatePrivateUrl(MediaInterface $media, string $format): string
    {
        return $this->thumbnail->generatePrivateUrl($this, $media, $format);
    }

    protected function doTransform(MediaInterface $media): void
    {
        if ([] === $this->allowedExtensions) {
            $media->setProviderStatus(MediaInterface::STATUS_ERROR);

            throw new UploadException('There are no allowed extensions for this image.');
        }

        if ([] === $this->allowedMimeTypes) {
            $media->setProviderStatus(MediaInterface::STATUS_ERROR);

            throw new UploadException('There are no allowed mime types for this image.');
        }
        $this->fixBinaryContent($media);
        $this->fixFilename($media);

        $binaryContent = $media->getBinaryContent();


        // this is the name used to store the file
        if (null === $media->getProviderReference()
            || MediaInterface::MISSING_BINARY_REFERENCE === $media->getProviderReference()
        ) {
            $media->setProviderReference($this->generateReferenceName($media));
        }

        if ($binaryContent instanceof UploadedFile) {
            $extension = $binaryContent->getClientOriginalExtension();
        } elseif ($binaryContent instanceof File) {
            $extension = $binaryContent->getExtension();
        } else {
            // Should not happen, FileProvider should throw an exception in that case
            return;
        }

        $extension = '' !== $extension ? $extension : $binaryContent->guessExtension();

        if (null === $extension || !\in_array(strtolower($extension), $this->allowedExtensions, true)) {
            $media->setProviderStatus(MediaInterface::STATUS_ERROR);

            throw new UploadException(sprintf(
                'The image extension "%s" is not one of the allowed (%s).',
                $extension ?? '',
                '"'.implode('", "', $this->allowedExtensions).'"'
            ));
        }

        $mimeType = $binaryContent->getMimeType();

        if (null === $mimeType || !\in_array(strtolower($mimeType), $this->allowedMimeTypes, true)) {
            $media->setProviderStatus(MediaInterface::STATUS_ERROR);

            throw new UploadException(sprintf(
                'The image mime type "%s" is not one of the allowed (%s).',
                $mimeType ?? '',
                '"'.implode('", "', $this->allowedMimeTypes).'"'
            ));
        }

        try {
            $image = $this->imagineAdapter->open($binaryContent->getPathname());
        } catch (\RuntimeException) {
            $media->setProviderStatus(MediaInterface::STATUS_ERROR);

            return;
        }

        $size = $image->getSize();

        $media->setWidth($size->getWidth());
        $media->setHeight($size->getHeight());

        $media->setProviderStatus(MediaInterface::STATUS_OK);
    }

    public function getReferenceFile(MediaInterface $media): GaufretteFile
    {
        return $this->getFilesystem()->get($this->getReferenceImage($media), true);
    }

    public function getReferenceImage(MediaInterface $media): string
    {
        $providerReference = $media->getProviderReference();

        if (null === $providerReference) {
            throw new \InvalidArgumentException('Unable to generate reference image for media without provider reference.');
        }

        return sprintf('%s/%s', $this->generatePath($media), $providerReference);
    }

    public function postUpdate(MediaInterface $media): void
    {
        if (!$media->getBinaryContent() instanceof \SplFileInfo) {
            return;
        }

        // Delete the current file from the FS
        $oldMedia = clone $media;
        // if no previous reference is provided, it prevents
        // Filesystem from trying to remove a directory
        if (null !== $media->getPreviousProviderReference()) {
            $oldMedia->setProviderReference($media->getPreviousProviderReference());

            $path = $this->getReferenceImage($oldMedia);

            if ($this->getFilesystem()->has($path)) {
                $this->getFilesystem()->delete($path);
            }
        }

        $this->fixBinaryContent($media);

        $this->setFileContents($media);

        $this->generateThumbnails($media);

        $media->resetBinaryContent();
    }

    /**
     * Set the file contents for an image.
     */
    protected function setFileContents(MediaInterface $media, ?string $contents = null): void
    {
        $providerReference = $media->getProviderReference();

        if (null === $providerReference) {
            throw new \RuntimeException(sprintf(
                'Unable to generate path to file without provider reference for media "%s".',
                (string) $media
            ));
        }

        $file = $this->getFilesystem()->get(
            sprintf('%s/%s', $this->generatePath($media), $providerReference),
            true
        );

        $metadata = null !== $this->metadata ? $this->metadata->get($media, $file->getName()) : [];

        if (null !== $contents) {
            $file->setContent($contents, $metadata);

            return;
        }

        $binaryContent = $media->getBinaryContent();
        if ($binaryContent instanceof File) {
            $path = false !== $binaryContent->getRealPath() ? $binaryContent->getRealPath() : $binaryContent->getPathname();
            $fileContents = file_get_contents($path);

            if (false === $fileContents) {
                throw new \RuntimeException(sprintf('Unable to get file contents for media %s', $media->getId() ?? ''));
            }

            $file->setContent($fileContents, $metadata);

            return;
        }
    }

    protected function fixBinaryContent(MediaInterface $media): void
    {
        if (null === $media->getBinaryContent() || $media->getBinaryContent() instanceof File) {
            return;
        }

        // if the binary content is a filename => convert to a valid File
        if (!is_file($media->getBinaryContent())) {
            throw new \RuntimeException(sprintf('The file does not exist: %s', $media->getBinaryContent()));
        }

        $binaryContent = new File($media->getBinaryContent());
        $media->setBinaryContent($binaryContent);
    }

    public function buildCreateForm(FormMapper $form): void
    {
        $form->add('binaryContent', FileType::class, [
            'constraints' => [
                new NotBlank(),
                new NotNull(),
            ],
        ]);
    }

    public function buildEditForm(FormMapper $form): void
    {
        $form->add('name');
        $form->add('enabled', null, ['required' => false]);
        $form->add('authorName');
        $form->add('cdnIsFlushable');
        $form->add('description');
        $form->add('copyright');
        $form->add('binaryContent', FileType::class, ['required' => false]);
    }

    public function postPersist(MediaInterface $media): void
    {
        if (null === $media->getBinaryContent()) {
            return;
        }

        $this->setFileContents($media);

        $this->generateThumbnails($media);

        $media->resetBinaryContent();
    }

    public function getHelperProperties(MediaInterface $media, string $format, array $options = []): array
    {
        if (isset($options['srcset'], $options['picture'])) {
            throw new \LogicException("The 'srcset' and 'picture' options must not be used simultaneously.");
        }

        if (MediaProviderInterface::FORMAT_REFERENCE === $format) {
            $box = $media->getBox();
        } else {
            $resizerFormat = $this->getFormat($format);
            if (false === $resizerFormat) {
                throw new \RuntimeException(sprintf('The image format "%s" is not defined.
                        Is the format registered in your ``sonata_media`` configuration?', $format));
            }

            if (null === $this->resizer) {
                throw new \RuntimeException('Resizer not set on the image provider.');
            }

            $box = $this->resizer->getBox($media, $resizerFormat);
        }

        $mediaWidth = $box->getWidth();

        $params = [
            'alt' => $media->getDescription() ?? $media->getName(),
            'title' => $media->getName(),
            'src' => $this->generatePublicUrl($media, $format),
            'width' => $mediaWidth,
            'height' => $box->getHeight(),
        ];

        if (isset($options['picture'])) {
            $pictureParams = [];
            foreach ($options['picture'] as $key => $pictureFormat) {
                $formatName = $this->getFormatName($media, $pictureFormat);
                $settings = $this->getFormat($formatName);

                if (false === $settings) {
                    throw new \RuntimeException(sprintf('The image format "%s" is not defined.
                            Is the format registered in your ``sonata_media`` configuration?', $formatName));
                }

                if (null === $this->resizer) {
                    throw new \RuntimeException('Resizer not set on the image provider.');
                }

                $src = $this->generatePublicUrl($media, $formatName);
                $mediaQuery = \is_string($key)
                    ? $key
                    : sprintf('(max-width: %dpx)', $this->resizer->getBox($media, $settings)->getWidth());

                $pictureParams['source'][] = ['media' => $mediaQuery, 'srcset' => $src];
            }

            unset($options['picture']);
            $pictureParams['img'] = array_merge($params, $options);
            $params = ['picture' => $pictureParams];
        } else {
            $srcSetFormats = $this->getFormats();

            if (isset($options['srcset']) && \is_array($options['srcset'])) {
                $srcSetFormats = [];
                foreach ($options['srcset'] as $srcSetFormat) {
                    $formatName = $this->getFormatName($media, $srcSetFormat);
                    $settings = $this->getFormat($formatName);

                    if (false === $settings) {
                        throw new \RuntimeException(sprintf('The image format "%s" is not defined.
                                Is the format registered in your ``sonata_media`` configuration?', $formatName));
                    }

                    $srcSetFormats[$formatName] = $settings;
                }
                unset($options['srcset']);

                // Make sure the requested format is also in the srcSetFormats
                if (!isset($srcSetFormats[$format])) {
                    $settings = $this->getFormat($format);

                    if (false === $settings) {
                        throw new \RuntimeException(sprintf('The image format "%s" is not defined.
                                Is the format registered in your ``sonata_media`` configuration?', $format));
                    }

                    $srcSetFormats[$format] = $settings;
                }
            }

            if (!isset($options['srcset'])) {
                $srcSet = [];

                foreach ($srcSetFormats as $providerFormat => $settings) {
                    $context = $media->getContext();

                    // Check if format belongs to the current media's context
                    if (null !== $context && str_starts_with($providerFormat, $context)) {
                        if (null === $this->resizer) {
                            throw new \RuntimeException('Resizer not set on the image provider.');
                        }

                        $width = $this->resizer->getBox($media, $settings)->getWidth();

                        $srcSet[] = sprintf('%s %dw', $this->generatePublicUrl($media, $providerFormat), $width);
                    }
                }

                // The reference format is not in the formats list
                $srcSet[] = sprintf(
                    '%s %dw',
                    $this->generatePublicUrl($media, MediaProviderInterface::FORMAT_REFERENCE),
                    $media->getBox()->getWidth()
                );

                $params['srcset'] = implode(', ', $srcSet);
            }

            $params['sizes'] = sprintf('(max-width: %1$dpx) 100vw, %1$dpx', $mediaWidth);
        }

        return array_merge($params, $options);
    }

    public function getDownloadResponse(MediaInterface $media, string $format, string $mode, array $headers = []): Response
    {
        // build the default headers
        $headers = array_merge([
            'Content-Type' => $media->getContentType(),
            'Content-Disposition' => sprintf('attachment; filename="%s"', $media->getMetadataValue('filename')),
        ], $headers);

        if (!\in_array($mode, ['http', 'X-Sendfile', 'X-Accel-Redirect'], true)) {
            throw new \RuntimeException('Invalid mode provided');
        }

        if ('http' === $mode) {
            if (MediaProviderInterface::FORMAT_REFERENCE === $format) {
                $file = $this->getReferenceFile($media);
            } else {
                $file = $this->getFilesystem()->get($this->generatePrivateUrl($media, $format));
            }

            return new StreamedResponse(static function () use ($file): void {
                echo $file->getContent();
            }, 200, $headers);
        }

        $adapter = $this->getFilesystem()->getAdapter();

        if (!$adapter instanceof Local) {
            throw new \RuntimeException(sprintf('Cannot use X-Sendfile or X-Accel-Redirect with non %s.', Local::class));
        }

        $directory = $adapter->getDirectory();

        if (false === $directory) {
            throw new \RuntimeException('Cannot retrieve directory from the adapter.');
        }

        return new BinaryFileResponse(
            sprintf('%s/%s', $directory, $this->generatePrivateUrl($media, $format)),
            200,
            $headers
        );
    }

    public function buildMediaType(FormBuilderInterface $formBuilder): void
    {
        $formBuilder->add('binaryContent', FileType::class, [
            'required' => false,
            'label' => 'widget_label_binary_content',
        ]);
    }

    public function updateMetadata(MediaInterface $media, bool $force = true): void
    {
        try {
            if (!$media->getBinaryContent() instanceof \SplFileInfo) {
                // this is now optimized at all!!!
                $path = tempnam(sys_get_temp_dir(), 'sonata_update_metadata');

                if (false === $path) {
                    throw new \LogicException(sprintf('Unable to update metadata for media %s.', $media->getId() ?? ''));
                }

                $fileObject = new \SplFileObject($path, 'w');
                $fileObject->fwrite($this->getReferenceFile($media)->getContent());
            } else {
                $fileObject = $media->getBinaryContent();
            }

            $image = $this->imagineAdapter->open($fileObject->getPathname());
            $size = $image->getSize();

            $media->setSize($fileObject->getSize());
            $media->setWidth($size->getWidth());
            $media->setHeight($size->getHeight());
        } catch (\LogicException) {
            $media->setProviderStatus(MediaInterface::STATUS_ERROR);

            $media->setSize(0);
            $media->setWidth(0);
            $media->setHeight(0);
        }
    }

    /**
     * @throws \RuntimeException
     */
    protected function fixFilename(MediaInterface $media): void
    {
        if ($media->getBinaryContent() instanceof UploadedFile) {
            $media->setName($media->getName() ?? $media->getBinaryContent()->getClientOriginalName());
            $media->setMetadataValue('filename', $media->getBinaryContent()->getClientOriginalName());
        } elseif ($media->getBinaryContent() instanceof File) {
            $media->setName($media->getName() ?? $media->getBinaryContent()->getBasename());
            $media->setMetadataValue('filename', $media->getBinaryContent()->getBasename());
        }

        // This is the original name
        if (null === $media->getName()) {
            throw new \RuntimeException('Please define a valid media\'s name');
        }
    }
}

Edit services.yaml:

    admin.provider.image:
        class: App\Provider\ImageProvider
        tags:
            - { name: sonata.media.provider }
        arguments:
            - "admin.provider.image"
            - "@sonata.media.filesystem.local"
            - "@sonata.media.cdn.server"
            - "@sonata.media.generator.default"
            - "@sonata.media.thumbnail.format"
            - ['gif', 'svg', 'jpeg', 'jpg', 'png']
            - ['image/svg+xml', 'image/gif', 'image/jpeg', 'image/jpg', 'image/png']
            - '@liip_imagine'
            - '@sonata.media.metadata.proxy'
        calls:
            - [ setTemplates, [{ helper_thumbnail : 'admin/provider/thumbnail_image.html.twig', helper_view : 'admin/provider/view_image.html.twig' }]]
        public: true

Edit sonata_media.yaml:

        category_image:
            providers:
                - admin.provider.image

            formats:
                ~

Expected results

Creating
public/upload/media/category_image/0001/01/8d8142d23ee2982c4e1d50974de6ed0bdf51c8ae.png
public/upload/media/category_image/0001/01/thumb_1_admin.png

Actual results

Only one file is created, without the administrative one.
public/upload/media/category_image/0001/01/8d8142d23ee2982c4e1d50974de6ed0bdf51c8ae.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant