Tuesday, 17 September 2024

Create a custom Flysystem adapter for Cloudinary in Laravel

1. Install Cloudinary SDK

 composer require cloudinary/cloudinary_php

2. Create a Custom Flysystem Adapter

// app/Services/CloudinaryAdapter.php

namespace App\Services;

use Cloudinary\Cloudinary;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToWriteFile;
use League\Flysystem\UnableToDeleteFile;

class CloudinaryAdapter implements FilesystemAdapter
{
    protected $cloudinary;

    public function __construct(Cloudinary $cloudinary)
    {
        $this->cloudinary = $cloudinary;
    }

    public function write(string $path, string $contents, array $config): void
    {
        try {
            $this->cloudinary->uploadApi()->upload($contents, ['public_id' => $path]);
        } catch (\Exception $e) {
            throw UnableToWriteFile::atLocation($path, $e->getMessage());
        }
    }

    public function read(string $path): string
    {
        try {
            $result = $this->cloudinary->image($path)->toUrl();
            return file_get_contents($result);
        } catch (\Exception $e) {
            throw UnableToReadFile::atLocation($path, $e->getMessage());
        }
    }

    public function delete(string $path): void
    {
        try {
            $this->cloudinary->uploadApi()->destroy($path);
        } catch (\Exception $e) {
            throw UnableToDeleteFile::atLocation($path, $e->getMessage());
        }
    }

    // Implement other required methods (copy, move, etc.) following the same pattern.
}

3. Register the Custom Driver 

// app/Providers/AppServiceProvider.php

use App\Services\CloudinaryAdapter;
use Cloudinary\Cloudinary;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\Filesystem;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Storage::extend('cloudinary', function ($app, $config) {
            $cloudinary = new Cloudinary([
                'cloud_name' => $config['cloud_name'],
                'api_key'    => $config['api_key'],
                'api_secret' => $config['api_secret'],
            ]);

            $adapter = new CloudinaryAdapter($cloudinary);

            return new Filesystem($adapter);
        });
    }
}

4. Add Cloudinary Disk to config/filesystems.php

// config/filesystems.php

'disks' => [

    // Other disks...

    'cloudinary' => [
        'driver'    => 'cloudinary',
        'cloud_name' => env('CLOUDINARY_CLOUD_NAME'),
        'api_key'    => env('CLOUDINARY_API_KEY'),
        'api_secret' => env('CLOUDINARY_API_SECRET'),
    ],

],

.env file:

CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret

5. Usage Example

 // Upload file to Cloudinary
Storage::disk('cloudinary')->put('image_name', $fileContent);

// Retrieve file URL from Cloudinary
$url = Storage::disk('cloudinary')->url('image_name');

// Delete file from Cloudinary
Storage::disk('cloudinary')->delete('image_name');

Thank you

No comments:

Post a Comment

Golang Advanced Interview Q&A