47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Support;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
|
|
|
class IncomingFile
|
|
{
|
|
public static function store(mixed $value): ?string
|
|
{
|
|
if (is_array($value)) {
|
|
foreach ($value as $item) {
|
|
$stored = self::store($item);
|
|
|
|
if ($stored !== null) {
|
|
return $stored;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
if ($value instanceof TemporaryUploadedFile || $value instanceof UploadedFile) {
|
|
$extension = strtolower($value->getClientOriginalExtension());
|
|
$stem = preg_replace('/[^A-Za-z0-9_-]+/', '-', pathinfo($value->getClientOriginalName(), PATHINFO_FILENAME)) ?? '';
|
|
$stem = trim($stem, '-');
|
|
$stem = ($stem !== '' ? $stem : 'datei').'-'.bin2hex(random_bytes(4));
|
|
|
|
if (preg_match('/^[a-z0-9]{1,8}$/', $extension) !== 1) {
|
|
return null;
|
|
}
|
|
|
|
$path = $value->storeAs('inhalte', $stem.'.'.$extension, 'cms_incoming');
|
|
|
|
return is_string($path) && $path !== '' ? $path : null;
|
|
}
|
|
|
|
if (! is_string($value) || $value === '' || str_contains($value, '..') || ! str_starts_with($value, 'inhalte/')) {
|
|
return null;
|
|
}
|
|
|
|
return Storage::disk('cms_incoming')->exists($value) ? $value : null;
|
|
}
|
|
}
|