mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-06 00:14:36 +02:00
Delete the file associated with an attachment after an attachment is delted or changed.
This commit is contained in:
parent
87527dfdc6
commit
6b87823d5e
5 changed files with 135 additions and 17 deletions
|
@ -42,6 +42,11 @@ services:
|
|||
tags:
|
||||
- { name: doctrine.orm.entity_listener }
|
||||
|
||||
attachment_delete_listener:
|
||||
class: App\EntityListeners\AttachmentDeleteListener
|
||||
tags:
|
||||
- name: doctrine.orm.entity_listener
|
||||
|
||||
|
||||
App\Command\UpdateExchangeRatesCommand:
|
||||
arguments:
|
||||
|
|
|
@ -37,6 +37,7 @@ use Symfony\Component\Validator\Constraints as Assert;
|
|||
* @ORM\InheritanceType("SINGLE_TABLE")
|
||||
* @ORM\DiscriminatorColumn(name="class_name", type="string")
|
||||
* @ORM\DiscriminatorMap({"PartDB\Part" = "PartAttachment", "Part" = "PartAttachment"})
|
||||
* @ORM\EntityListeners({"App\EntityListeners\AttachmentDeleteListener"})
|
||||
*
|
||||
*/
|
||||
abstract class Attachment extends NamedDBElement
|
||||
|
@ -124,17 +125,6 @@ abstract class Attachment extends NamedDBElement
|
|||
return $this->element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the file in this attachement is existing. This works for files on the HDD, and for URLs
|
||||
* (it's not checked if the ressource behind the URL is really existing).
|
||||
*
|
||||
* @return bool True if the file is existing.
|
||||
*/
|
||||
public function isFileExisting(): bool
|
||||
{
|
||||
return file_exists($this->getPath()) || static::isURL($this->getPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* The URL to the external file.
|
||||
* Returns null, if the file is not external.
|
||||
|
@ -273,11 +263,11 @@ abstract class Attachment extends NamedDBElement
|
|||
{
|
||||
//Only set if the URL is not empty
|
||||
if (!empty($url)) {
|
||||
$this->path = $url;
|
||||
}
|
||||
if (strpos($url, '%BASE%') !== false || strpos($url, '%MEDIA%') !== false) {
|
||||
throw new \InvalidArgumentException("You can not reference internal files via the url field! But nice try!");
|
||||
}
|
||||
|
||||
if (strpos($url, '%BASE%') !== false || strpos($url, '%MEDIA%') !== false) {
|
||||
throw new \InvalidArgumentException("You can not reference internal files via the url field! But nice try!");
|
||||
$this->path = $url;
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
|
91
src/EntityListeners/AttachmentDeleteListener.php
Normal file
91
src/EntityListeners/AttachmentDeleteListener.php
Normal file
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
* part-db version 0.1
|
||||
* Copyright (C) 2005 Christoph Lechner
|
||||
* http://www.cl-projects.de/
|
||||
*
|
||||
* part-db version 0.2+
|
||||
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
|
||||
* http://code.google.com/p/part-db/
|
||||
*
|
||||
* Part-DB Version 0.4+
|
||||
* Copyright (C) 2016 - 2019 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 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;
|
||||
use App\Services\AttachmentHelper;
|
||||
use App\Services\AttachmentReverseSearch;
|
||||
use Doctrine\ORM\Event\LifecycleEventArgs;
|
||||
use Doctrine\ORM\Event\PreUpdateEventArgs;
|
||||
use Doctrine\ORM\Mapping\PostRemove;
|
||||
use Doctrine\ORM\Mapping\PreUpdate;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @package App\EntityListeners
|
||||
*/
|
||||
class AttachmentDeleteListener
|
||||
{
|
||||
protected $attachmentReverseSearch;
|
||||
protected $attachmentHelper;
|
||||
|
||||
public function __construct(AttachmentReverseSearch $attachmentReverseSearch, AttachmentHelper $attachmentHelper)
|
||||
{
|
||||
$this->attachmentReverseSearch = $attachmentReverseSearch;
|
||||
$this->attachmentHelper = $attachmentHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the file associated with the attachment, if the file associated with the attachment changes.
|
||||
* @param Attachment $attachment
|
||||
* @param PreUpdateEventArgs $event
|
||||
*
|
||||
* @PreUpdate
|
||||
*/
|
||||
public function preUpdateHandler(Attachment $attachment, PreUpdateEventArgs $event)
|
||||
{
|
||||
if ($event->hasChangedField('path')) {
|
||||
$file = new \SplFileInfo($this->attachmentHelper->placeholderToRealPath($event->getOldValue('path')));
|
||||
$this->attachmentReverseSearch->deleteIfNotUsed($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the file associated with the attachment, after the attachment was deleted.
|
||||
*
|
||||
* @param Attachment $attachment
|
||||
* @param LifecycleEventArgs $event
|
||||
*
|
||||
* @PostRemove
|
||||
*/
|
||||
public function postRemoveHandler(Attachment $attachment, LifecycleEventArgs $event)
|
||||
{
|
||||
$file = $this->attachmentHelper->attachmentToFile($attachment);
|
||||
//Only delete if the attachment has a valid file.
|
||||
if ($file !== null) {
|
||||
$this->attachmentReverseSearch->deleteIfNotUsed($file);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -34,10 +34,8 @@ namespace App\Services;
|
|||
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use App\Entity\Attachments\PartAttachment;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
|
||||
|
@ -71,6 +69,21 @@ class AttachmentHelper
|
|||
return $this->base_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an SPLFileInfo object representing the file associated with the attachment.
|
||||
* @param Attachment $attachment The attachment for which the file should be generated
|
||||
* @return \SplFileInfo|null The fileinfo for the attachment file. Null, if the attachment is external or has
|
||||
* invalid file.
|
||||
*/
|
||||
public function attachmentToFile(Attachment $attachment) : ?\SplFileInfo
|
||||
{
|
||||
if ($attachment->isExternal() || !$this->isFileExisting($attachment)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new \SplFileInfo($this->toAbsoluteFilePath($attachment));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an relative placeholder filepath (with %MEDIA% or older %BASE%) to an absolute filepath on disk.
|
||||
* @param string $placeholder_path The filepath with placeholder for which the real path should be determined.
|
||||
|
|
|
@ -33,6 +33,7 @@ namespace App\Services;
|
|||
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
|
||||
/**
|
||||
|
@ -66,4 +67,22 @@ class AttachmentReverseSearch
|
|||
return $repo->findBy(['path' => [$relative_path_new, $relative_path_old]]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given file if it is not used by more than $threshold attachments
|
||||
* @param \SplFileInfo $file The file that should be removed
|
||||
* @param int $threshold The threshold used, to determine if a file should be deleted or not.
|
||||
* @return bool True, if the file was delete. False if not.
|
||||
*/
|
||||
public function deleteIfNotUsed(\SplFileInfo $file, int $threshold = 0) : bool
|
||||
{
|
||||
/* When the file is used more then $threshold times, don't delete it */
|
||||
if (count($this->findAttachmentsByFile($file)) > $threshold) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$fs = new Filesystem();
|
||||
$fs->remove($file);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue