Fixed the file deleting mechanism when changing/deleting attachments.

Also the file is also delted from thumbnail cache.
This commit is contained in:
Jan Böhmer 2019-10-19 19:53:37 +02:00
parent d859d8533d
commit 650ad4b578
2 changed files with 16 additions and 3 deletions

View file

@ -90,7 +90,7 @@ class AttachmentDeleteListener
public function postRemoveHandler(Attachment $attachment, LifecycleEventArgs $event) public function postRemoveHandler(Attachment $attachment, LifecycleEventArgs $event)
{ {
//Dont delete file if the attachment uses a builtin ressource: //Dont delete file if the attachment uses a builtin ressource:
if (Attachment::checkIfBuiltin($event->getOldValue('path'))) { if ($attachment->isBuiltIn()) {
return; return;
} }

View file

@ -33,7 +33,9 @@ namespace App\Services;
use App\Entity\Attachments\Attachment; use App\Entity\Attachments\Attachment;
use App\Services\Attachments\AttachmentPathResolver; use App\Services\Attachments\AttachmentPathResolver;
use App\Services\Attachments\AttachmentURLGenerator;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\File;
@ -45,11 +47,16 @@ class AttachmentReverseSearch
{ {
protected $em; protected $em;
protected $pathResolver; 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->em = $em;
$this->pathResolver = $pathResolver; $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. * @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. * @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 */ /* When the file is used more then $threshold times, don't delete it */
if (count($this->findAttachmentsByFile($file)) > $threshold) { if (count($this->findAttachmentsByFile($file)) > $threshold) {
return false; return false;
} }
//Remove file from liip image cache
$this->cacheManager->remove($this->attachmentURLGenerator->absolutePathToAssetPath($file->getPathname()));
$fs = new Filesystem(); $fs = new Filesystem();
$fs->remove($file); $fs->remove($file);
return true; return true;
} }
} }