simplify media deletion

This commit is contained in:
Yisroel Baum 2026-06-02 17:12:44 +03:00
parent 247433bc60
commit 01434a9b2d
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
4 changed files with 34 additions and 15 deletions

View file

@ -10,8 +10,6 @@ use App\Shared\Files\FileUploader;
class UpdateIconImageUrl class UpdateIconImageUrl
{ {
private const MANAGED_FOLDER_PREFIX = 'element-icons/';
public function __construct( public function __construct(
private ElementRepository $elementRepository, private ElementRepository $elementRepository,
private FileUploader $fileUploader, private FileUploader $fileUploader,
@ -62,10 +60,6 @@ class UpdateIconImageUrl
return; return;
} }
if (! str_starts_with($path, self::MANAGED_FOLDER_PREFIX)) {
return;
}
$this->fileUploader->delete($path); $this->fileUploader->delete($path);
} }
} }

View file

@ -10,8 +10,6 @@ use App\Shared\Files\FileUploader;
class UpdatePdfPath class UpdatePdfPath
{ {
private const MANAGED_FOLDER_PREFIX = 'element-pdfs/';
public function __construct( public function __construct(
private ElementRepository $elementRepository, private ElementRepository $elementRepository,
private FileUploader $fileUploader, private FileUploader $fileUploader,
@ -62,10 +60,6 @@ class UpdatePdfPath
return; return;
} }
if (! str_starts_with($path, self::MANAGED_FOLDER_PREFIX)) {
return;
}
$this->fileUploader->delete($path); $this->fileUploader->delete($path);
} }
} }

View file

@ -6,6 +6,7 @@ use App\Element\CreateElementDto;
use App\Element\ElementRepository; use App\Element\ElementRepository;
use App\Set\SetRepository; use App\Set\SetRepository;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Storage;
use RuntimeException; use RuntimeException;
class ElementSeeder extends Seeder class ElementSeeder extends Seeder
@ -15,11 +16,25 @@ class ElementSeeder extends Seeder
$setRepository = app(SetRepository::class); $setRepository = app(SetRepository::class);
$elementRepository = app(ElementRepository::class); $elementRepository = app(ElementRepository::class);
$baderechSet = $setRepository->find(1); $baderechSet = $setRepository->find(1);
$rootIconImageUrl = $this->seedStorageFile(
base_path(
'../frontend/rabbi_gerzi/public/assets/'
. 'baderech-haavodah-icon.png',
),
'element-icons/baderech-haavodah-icon.png',
);
$introductionPdfPath = $this->seedStorageFile(
base_path(
'../frontend/rabbi_gerzi/public/assets/pdfs/baderech.pdf',
),
'element-pdfs/baderech.pdf',
);
$rootElement = $elementRepository->create(new CreateElementDto( $rootElement = $elementRepository->create(new CreateElementDto(
set: $baderechSet, set: $baderechSet,
title: $baderechSet->getName(), title: $baderechSet->getName(),
description: $baderechSet->getDescription(), description: $baderechSet->getDescription(),
iconImageUrl: '/assets/baderech-haavodah-icon.png', iconImageUrl: $rootIconImageUrl,
richText: '', richText: '',
pdfPath: null, pdfPath: null,
youtubeUrl: null, youtubeUrl: null,
@ -36,7 +51,7 @@ class ElementSeeder extends Seeder
. 'unlock the ability to live with strength, confidence, ' . 'unlock the ability to live with strength, confidence, '
. 'and hope.', . 'and hope.',
'richText' => $this->introductionRichText(), 'richText' => $this->introductionRichText(),
'pdfPath' => '/assets/pdfs/baderech.pdf', 'pdfPath' => $introductionPdfPath,
], ],
[ [
'title' => '2. Foundations', 'title' => '2. Foundations',
@ -116,6 +131,20 @@ class ElementSeeder extends Seeder
)); ));
} }
private function seedStorageFile(
string $sourcePath,
string $storagePath,
): string {
$contents = file_get_contents($sourcePath);
if ($contents === false) {
throw new RuntimeException("Seed file missing: $sourcePath");
}
Storage::disk('public')->put($storagePath, $contents);
return $storagePath;
}
private function introductionRichText(): string private function introductionRichText(): string
{ {
return '<h2 style="text-align: center;">' return '<h2 style="text-align: center;">'

View file

@ -117,10 +117,12 @@ describe('media page sets', () => {
.should('have.css', 'text-align', 'center') .should('have.css', 'text-align', 'center')
cy.get('[data-cy="element-youtube-embed"]').should('not.exist') cy.get('[data-cy="element-youtube-embed"]').should('not.exist')
cy.contains('[data-cy="element-pdf-link"]', 'View PDF') cy.contains('[data-cy="element-pdf-link"]', 'View PDF')
.as('pdfLink')
.should('be.visible') .should('be.visible')
.and('have.attr', 'target', '_blank')
cy.get('@pdfLink')
.and('have.attr', 'href') .and('have.attr', 'href')
.and('include', '/storage/element-pdfs/baderech.pdf') .and('include', '/storage/element-pdfs/baderech.pdf')
.and('have.attr', 'target', '_blank')
cy.contains('[data-cy="child-element-link"]', introductionAudioTitle) cy.contains('[data-cy="child-element-link"]', introductionAudioTitle)
.as('introductionAudioLink') .as('introductionAudioLink')
.should('have.attr', 'href', '/element/8') .should('have.attr', 'href', '/element/8')