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

add new feature - Inject URI on load #1144

Open
gomcodoctor opened this issue Aug 10, 2020 · 9 comments
Open

add new feature - Inject URI on load #1144

gomcodoctor opened this issue Aug 10, 2020 · 9 comments

Comments

@gomcodoctor
Copy link

gomcodoctor commented Aug 10, 2020

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]
@garak
Copy link
Collaborator

garak commented Aug 10, 2020

I'm not sure to get your use case

@gomcodoctor
Copy link
Author

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

@garak
Copy link
Collaborator

garak commented Aug 10, 2020

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

@gomcodoctor
Copy link
Author

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

@garak
Copy link
Collaborator

garak commented Aug 12, 2020

Is the code you proposed above a working example?

@gomcodoctor
Copy link
Author

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

@garak
Copy link
Collaborator

garak commented Aug 12, 2020

So, can't you just use it?

@gomcodoctor
Copy link
Author

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

@garak
Copy link
Collaborator

garak commented Aug 12, 2020

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

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

2 participants