F
Filamentβ€’6mo ago
marksman

change fileupload field preview url

can i change the preview url of a uploaded file? dont want to use s3 for preview (only storage )
Solution:
You'd need to use a getTemporaryUrl method override for the S3 driver where you use a local route which fetches the assets and returns it for you.
Jump to solution
8 Replies
Solution
toeknee
toekneeβ€’6mo ago
You'd need to use a getTemporaryUrl method override for the S3 driver where you use a local route which fetches the assets and returns it for you.
marksman
marksmanOPβ€’6mo ago
its a laravel thing filament just calls storage::url() ? seems to only call temporary on non public files tho
if ($component->getVisibility() === 'private') {
try {
$url = $storage->temporaryUrl(
$file,
now()->addMinutes(5),
);
} catch (Throwable $exception) {
// This driver does not support creating temporary URLs.
}
}

$url ??= $storage->url($file);
if ($component->getVisibility() === 'private') {
try {
$url = $storage->temporaryUrl(
$file,
now()->addMinutes(5),
);
} catch (Throwable $exception) {
// This driver does not support creating temporary URLs.
}
}

$url ??= $storage->url($file);
but that helps, thank you
toeknee
toekneeβ€’6mo ago
Exactly, you are welcome.
Mona
Monaβ€’6h ago
@toeknee hello I am trying to use S3 to upload videos to it and I save the full path in the database as the access is public.. what I did is first save the files in the local storage then when click save I upload the files to S3 and delete them from the local storage.. this is working fine I think The issue I face is to show the video on the FileUpload filament when I edit the record.. could you please help me find a solution for preview the video Here is my code, in the filament resource: FileUpload::make('link_en') ->disk('public') ->directory('spotlights/videos') ->label('Video (English)') ->acceptedFileTypes(['video/*']) ->preserveFilenames() ->required(), and in the Model I do the upload: static::saving(function ($model) { if (!empty($model->link_en)) { $originalFilePath = $model->link_en; $tempFilePath = storage_path('app/public/' . $model->link_en); $s3FilePath = 'spotlights/videos/' . $model->slug . basename($tempFilePath); // Upload to S3 if (Storage::disk('s3')->put($s3FilePath, file_get_contents($tempFilePath))) { Storage::disk('s3')->setVisibility($s3FilePath, 'public'); $s3Bucket = config('filesystems.disks.s3.bucket'); $s3Region = config('filesystems.disks.s3.region'); $s3BaseUrl = "https://{$s3Bucket}.s3.{$s3Region}.amazonaws.com/"; $fullPath = $s3BaseUrl . $s3FilePath; $model->link_en = $fullPath; Storage::disk('public')->delete($originalFilePath); } } }
toeknee
toekneeβ€’6h ago
Saving full path isn't ideal, because you don't have the file method from the disk. You should store what is recommend buy the plugin, so the file driver gets the file as normal. If you want a full url you store that in addition too.
Mona
Monaβ€’6h ago
you mean it is better to use ->disk('s3') and in this case we can save it directly once we click save? but still with this way I can't see the preview on the FileUpload when edit the record 😦
toeknee
toekneeβ€’5h ago
That is correct yet Preview won't work actually because FilePond doesn't natively support it as per: https://pqina.nl/filepond/#examples Preview only shows for Image files
Easy File Uploading With JavaScript | FilePond
A JavaScript library that can upload anything you throw at it, optimizes images for faster uploads, and offers a great, accessible, silky smooth user experience.
Mona
Monaβ€’4h ago
I found this for filepond plugin media preview but also not working https://github.com/nielsboogaard/filepond-plugin-media-preview can you suggest me any other solution? or just make an option when edit the record to upload a new video or just keep the old one without previewing the video? Many thanks for you help πŸ™‚
GitHub
GitHub - nielsboogaard/filepond-plugin-media-preview: Renders a vid...
Renders a video or audio preview inside the FilePond plugin - nielsboogaard/filepond-plugin-media-preview

Did you find this page helpful?