From 1934a6b94fef5fc979cde923697090a6f4da2787 Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Tue, 2 Jun 2026 17:00:33 +0300 Subject: [PATCH] fix media removal --- backend/app/Controllers/ElementController.php | 8 +++++ .../UpdateElement/UpdateIconImageUrl.php | 31 ++++++++++++++++--- .../UseCases/UpdateElement/UpdatePdfPath.php | 29 +++++++++++++++-- backend/app/Shared/Files/FileUploader.php | 2 ++ .../app/Shared/Files/LaravelFileUploader.php | 5 +++ .../Controllers/ElementControllerTest.php | 4 +-- .../Element/UseCases/UpdateElementTest.php | 4 +-- 7 files changed, 71 insertions(+), 12 deletions(-) diff --git a/backend/app/Controllers/ElementController.php b/backend/app/Controllers/ElementController.php index 5897a51..1cfc205 100644 --- a/backend/app/Controllers/ElementController.php +++ b/backend/app/Controllers/ElementController.php @@ -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; } diff --git a/backend/app/Element/UseCases/UpdateElement/UpdateIconImageUrl.php b/backend/app/Element/UseCases/UpdateElement/UpdateIconImageUrl.php index cc07171..f6f13f5 100644 --- a/backend/app/Element/UseCases/UpdateElement/UpdateIconImageUrl.php +++ b/backend/app/Element/UseCases/UpdateElement/UpdateIconImageUrl.php @@ -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); + } } diff --git a/backend/app/Element/UseCases/UpdateElement/UpdatePdfPath.php b/backend/app/Element/UseCases/UpdateElement/UpdatePdfPath.php index d6fde1e..2bb2cc5 100644 --- a/backend/app/Element/UseCases/UpdateElement/UpdatePdfPath.php +++ b/backend/app/Element/UseCases/UpdateElement/UpdatePdfPath.php @@ -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); + } } diff --git a/backend/app/Shared/Files/FileUploader.php b/backend/app/Shared/Files/FileUploader.php index b5bad4e..ac30916 100644 --- a/backend/app/Shared/Files/FileUploader.php +++ b/backend/app/Shared/Files/FileUploader.php @@ -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; } diff --git a/backend/app/Shared/Files/LaravelFileUploader.php b/backend/app/Shared/Files/LaravelFileUploader.php index aca4434..89416b0 100644 --- a/backend/app/Shared/Files/LaravelFileUploader.php +++ b/backend/app/Shared/Files/LaravelFileUploader.php @@ -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 = [ diff --git a/backend/tests/Unit/Controllers/ElementControllerTest.php b/backend/tests/Unit/Controllers/ElementControllerTest.php index a8bc34a..5416d92 100644 --- a/backend/tests/Unit/Controllers/ElementControllerTest.php +++ b/backend/tests/Unit/Controllers/ElementControllerTest.php @@ -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, diff --git a/backend/tests/Unit/Element/UseCases/UpdateElementTest.php b/backend/tests/Unit/Element/UseCases/UpdateElementTest.php index 4e6e1bb..8d823e3 100644 --- a/backend/tests/Unit/Element/UseCases/UpdateElementTest.php +++ b/backend/tests/Unit/Element/UseCases/UpdateElementTest.php @@ -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,