|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CloudFiles\Adapter; |
| 4 | + |
| 5 | +use ExpressionEngine\Dependency\Aws\S3\S3Client; |
| 6 | +use ExpressionEngine\Dependency\League\Flysystem; |
| 7 | +use ExpressionEngine\Library\Filesystem\Adapter\AdapterInterface; |
| 8 | +use ExpressionEngine\Library\Filesystem\Adapter\AdapterTrait; |
| 9 | +use ExpressionEngine\Service\Validation\ValidationAware; |
| 10 | + |
| 11 | +class BackblazeB2 extends Flysystem\AwsS3v3\AwsS3Adapter implements AdapterInterface, ValidationAware |
| 12 | +{ |
| 13 | + use AdapterTrait; |
| 14 | + |
| 15 | + protected $_validation_rules = [ |
| 16 | + 'key' => 'required', |
| 17 | + 'secret' => 'required', |
| 18 | + 'region' => 'required', |
| 19 | + 'bucket' => 'required', |
| 20 | + ]; |
| 21 | + |
| 22 | + public function __construct($settings = []) |
| 23 | + { |
| 24 | + $this->settings = $settings; |
| 25 | + $client = new S3Client([ |
| 26 | + 'credentials' => [ |
| 27 | + 'key' => $settings['key'], |
| 28 | + 'secret' => $settings['secret'] |
| 29 | + ], |
| 30 | + 'region' => $settings['region'], |
| 31 | + 'version' => 'latest', |
| 32 | + 'endpoint' => "https://s3.{$settings['region']}.backblazeb2.com", |
| 33 | + 'exception_class' => \ExpressionEngine\Dependency\Aws\S3\Exception\S3Exception::class |
| 34 | + ]); |
| 35 | + |
| 36 | + parent::__construct($client, $settings['bucket']); |
| 37 | + } |
| 38 | + |
| 39 | + public static function getSettingsForm($settings) |
| 40 | + { |
| 41 | + return [ |
| 42 | + [ |
| 43 | + 'title' => 'Application Key ID', |
| 44 | + 'desc' => 'Enter your Backblaze B2 Key ID', |
| 45 | + 'fields' => [ |
| 46 | + 'adapter_settings[key]' => [ |
| 47 | + 'type' => 'text', |
| 48 | + 'value' => $settings['key'] ?? '', |
| 49 | + 'required' => true |
| 50 | + ] |
| 51 | + ] |
| 52 | + ], |
| 53 | + [ |
| 54 | + 'title' => 'Application Key', |
| 55 | + 'desc' => 'Enter your Backblaze B2 Key', |
| 56 | + 'fields' => [ |
| 57 | + 'adapter_settings[secret]' => [ |
| 58 | + 'type' => 'text', |
| 59 | + 'value' => $settings['secret'] ?? '', |
| 60 | + 'required' => true |
| 61 | + ] |
| 62 | + ] |
| 63 | + ], |
| 64 | + [ |
| 65 | + 'title' => 'Region', |
| 66 | + 'desc' => 'Enter the region for your Backblaze B2 Bucket', |
| 67 | + 'fields' => [ |
| 68 | + 'adapter_settings[region]' => [ |
| 69 | + 'type' => 'text', |
| 70 | + 'value' => $settings['region'] ?? '', |
| 71 | + 'required' => true |
| 72 | + ] |
| 73 | + ] |
| 74 | + ], |
| 75 | + [ |
| 76 | + 'title' => 'Bucket Name', |
| 77 | + 'desc' => 'Enter the name of your Backblaze B2 Bucket', |
| 78 | + 'fields' => [ |
| 79 | + 'adapter_settings[bucket]' => [ |
| 80 | + 'type' => 'text', |
| 81 | + 'value' => $settings['bucket'] ?? '', |
| 82 | + 'required' => true |
| 83 | + ] |
| 84 | + ] |
| 85 | + ], |
| 86 | + [ |
| 87 | + 'title' => 'Path', |
| 88 | + 'desc' => 'Enter the path inside your Backblaze B2 Bucket', |
| 89 | + 'fields' => [ |
| 90 | + 'server_path' => [ |
| 91 | + 'type' => 'text', |
| 92 | + 'value' => $settings['server_path'] ?? '', |
| 93 | + 'required' => false |
| 94 | + ] |
| 95 | + ] |
| 96 | + ], |
| 97 | + [ |
| 98 | + 'title' => 'Url', |
| 99 | + 'desc' => 'Enter the url used to access your Backblaze B2 Bucket', |
| 100 | + 'fields' => [ |
| 101 | + 'url' => [ |
| 102 | + 'type' => 'text', |
| 103 | + 'value' => $settings['url'] ?? '', |
| 104 | + 'required' => false |
| 105 | + ] |
| 106 | + ] |
| 107 | + ] |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | + public function getBaseUrl() |
| 112 | + { |
| 113 | + return implode('/', array_filter([ |
| 114 | + 'https://s3.'.$this->settings['region'].'.backblazeb2.com', |
| 115 | + $this->getBucket(), |
| 116 | + $this->getPathPrefix() |
| 117 | + ])); |
| 118 | + } |
| 119 | +} |
0 commit comments