2019-08-27 22:24:56 +02:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-08-27 22:24:56 +02:00
|
|
|
namespace App\EntityListeners;
|
|
|
|
|
2023-06-11 14:55:06 +02:00
|
|
|
use App\Entity\Attachments\AttachmentContainingDBElement;
|
2019-08-27 22:24:56 +02:00
|
|
|
use App\Entity\Attachments\Attachment;
|
2019-10-19 23:29:51 +02:00
|
|
|
use App\Services\Attachments\AttachmentManager;
|
2019-09-28 16:06:37 +02:00
|
|
|
use App\Services\Attachments\AttachmentPathResolver;
|
2019-11-09 00:47:20 +01:00
|
|
|
use App\Services\Attachments\AttachmentReverseSearch;
|
2023-01-24 23:47:25 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Doctrine\ORM\Event\PostRemoveEventArgs;
|
|
|
|
use Doctrine\ORM\Event\PreRemoveEventArgs;
|
2019-08-27 22:24:56 +02:00
|
|
|
use Doctrine\ORM\Event\PreUpdateEventArgs;
|
2020-05-13 20:46:13 +02:00
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
2019-08-27 22:24:56 +02:00
|
|
|
use Doctrine\ORM\Mapping\PostRemove;
|
|
|
|
use Doctrine\ORM\Mapping\PreUpdate;
|
2020-01-05 22:49:00 +01:00
|
|
|
use SplFileInfo;
|
2019-08-27 22:24:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This listener watches for changes on attachments and deletes the files associated with an attachment, that are not
|
2023-04-15 23:14:53 +02:00
|
|
|
* used anymore. This can happen after an attachment is deleted or the path is changed.
|
2019-08-27 22:24:56 +02:00
|
|
|
*/
|
|
|
|
class AttachmentDeleteListener
|
|
|
|
{
|
2023-06-11 14:15:46 +02:00
|
|
|
public function __construct(protected AttachmentReverseSearch $attachmentReverseSearch, protected AttachmentManager $attachmentHelper, protected AttachmentPathResolver $pathResolver)
|
2019-08-27 22:24:56 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the file associated with the attachment, if the file associated with the attachment changes.
|
|
|
|
*/
|
2023-05-28 01:33:45 +02:00
|
|
|
#[PreUpdate]
|
2020-01-05 15:46:58 +01:00
|
|
|
public function preUpdateHandler(Attachment $attachment, PreUpdateEventArgs $event): void
|
2019-08-27 22:24:56 +02:00
|
|
|
{
|
2025-02-22 17:29:14 +01:00
|
|
|
if ($event->hasChangedField('internal_path')) {
|
|
|
|
$old_path = $event->getOldValue('internal_path');
|
2020-03-30 16:29:31 +02:00
|
|
|
|
2019-09-25 16:03:22 +02:00
|
|
|
//Dont delete file if the attachment uses a builtin ressource:
|
2020-03-30 16:29:31 +02:00
|
|
|
if (Attachment::checkIfBuiltin($old_path)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$real_path = $this->pathResolver->placeholderToRealPath($old_path);
|
|
|
|
|
|
|
|
//If the attachment does not point to a valid file, ignore it!
|
2020-04-10 13:05:08 +02:00
|
|
|
if (null === $real_path) {
|
2019-09-25 16:03:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-30 16:29:31 +02:00
|
|
|
$file = new SplFileInfo($real_path);
|
2019-08-27 22:24:56 +02:00
|
|
|
$this->attachmentReverseSearch->deleteIfNotUsed($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:46:13 +02:00
|
|
|
/**
|
|
|
|
* Ensure that attachments are not used in preview, so that they can be deleted (without integrity violation).
|
|
|
|
*/
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\PreRemove]
|
2023-01-24 23:47:25 +01:00
|
|
|
public function preRemoveHandler(Attachment $attachment, PreRemoveEventArgs $event): void
|
2020-05-13 20:46:13 +02:00
|
|
|
{
|
|
|
|
//Ensure that the attachment that will be deleted, is not used as preview picture anymore...
|
|
|
|
$attachment_holder = $attachment->getElement();
|
|
|
|
|
2023-06-11 14:55:06 +02:00
|
|
|
if (!$attachment_holder instanceof AttachmentContainingDBElement) {
|
2020-05-13 20:46:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//... Otherwise remove it as preview picture
|
|
|
|
if ($attachment_holder->getMasterPictureAttachment() === $attachment) {
|
|
|
|
$attachment_holder->setMasterPictureAttachment(null);
|
2023-01-24 23:47:25 +01:00
|
|
|
|
|
|
|
//Recalculate the changes on the attachment holder, so the master picture change is really written to DB
|
|
|
|
$em = $event->getObjectManager();
|
|
|
|
if (!$em instanceof EntityManagerInterface) {
|
|
|
|
throw new \RuntimeException('Invalid EntityManagerInterface!');
|
|
|
|
}
|
2023-06-11 14:15:46 +02:00
|
|
|
$classMetadata = $em->getClassMetadata($attachment_holder::class);
|
2023-01-24 23:47:25 +01:00
|
|
|
$em->getUnitOfWork()->computeChangeSet($classMetadata, $attachment_holder);
|
2020-05-13 20:46:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 22:24:56 +02:00
|
|
|
/**
|
|
|
|
* Removes the file associated with the attachment, after the attachment was deleted.
|
|
|
|
*/
|
2023-05-28 01:33:45 +02:00
|
|
|
#[PostRemove]
|
2023-01-24 23:47:25 +01:00
|
|
|
public function postRemoveHandler(Attachment $attachment, PostRemoveEventArgs $event): void
|
2019-08-27 22:24:56 +02:00
|
|
|
{
|
2019-09-25 16:03:22 +02:00
|
|
|
//Dont delete file if the attachment uses a builtin ressource:
|
2019-10-19 19:53:37 +02:00
|
|
|
if ($attachment->isBuiltIn()) {
|
2019-09-25 16:03:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-27 22:24:56 +02:00
|
|
|
$file = $this->attachmentHelper->attachmentToFile($attachment);
|
|
|
|
//Only delete if the attachment has a valid file.
|
2023-06-11 14:15:46 +02:00
|
|
|
if ($file instanceof \SplFileInfo) {
|
2020-06-13 21:55:20 +02:00
|
|
|
/* The original file has already been removed, so we have to decrease the threshold to zero,
|
|
|
|
as any remaining attachment depends on this attachment, and we must not delete this file! */
|
|
|
|
$this->attachmentReverseSearch->deleteIfNotUsed($file, 0);
|
2019-08-27 22:24:56 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|