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

@ -114,7 +114,15 @@ class ElementController
private function stringInput(Request $request, string $key): ?string
{
if (! $request->exists($key)) {
return null;
}
$value = $request->input($key);
if ($value === null) {
return '';
}
if (! is_string($value)) {
return null;
}

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);
}
}

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);
}
}

View file

@ -7,4 +7,6 @@ interface FileUploader
public function upload(FileToUpload $file, string $folder): string;
public function url(string $path): string;
public function delete(string $path): void;
}

View file

@ -20,6 +20,11 @@ class LaravelFileUploader implements FileUploader
return Storage::disk('public')->url($path);
}
public function delete(string $path): void
{
Storage::disk('public')->delete($path);
}
private function extensionFor(FileToUpload $file): string
{
$mimeExtensions = [

View file

@ -38,9 +38,9 @@ class ElementControllerTest extends TestCase
$updateElement = new UpdateElement(
new UpdateTitle($this->elementRepo),
new UpdateDescription($this->elementRepo),
new UpdateIconImageUrl($this->elementRepo),
new UpdateIconImageUrl($this->elementRepo, $this->fileUploader),
new UpdateRichText($this->elementRepo),
new UpdatePdfPath($this->elementRepo),
new UpdatePdfPath($this->elementRepo, $this->fileUploader),
new UpdateYoutubeUrl($this->elementRepo),
new UpdateIconImage(
$this->elementRepo,

View file

@ -36,9 +36,9 @@ class UpdateElementTest extends TestCase
$this->updateElement = new UpdateElement(
new UpdateTitle($this->elementRepo),
new UpdateDescription($this->elementRepo),
new UpdateIconImageUrl($this->elementRepo),
new UpdateIconImageUrl($this->elementRepo, $this->fileUploader),
new UpdateRichText($this->elementRepo),
new UpdatePdfPath($this->elementRepo),
new UpdatePdfPath($this->elementRepo, $this->fileUploader),
new UpdateYoutubeUrl($this->elementRepo),
new UpdateIconImage(
$this->elementRepo,