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).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* 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
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-08-27 22:24:56 +02:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-08-27 22:24:56 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\EntityListeners;
|
|
|
|
|
|
|
|
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;
|
2019-08-27 22:24:56 +02:00
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs;
|
|
|
|
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
|
|
|
|
* used any more. This can happens after an attachment is delteted or the path is changed.
|
|
|
|
*/
|
|
|
|
class AttachmentDeleteListener
|
|
|
|
{
|
2022-09-18 22:59:31 +02:00
|
|
|
protected AttachmentReverseSearch $attachmentReverseSearch;
|
|
|
|
protected AttachmentManager $attachmentHelper;
|
|
|
|
protected AttachmentPathResolver $pathResolver;
|
2019-08-27 22:24:56 +02:00
|
|
|
|
2019-10-19 23:29:51 +02:00
|
|
|
public function __construct(AttachmentReverseSearch $attachmentReverseSearch, AttachmentManager $attachmentHelper, AttachmentPathResolver $pathResolver)
|
2019-08-27 22:24:56 +02:00
|
|
|
{
|
|
|
|
$this->attachmentReverseSearch = $attachmentReverseSearch;
|
|
|
|
$this->attachmentHelper = $attachmentHelper;
|
2019-09-28 16:06:37 +02:00
|
|
|
$this->pathResolver = $pathResolver;
|
2019-08-27 22:24:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the file associated with the attachment, if the file associated with the attachment changes.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
{
|
|
|
|
if ($event->hasChangedField('path')) {
|
2020-03-30 16:29:31 +02:00
|
|
|
$old_path = $event->getOldValue('path');
|
|
|
|
|
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).
|
2020-08-21 21:36:22 +02:00
|
|
|
*
|
2020-05-13 20:46:13 +02:00
|
|
|
* @ORM\PreRemove()
|
|
|
|
*/
|
|
|
|
public function preRemoveHandler(Attachment $attachment, LifecycleEventArgs $event): void
|
|
|
|
{
|
|
|
|
//Ensure that the attachment that will be deleted, is not used as preview picture anymore...
|
|
|
|
$attachment_holder = $attachment->getElement();
|
|
|
|
|
2020-08-21 21:36:22 +02:00
|
|
|
if (null === $attachment_holder) {
|
2020-05-13 20:46:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//... Otherwise remove it as preview picture
|
|
|
|
if ($attachment_holder->getMasterPictureAttachment() === $attachment) {
|
|
|
|
$attachment_holder->setMasterPictureAttachment(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 22:24:56 +02:00
|
|
|
/**
|
|
|
|
* Removes the file associated with the attachment, after the attachment was deleted.
|
|
|
|
*
|
|
|
|
* @PostRemove
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
public function postRemoveHandler(Attachment $attachment, LifecycleEventArgs $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.
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null !== $file) {
|
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
|
|
|
}
|