Created
August 27, 2024 07:50
-
-
Save neverything/e04320d305378910327315eb3362692c to your computer and use it in GitHub Desktop.
Background File Uploads using Filament Forms, FilePond, Livewire and Alpine.js: https://silvanhagen.com/writing/background-file-upload-in-filament-forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Filament\Resources; | |
use App\Filament\Resources\LinkResource\Pages; | |
use App\Models\Link; | |
use AshAllenDesign\FaviconFetcher\Facades\Favicon; | |
use Filament\Forms; | |
use Filament\Forms\Form; | |
use Filament\Forms\Set; | |
use Filament\Resources\Resource; | |
use Filament\Tables; | |
use Filament\Tables\Table; | |
use Illuminate\Http\UploadedFile; | |
use Illuminate\Support\Facades\Storage; | |
use Illuminate\Support\Facades\URL; | |
use Illuminate\Support\Str; | |
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile; | |
class LinkResource extends Resource | |
{ | |
protected static ?string $model = Link::class; | |
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; | |
public static function form(Form $form): Form | |
{ | |
return $form | |
->schema([ | |
Forms\Components\TextInput::make('url') | |
->label('Website URL') | |
->live(onBlur: true) | |
->afterStateUpdated(function (Set $set, $state, $livewire) { | |
$title_from_url = Str::of($state) | |
->remove(['http://', 'https://']) | |
->before('/') | |
->title(); | |
$set('title', $title_from_url); | |
$favicon = Favicon::fetchAll($state)->largest(); | |
$file = self::createTemporaryFileUploadFromUrl($favicon); | |
$livewire->dispatch('add-favicon', $file); | |
}) | |
->url() | |
->required(), | |
Forms\Components\TextInput::make('title') | |
->required(), | |
Forms\Components\FileUpload::make('favicon_path') | |
->label('Favicon') | |
->extraAlpineAttributes(['x-on:add-favicon.window' => ' | |
const pond = FilePond.find($el.querySelector(".filepond--root")); | |
setTimeout(() => { | |
pond.removeFiles({ revert: true }); | |
pond.addFile($event.detail); | |
}, 750);', | |
]) | |
->image(), | |
])->columns(1); | |
} | |
public static function createTemporaryFileUploadFromUrl($favicon): string | |
{ | |
// Step 1: Save the file to a temporary location | |
$tempFilePath = tempnam(sys_get_temp_dir(), 'upload'); | |
file_put_contents($tempFilePath, $favicon->content()); | |
// Step 2: Create a UploadedFile instance | |
$mimeType = mime_content_type($tempFilePath); | |
$tempFile = new UploadedFile($tempFilePath, basename($favicon->getFaviconUrl()), $mimeType); | |
$path = Storage::put('livewire-tmp', $tempFile); | |
// Step 3: Create a TemporaryUploadedFile instance | |
$file = TemporaryUploadedFile::createFromLivewire($path); | |
return URL::temporarySignedRoute( | |
'livewire.preview-file', now()->addMinutes(30)->endOfHour(), ['filename' => $file->getFilename()] | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment