Co-authored-by: jon brookes <marshyon@gmail.com> Reviewed-on: https://codeberg.org/headshed/share-lt/pulls/18
90 lines
3.4 KiB
PHP
90 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\Media\Schemas;
|
|
|
|
use Filament\Forms\Components\FileUpload;
|
|
use Filament\Forms\Components\Hidden;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Schemas\Schema;
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media as SpatieMedia;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class MediaForm
|
|
{
|
|
public static function configure(Schema $schema): Schema
|
|
{
|
|
return $schema
|
|
->components([
|
|
TextInput::make('name')
|
|
->required()
|
|
->maxLength(255),
|
|
TextInput::make('collection_name')
|
|
->default('default')
|
|
->required()
|
|
->maxLength(255),
|
|
Hidden::make('disk')
|
|
->default('public'),
|
|
FileUpload::make('file')
|
|
->multiple() // workaround for Filament v4 single-file bug
|
|
->label('File')
|
|
->imageEditor()
|
|
->imageEditorAspectRatios([
|
|
'16:9',
|
|
'4:3',
|
|
'1:1',
|
|
])
|
|
->columnSpanFull()
|
|
->disk('s3')
|
|
->directory('media')
|
|
->visibility('public')
|
|
->acceptedFileTypes(['image/*', 'application/pdf'])
|
|
->maxSize(10240)
|
|
->required(fn($context) => $context === 'create')
|
|
|
|
|
|
->afterStateHydrated(function (FileUpload $component, $state, $record): void {
|
|
Log::info('MediaForm afterStateHydrated invoked', ['record_id' => $record?->id, 'state' => $state]);
|
|
|
|
try {
|
|
if (! $record) {
|
|
return;
|
|
}
|
|
|
|
$media = $record;
|
|
if (! $media instanceof SpatieMedia) {
|
|
return;
|
|
}
|
|
|
|
// Construct the correct path: {media_id}/{filename}
|
|
$path = $media->id . '/' . $media->file_name;
|
|
|
|
try {
|
|
$disk = $media->disk ?? 'public';
|
|
if (Storage::disk($disk)->exists($path)) {
|
|
$component->state($path);
|
|
}
|
|
} catch (\Throwable $e) {
|
|
Log::error('MediaForm afterStateHydrated storage check failed', [
|
|
'err' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
'disk' => $media->disk ?? null,
|
|
'path' => $path,
|
|
]);
|
|
}
|
|
} catch (\Throwable $e) {
|
|
Log::error('MediaForm afterStateHydrated unhandled', [
|
|
'err' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
'record' => $record?->id,
|
|
'state' => $state,
|
|
]);
|
|
throw $e;
|
|
}
|
|
}),
|
|
|
|
|
|
|
|
]);
|
|
}
|
|
}
|