Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

26 changes: 21 additions & 5 deletions src/Filament/Forms/Components/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class ImageManager extends Field
Expand Down Expand Up @@ -84,7 +85,7 @@ protected function setUp(): void
$tempPath = storage_path('app/public/'.$item['temp_file']);
if (file_exists($tempPath)) {
$record->addMedia($tempPath)
->usingFileName($item['file_name'] ?? basename($tempPath))
->usingFileName($this->sanitizeFilename($item['file_name'] ?? basename($tempPath)))
->withCustomProperties([
'name' => $item['name'] ?? [],
'description' => $item['description'] ?? [],
Expand All @@ -98,7 +99,7 @@ protected function setUp(): void
} elseif (isset($item['temp_url'])) {
try {
$record->addMediaFromUrl($item['temp_url'])
->usingFileName($item['file_name'] ?? basename($item['temp_url']))
->usingFileName($this->sanitizeFilename($item['file_name'] ?? basename($item['temp_url'])))
->withCustomProperties([
'name' => $item['name'] ?? [],
'description' => $item['description'] ?? [],
Expand Down Expand Up @@ -213,7 +214,7 @@ public function getUploadAction(): Action

if (file_exists($fullPath)) {
$tempId = 'temp_'.uniqid();
$fileName = basename($filePath);
$fileName = $this->sanitizeFilename(basename($filePath));

$currentState[] = [
'id' => null,
Expand Down Expand Up @@ -256,7 +257,7 @@ public function getUploadAction(): Action

if (file_exists($fullPath)) {
$record->addMedia($fullPath)
->usingFileName(basename($filePath))
->usingFileName($this->sanitizeFilename(basename($filePath)))
->withCustomProperties([
'name' => [],
'description' => [],
Expand Down Expand Up @@ -331,7 +332,7 @@ public function getUrlUploadAction(): Action
'description' => [],
'is_cover' => count($currentState) === 0,
'position' => ++$maxPosition,
'file_name' => basename($url),
'file_name' => $this->sanitizeFilename(basename($url)),
'mime_type' => 'image/*',
'size' => 0,
];
Expand Down Expand Up @@ -694,6 +695,21 @@ protected function ensureSingleCoverImage(Model $record): void
}
}

protected function sanitizeFilename(string $filename): string
{
$pathInfo = pathinfo($filename);
$name = $pathInfo['filename'] ?? 'image';
$extension = isset($pathInfo['extension']) ? '.'.$pathInfo['extension'] : '';

$sanitizedName = Str::slug($name, '-');

if (empty($sanitizedName)) {
$sanitizedName = 'image-'.time();
}

return $sanitizedName.$extension;
}

protected function cleanupOldTempFiles(): void
{
$tempDir = storage_path('app/public/temp-images');
Expand Down
Loading
Loading