FilamentF
Filament2y ago
Luiz

image upload using URL

Has anyone managed to create a way to upload media via URL to the https://filamentphp.com/plugins/filament-spatie-media-library plugin?

What I've managed to do so far is this:

Forms\Components\TextInput::make('url')
->label('URL')
->url()
->dehydrated(false)
->live(onBlur: true)
->suffixAction(
    Forms\Components\Actions\Action::make('downloadFile')
        ->icon('heroicon-m-arrow-down-tray')
        ->action(function ($record, Get $get, Set $set, $state, \Filament\Forms\Components\Component $component) {
            $contents = file_get_contents($state);
            $fileName = Str::ulid();
            $extension = pathinfo($state, PATHINFO_EXTENSION);
            $fileNameWithExtension = $fileName . '.' . $extension;
            $filePath = 'livewire-tmp/' . $fileNameWithExtension;

            // $SpatieMediaLibraryFileUpload = $component->getContainer()->getComponent(fn ($childComponent) => $childComponent->getName() === 'media');

            $storage = Storage::disk('local');

            $storage->put($filePath, $contents);

            $tempFile = TemporaryUploadedFile::createFromLivewire($fileNameWithExtension);

            $media = $get('media');
            $media[Str::uuid()->toString()] = $tempFile;

            $set('media', $media);

            $set('url', null);
        })
)


However, nothing appears on the form and when I try to save it, an empty error occurs.
Filament
Filament support for Spatie's Laravel Media Library package.
Was this page helpful?