FileUpload for Cloudflare Images
Hi,
I have a Forms\Components\FileUpload, and when editing, I want to show an image in a URL (not in the disk), something like:
Forms\Components\FileUpload::make('image')
->formatStateUsing(function($record) {
return 'https://picsum.photos/200';
})
(that code fails with error: foreach() argument must be of type array|object, string given)
How can I show the image in the URL?
Thanks6 Replies
You can do this from disk, not url
Ok, I understand.
I'm trying to use the FileUpload to manage Cloudflare Images (it works with http calls)
I'm using saveUploadedFileUsing to upload files, it works. But my problem is that I cannot view the uploaded file. Any recommendation?
The ideal solution should be a filesystem disk, but I didn't find any working pkg for that.
I’m pretty sure there’s an R2 filesystem adapter for laravel. The disk is handled at the laravel level and not filament level. If that makes sense.
use this with r2 storage
Thanks for the replies.
With R2 everything works perfect for me because it uses it as a native disk. The problem is that CF Images is not R2, the way to save and view images is directly HTTPS (save with a post/curl, and load with the direct url).
https://developers.cloudflare.com/images/upload-images/upload-url/
Cloudflare Docs
Upload via URL | Cloudflare Image Optimization docs
Before you upload an image, check the list of supported formats and dimensions to confirm your image will be accepted.
In the end I made my own driver. But I have only one problem left that I can't solve:
class CloudflareImagesAdapter implements FilesystemAdapter
{
protected $client;
protected $apiKey;
protected $accountId;
protected $baseURL;
protected $response;
public function __construct(string $apiKey, string $accountId, string $baseURL)
{
$this->client = new Client([
'base_uri' => 'https://api.cloudflare.com/client/v4/accounts/' . $accountId . '/images/v1',
]);
$this->apiKey = $apiKey;
$this->accountId = $accountId;
$this->baseURL = $baseURL;
}
public function write(string $path, string $contents, Config $config): void
{
$response = $this->client->post('', [
'headers' => [
'Authorization' => 'Bearer ' . $this->apiKey,
],
'multipart' => [
[
'name' => 'file',
'contents' => $contents,
'filename' => basename($path),
],
],
]);
if ($response->getStatusCode() !== 200) {
throw new \Exception('Failed to upload file to Cloudflare Images');
}
}
The problem is that after the upload, I get:
{
"result": {
"id": "2cdc28f0-017a-49c4-9ed7-87056c83901",
"filename": "image.jpeg",
etc...
}
Filament is storing in my model Image: "image.jpeg", but I need to store: "2cdc28f0-017a-49c4-9ed7-87056c83901"
How could I do that?
Thanks