mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
Fixed the file deleting mechanism when changing/deleting attachments.
Also the file is also delted from thumbnail cache.
This commit is contained in:
parent
d859d8533d
commit
650ad4b578
2 changed files with 16 additions and 3 deletions
|
@ -90,7 +90,7 @@ class AttachmentDeleteListener
|
|||
public function postRemoveHandler(Attachment $attachment, LifecycleEventArgs $event)
|
||||
{
|
||||
//Dont delete file if the attachment uses a builtin ressource:
|
||||
if (Attachment::checkIfBuiltin($event->getOldValue('path'))) {
|
||||
if ($attachment->isBuiltIn()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,9 @@ namespace App\Services;
|
|||
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use App\Services\Attachments\AttachmentPathResolver;
|
||||
use App\Services\Attachments\AttachmentURLGenerator;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
|
||||
|
@ -45,11 +47,16 @@ class AttachmentReverseSearch
|
|||
{
|
||||
protected $em;
|
||||
protected $pathResolver;
|
||||
protected $cacheManager;
|
||||
protected $attachmentURLGenerator;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, AttachmentPathResolver $pathResolver)
|
||||
public function __construct(EntityManagerInterface $em, AttachmentPathResolver $pathResolver,
|
||||
CacheManager $cacheManager, AttachmentURLGenerator $attachmentURLGenerator)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->pathResolver = $pathResolver;
|
||||
$this->cacheManager = $cacheManager;
|
||||
$this->attachmentURLGenerator = $attachmentURLGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -74,16 +81,22 @@ class AttachmentReverseSearch
|
|||
* @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
|
||||
public function deleteIfNotUsed(\SplFileInfo $file, int $threshold = 1) : bool
|
||||
{
|
||||
|
||||
/* When the file is used more then $threshold times, don't delete it */
|
||||
if (count($this->findAttachmentsByFile($file)) > $threshold) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//Remove file from liip image cache
|
||||
$this->cacheManager->remove($this->attachmentURLGenerator->absolutePathToAssetPath($file->getPathname()));
|
||||
|
||||
$fs = new Filesystem();
|
||||
$fs->remove($file);
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue