VichUploaderBundle
VichUploaderBundle copied to clipboard
add new feature - Inject URI on load
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]
I'm not sure to get your use case
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
Can't you use flysystem (or other available providers)?
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
Is the code you proposed above a working example?
Ya it's working..we need to add corresponding services like inject on load
So, can't you just use it?
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
Feel free to propose a PR. You must include tests and documentation. Thank you.