fix media removal
This commit is contained in:
parent
4b432d9b99
commit
1934a6b94f
7 changed files with 71 additions and 12 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 = [
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue