承接 wearemodus/flysystem-cloudinary 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wearemodus/flysystem-cloudinary

Composer 安装命令:

composer require wearemodus/flysystem-cloudinary

包简介

Cloudinary adapter for Flysystem.

README 文档

README

Software License

This is a Flysystem adapter for Cloudinary API.

Installation

composer require wearemodus/flysystem-cloudinary '~2.0'

Or just add the following string to require part of your composer.json:

{
  "require": {
    "wearemodus/flysystem-cloudinary": "~2.0"
  }
}

Bootstrap

<?php
use WeAreModus\Flysystem\Cloudinary\ApiFacade as CloudinaryClient;
use WeAreModus\Flysystem\Cloudinary\CloudinaryAdapter;
use League\Flysystem\Filesystem;

include __DIR__ . '/vendor/autoload.php';

$client = new CloudinaryClient([
    'cloud_name' => 'your-cloudname-here',
    'api_key' => 'api-key',
    'api_secret' => 'You-know-what-to-do',
    'overwrite' => true, // set this to true if you want to overwrite existing files using $filesystem->write();
]);

$adapter = new CloudinaryAdapter($client);
// This option disables assert that file is absent before calling `write`.
// It is necessary if you want to overwrite files on `write` as Cloudinary does it by default.
$filesystem = new Filesystem($adapter, ['disable_asserts' => true]);

Cloudinary features

Please, keep in mind three possible pain-in-asses of Cloudinary:

  • It adds automatically file extension to its public_id. In terms of Flysystem, cloudinary's public_id is considered as filename. However, if you set public_id as 'test.jpg' Cloudinary will save the file as 'test.jpg.jpg'. In order to work it around, you can use PathConverterInterface.
  • It does not support folders creation through the API
  • If you want to save your files using folder you should set public_ids like 'test/test.jpg' and allow automated folder creation in your account settings in Cloudinary dashboard.

Good news!

The library supports Cloudinary Transformations!

WeAreModus Fork

This fork includes some essential changes needed for my use-case. See CHANGELOG.md for a full list of changes, but here are the cliff notes:

  • Upload settings default to resource_type 'auto' in order to support video upload in addition to images
  • DataUri now reads the mime-type of media using only the first 1 MB, in order to prevent memory overflow for large files due to finfo->buffer()
  • ApiFacade constructor now accepts an uploadOptions argument containing options that are passed through to the Cloudinary SDK's upload function
  • Added explicit PHP types (this will prevent compatibility with PHP <7.4)
  • Updated dependencies (PHP >=7.4, PHPUnit ^9, cloudinary_php ^2, php_codesniffer 3.*)

Using with Laravel-MediaLibrary

In order to integrate this Cloudinary filesystem with Laravel-MediaLibrary, you will need to create a new filesystem driver as outline below:

  1. Define your Cloudinary .env variables
    • CLOUDINARY_CLOUD_NAME=
    • CLOUDINARY_API_KEY=
    • CLOUDINARY_API_SECRET=
  2. Define a new filesystem driver in your filesystems.php config file (within the 'disks' array)
    'disks' => [
         ...
    
         'cloudinary' => [
             'driver'     => 'cloudinary',
             'cloud_name' => env('CLOUDINARY_CLOUD_NAME'),
             'api_key'    => env('CLOUDINARY_API_KEY'),
             'api_secret' => env('CLOUDINARY_API_SECRET'),
         ],
    ]
  3. Create a new CloudinaryServiceProvider php artisan make:provider CloudinarySeviceProvider, and insert this code into the boot() method
    use WeAreModus\Flysystem\Cloudinary\ApiFacade as CloudinaryClient;
    use WeAreModus\Flysystem\Cloudinary\CloudinaryAdapter;
    use WeAreModus\Flysystem\Cloudinary\Converter\TruncateExtensionConverter;
    use Illuminate\Support\Facades\Storage;
    use Illuminate\Support\ServiceProvider;
    use League\Flysystem\Filesystem;
    
    ...
    
    public function boot() 
    {
        Storage::extend('cloudinary', function ($app, $config) {
        $client = new CloudinaryClient(
                [
                    'cloud_name' => $config['cloud_name'],
                    'api_key'    => $config['api_key'],
                    'api_secret' => $config['api_secret'],
                    'overwrite'  => $config['overwrite'] ?? true,
                ], // Cloudinary API options
                [
                    'resource_type'          => 'auto',
                    'eager'                  => [
                        ['fetch_format' => 'mp4', 'format' => '', 'video_codec' => 'h264', 'quality' => '70'], // f_mp4,vc_h264,q_70
                        ['fetch_format' => 'png', 'format' => '', 'quality' => '70'], // f_png,q_70
                    ],
                    'eager_async'            => 'true',
                    'eager_notification_url' => 'https://mysite.test/webhook/eager',
                    'notification_url'       => 'https://mysite.test/webhook/upload',
                    ...
                ], // Upload options
                new TruncateExtensionConverter()
           );
            
            return new Filesystem(new CloudinaryAdapter($client));
        });
    }
  4. Finally, simply define 'cloudinary' as your diskName when managing media
    $this->addMediaCollection('videos')->useDisk('cloudinary'); // Use Cloudinary as disk for the entire collection
    // OR
    $model->addMedia($mediaPath)
          ->toMediaCollection('videos', 'cloudinary'); // USe Cloudinary as disk for this media only

wearemodus/flysystem-cloudinary 适用场景与选型建议

wearemodus/flysystem-cloudinary 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 870 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 07 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 wearemodus/flysystem-cloudinary 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 wearemodus/flysystem-cloudinary 我们能提供哪些服务?
定制开发 / 二次开发

基于 wearemodus/flysystem-cloudinary 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 870
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 18
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-22