dustin10 / VichUploaderBundle

A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
MIT License
1.83k stars 519 forks source link

Removing a file from within the message queue handler #1422

Closed Jayohhtchen closed 7 months ago

Jayohhtchen commented 8 months ago

I'm really struggling to delete a file using the message queue as outlined in the docs: https://github.com/dustin10/VichUploaderBundle/blob/master/docs/events/howto/remove_files_asynchronously.md

I don't understand how to delete the file in the handler. What does this mean? // delete your file according to your mapping configuration

The entity is removed by this point and all I have is the filename from the message queue. Can someobody help my newb self out. Might be good if there was an example in the docs?

I've tried looking in closed issues, the docs and around the web but not found any examples.

garak commented 8 months ago

The mapping is what allows you to get the path from the entity. I never did it myself, but I suppose you can just try to create a new entity, use the path you already have, get the actual file path from the combination entity + mapping and you're done.

Jayohhtchen commented 8 months ago

Thanks Garak. Would that allow me then to use the remove function in the upload Handler?

garak commented 8 months ago

Thanks Garak. Would that allow me then to use the remove function in the upload Handler?

Yep, that's the intended way

Jayohhtchen commented 7 months ago

I'm not having much luck with this. I tried the new entity approach but not working for me and the file won't delete. I think this is because the remove function triggers off another PRE_REMOVE event and is set to not delete the file, so i'm stuck in an infinite loop of adding new messages to the handler.

It seems like the async method as described in the docs won't actually ever work with the current setup?

With the liipimaginefilters i can just call $this->cacheManager->remove($filename, 'original');
Is there any plan to implement anything like this to just remove by filename and the mapping you wan't to use?

Jayohhtchen commented 7 months ago

Scrap the above, I'm an idiot. I was using the wrong remove function.

Have got it to work using the below:


$filename = $message->getFilename();

$photo = new Photos();
$photo->setImage($filename);

$mapping = $this->PropertyMappingFactory->fromField($photo, 'imageFile');
$this->StorageInterface->remove($photo, $mapping);

Thank you for your help on this @garak

garak commented 7 months ago

Glad you made it. Can we close this?

Jayohhtchen commented 7 months ago

Closing as resolved. Thanks for your help again :)