Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
46 lines
1.5 KiB
PHP
46 lines
1.5 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Components;
|
||
|
||
use Filament\Forms\Components\FileUpload;
|
||
use Illuminate\Support\Str;
|
||
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
||
|
||
/**
|
||
* Uploads in den öffentlichen Ordner (public/uploads): nur bestimmte Dateitypen, und
|
||
* die Dateiendung ergibt sich aus dem geprüften Inhalt – nie aus dem Namen der
|
||
* hochgeladenen Datei (sonst ließe sich z. B. „bild.html“ oder „bild.php“ ablegen).
|
||
*/
|
||
final class SafeUpload
|
||
{
|
||
public const IMAGES = ['image/jpeg', 'image/png', 'image/webp', 'image/gif'];
|
||
|
||
public const EXTENSIONS = [
|
||
'image/jpeg' => 'jpg',
|
||
'image/png' => 'png',
|
||
'image/webp' => 'webp',
|
||
'image/gif' => 'gif',
|
||
'image/svg+xml' => 'svg',
|
||
'image/x-icon' => 'ico',
|
||
'image/vnd.microsoft.icon' => 'ico',
|
||
'font/woff2' => 'woff2',
|
||
'application/font-woff2' => 'woff2',
|
||
];
|
||
|
||
/**
|
||
* @param list<string> $types erlaubte MIME-Typen
|
||
*/
|
||
public static function apply(FileUpload $upload, array $types = self::IMAGES, ?string $forcedExtension = null): FileUpload
|
||
{
|
||
return $upload
|
||
->acceptedFileTypes($types)
|
||
->getUploadedFileNameForStorageUsing(fn (TemporaryUploadedFile $file) => self::name($file, $forcedExtension));
|
||
}
|
||
|
||
public static function name(TemporaryUploadedFile $file, ?string $forcedExtension = null): string
|
||
{
|
||
$extension = $forcedExtension ?? self::EXTENSIONS[strtolower((string) $file->getMimeType())] ?? 'bin';
|
||
|
||
return Str::ulid().'.'.$extension;
|
||
}
|
||
}
|