attachmentReverseSearch = $attachmentReverseSearch; $this->attachmentHelper = $attachmentHelper; $this->pathResolver = $pathResolver; } /** * Removes the file associated with the attachment, if the file associated with the attachment changes. * * @PreUpdate */ public function preUpdateHandler(Attachment $attachment, PreUpdateEventArgs $event): void { if ($event->hasChangedField('path')) { //Dont delete file if the attachment uses a builtin ressource: if (Attachment::checkIfBuiltin($event->getOldValue('path'))) { return; } $file = new SplFileInfo($this->pathResolver->placeholderToRealPath($event->getOldValue('path'))); $this->attachmentReverseSearch->deleteIfNotUsed($file); } } /** * Removes the file associated with the attachment, after the attachment was deleted. * * @PostRemove */ public function postRemoveHandler(Attachment $attachment, LifecycleEventArgs $event): void { //Dont delete file if the attachment uses a builtin ressource: if ($attachment->isBuiltIn()) { return; } $file = $this->attachmentHelper->attachmentToFile($attachment); //Only delete if the attachment has a valid file. if (null !== $file) { $this->attachmentReverseSearch->deleteIfNotUsed($file); } } }