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 UpdatePdfPath
{
public function __construct(private ElementRepository $elementRepository)
{
private const MANAGED_FOLDER_PREFIX = 'element-pdfs/';
public function __construct(
private ElementRepository $elementRepository,
private FileUploader $fileUploader,
) {
}
/**
@ -32,7 +37,12 @@ class UpdatePdfPath
throw new NotFoundException('Element not found');
}
$element->setPdfPath($this->nullableString($request->pdfPath));
$pdfPath = $this->nullableString($request->pdfPath);
if ($pdfPath === null) {
$this->deleteManagedFile($element->getPdfPath());
}
$element->setPdfPath($pdfPath);
return $this->elementRepository->update($element);
}
@ -45,4 +55,17 @@ class UpdatePdfPath
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);
}
}