A Cloudflare Images library for Laravel

v0.1.4 2023-03-29 20:36 UTC

This package is not auto-updated.

Last update: 2024-04-25 01:25:47 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status

️⚠️ WARNING: This package is still in development and not ready for production use. ⚠️

For example, currently there's no option to use a custom model in place of the package's Image and ImageType models. I'll add that functionality once my implementation is stable.

Installation

Install the package via composer:

composer require benbjurstrom/glint

Add cloudflare credentials to your services config file:

// config/services.php
'cloudflare' => [
    'account_hash' => env('CLOUDFLARE_IMAGES_ACCOUNT_HASH'),
    'account_id' => env('CLOUDFLARE_IMAGES_ACCOUNT_ID'),
    'api_token' => env('CLOUDFLARE_IMAGES_API_TOKEN'),
    'signing_key' => env('CLOUDFLARE_IMAGES_SIGNING_KEY'),
],

Then publish and run the migrations with:

php artisan vendor:publish --tag="glint-migrations"
php artisan migrate

Usage

Client upload

Since there's so many ways to handle authentication, authorization, and response formatting this is left to the user to implement. But an example controller might look something like this:

    public function store(Request $request)
    {
        Gate::authorize('uploadImages', [
            $request->user()
        ]);

        $data = $request->validate([
            'type_id' => 'required|uuid|exists:image_types,id',
            'model_id' => 'required|uuid',
            'model_type' => 'required',
        ]);

        $modelName = Relation::getMorphedModel($data['model_type']) ?? $data['model_type'];
        $model = (new $modelName)->findOrFail($data['model_id']);
        throw_unless($model instanceof HasImagesInterface, 
            \Exception::class, 'Model does not implement HasImagesInterface'
        );

        $type = ImageType::findOrFail($data['type_id']);
        $image = $model->addImageFromDraft($type);

        return response()->json($image);
    }

Credits

License

The MIT License (MIT). Please see License File for more information.