VichUploaderBundle icon indicating copy to clipboard operation
VichUploaderBundle copied to clipboard

add new feature - Inject URI on load

Open gomcodoctor opened this issue 3 years ago • 9 comments

As modern apps are moving toward headless using API so there should be Inject URI on load like Inject file on load

here is code sample

use Vich\UploaderBundle\Adapter\AdapterInterface;
use Vich\UploaderBundle\EventListener\Doctrine\BaseListener;
use Vich\UploaderBundle\Handler\UploadHandler;
use Vich\UploaderBundle\Metadata\MetadataReader;
use Vich\UploaderBundle\Storage\StorageInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;

class VichFeedLoadListener extends BaseListener
{

    /**
     * @var StorageInterface
     */
    private StorageInterface $storage;

    public function __construct(string $mapping,
                                AdapterInterface $adapter,
                                MetadataReader $metadata,
                                UploadHandler $handler,
                                StorageInterface $storage)
    {
        parent::__construct($mapping,$adapter, $metadata, $handler);
        $this->storage = $storage;
    }



    /**
     * The events the listener is subscribed to.
     *
     * @return array The array of events
     */
    public function getSubscribedEvents(): array
    {
        return [
            'postLoad',
        ];
    }

    public function postLoad(LifecycleEventArgs $event)
    {
        $object = $this->adapter->getObjectFromArgs($event);

        if (!$this->isUploadable($object)) {
            return;
        }

        foreach ($this->getUploadableFields($object) as $field) {
            $setter = sprintf('set%sUri', ucfirst($field));
            if(method_exists($object, $setter)){
                $object->$setter($this->storage->resolveUri($object, $field));
            }
        }
    }

}

and in Bundle extension

'inject_url_on_load' => ['name' => 'inject_url', 'priority' => 0]

gomcodoctor avatar Aug 10 '20 09:08 gomcodoctor

I'm not sure to get your use case

garak avatar Aug 10 '20 13:08 garak

Suppose there is a file of 100 mb, we are using headless system using api plateform, we can not use inject on load because json size will become too big, there should be a way via instead of injecting file on entity we should inject URI of file, this way end client click on link and download file

gomcodoctor avatar Aug 10 '20 15:08 gomcodoctor

Can't you use flysystem (or other available providers)?

garak avatar Aug 10 '20 15:08 garak

I think provider does not matter in this case.. my point is when using api, we can not inject whole file ( because of size ), we should only inject URI of file

gomcodoctor avatar Aug 10 '20 15:08 gomcodoctor

Is the code you proposed above a working example?

garak avatar Aug 12 '20 13:08 garak

Ya it's working..we need to add corresponding services like inject on load

gomcodoctor avatar Aug 12 '20 14:08 gomcodoctor

So, can't you just use it?

garak avatar Aug 12 '20 15:08 garak

I am already using it in my project.. it was just suggestion for additional feature.. Because api demand is rising day by day..I can create PR if you are willing to merge

gomcodoctor avatar Aug 12 '20 15:08 gomcodoctor

Feel free to propose a PR. You must include tests and documentation. Thank you.

garak avatar Aug 12 '20 15:08 garak