fix media removal

This commit is contained in:
Yisroel Baum 2026-06-02 17:00:33 +03:00
parent 4b432d9b99
commit 1934a6b94f
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
7 changed files with 71 additions and 12 deletions

View file

@ -6,11 +6,16 @@ use App\Element\Element;
use App\Element\ElementRepository;
use App\Exceptions\BadRequestException;
use App\Exceptions\NotFoundException;
use App\Shared\Files\FileUploader;
class UpdateIconImageUrl
{
public function __construct(private ElementRepository $elementRepository)
{
private const MANAGED_FOLDER_PREFIX = 'element-icons/';
public function __construct(
private ElementRepository $elementRepository,
private FileUploader $fileUploader,
) {
}
/**
@ -32,9 +37,12 @@ class UpdateIconImageUrl
throw new NotFoundException('Element not found');
}
$element->setIconImageUrl($this->nullableString(
$request->iconImageUrl,
));
$iconImageUrl = $this->nullableString($request->iconImageUrl);
if ($iconImageUrl === null) {
$this->deleteManagedFile($element->getIconImageUrl());
}
$element->setIconImageUrl($iconImageUrl);
return $this->elementRepository->update($element);
}
@ -47,4 +55,17 @@ class UpdateIconImageUrl
return $value;
}
private function deleteManagedFile(?string $path): void
{
if ($path === null) {
return;
}
if (! str_starts_with($path, self::MANAGED_FOLDER_PREFIX)) {
return;
}
$this->fileUploader->delete($path);
}
}