Loheide-eu-mitarbeiterplatt.../app/Filament/Components/SafeUpload.php
Friederich Loheide d91570f118
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m25s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 6m46s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m52s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 8m5s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m33s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m53s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 9m1s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m49s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 54s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m4s
Dateien nur mit Anmeldung, nur Microsoft-Login, mehrere Titelbilder (0.47.0)
- /uploads nur über das Portal: UploadController prüft Sitzung bzw. App-Token;
  frei nur die Dateien der Anmeldeseite (Logos, Symbole, Anmeldehintergrund,
  Stylesheet, Schrift); public/uploads/.htaccess leitet alles an index.php
  (Migration schreibt sie, da der Ordner vor dem Updater geschützt ist);
  Systemzustand prüft die Regel; nginx-Hinweis in docs/SECURITY.md
- Passwort-Anmeldung abschaltbar (PasswordLogin): Login-Formular, Passwort
  vergessen, App-Token und /api/v1/config; nur mit verknüpftem Konto
  abschaltbar, automatische Rückkehr ohne nutzbaren Dienst, Notausgang
  INHAUS_PASSWORD_LOGIN
- Titelbilder: profile_covers (bis 12), zufällig je Seitenaufruf
- ImageCompressor: Uploads im Backend (SafeUpload) und WordPress-Import auf
  ≤ 2400 px komprimiert, Format bleibt

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 17:26:15 +00:00

56 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Filament\Components;
use App\Support\ImageCompressor;
use Filament\Forms\Components\BaseFileUpload;
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))
// Bilder vor dem Ablegen verkleinern und komprimieren (Kamerafotos haben oft mehrere MB)
->saveUploadedFileUsing(function (BaseFileUpload $component, TemporaryUploadedFile $file): ?string {
if (str_starts_with((string) $file->getMimeType(), 'image/') && is_file((string) $file->getRealPath())) {
ImageCompressor::compressFile((string) $file->getRealPath());
}
return $component->saveUploadedFile($file);
});
}
public static function name(TemporaryUploadedFile $file, ?string $forcedExtension = null): string
{
$extension = $forcedExtension ?? self::EXTENSIONS[strtolower((string) $file->getMimeType())] ?? 'bin';
return Str::ulid().'.'.$extension;
}
}