-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCloudflareR2.php
More file actions
133 lines (123 loc) · 4.14 KB
/
CloudflareR2.php
File metadata and controls
133 lines (123 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace CloudFiles\Adapter;
use ExpressionEngine\Dependency\Aws\S3\S3Client;
use ExpressionEngine\Dependency\League\Flysystem;
use ExpressionEngine\Library\Filesystem\Adapter\AdapterInterface;
use ExpressionEngine\Library\Filesystem\Adapter\AdapterTrait;
use ExpressionEngine\Service\Validation\ValidationAware;
class CloudflareR2 extends AbstractS3 implements AdapterInterface, ValidationAware
{
use AdapterTrait;
protected $_validation_rules = [
'account_id' => 'required',
'key' => 'required',
'secret' => 'required',
'bucket' => 'required',
'url' => 'required',
];
public function __construct($settings = [])
{
$this->settings = $settings;
$client = new S3Client([
'credentials' => [
'key' => $settings['key'],
'secret' => $settings['secret']
],
'region' => 'auto',
'version' => 'latest',
'endpoint' => "https://{$settings['account_id']}.r2.cloudflarestorage.com",
'exception_class' => \ExpressionEngine\Dependency\Aws\S3\Exception\S3Exception::class
]);
parent::__construct($client, $settings['bucket']);
}
public static function getSettingsForm($settings)
{
return [
[
'title' => 'Account ID',
'desc' => 'Enter your Cloudflare R2 Account ID',
'fields' => [
'adapter_settings[account_id]' => [
'type' => 'text',
'value' => $settings['account_id'] ?? '',
'required' => true
]
]
],
[
'title' => 'Key',
'desc' => 'Enter your Cloudflare R2 Key',
'fields' => [
'adapter_settings[key]' => [
'type' => 'text',
'value' => $settings['key'] ?? '',
'required' => true
]
]
],
[
'title' => 'Secret',
'desc' => 'Enter your Cloudflare R2 Secret',
'fields' => [
'adapter_settings[secret]' => [
'type' => 'text',
'value' => $settings['secret'] ?? '',
'required' => true
]
]
],
[
'title' => 'Bucket Name',
'desc' => 'Enter the name of your Cloudflare R2 Bucket',
'fields' => [
'adapter_settings[bucket]' => [
'type' => 'text',
'value' => $settings['bucket'] ?? '',
'required' => true
]
]
],
[
'title' => 'Path',
'desc' => 'Enter the path inside your Cloudflare R2 Bucket',
'fields' => [
'server_path' => [
'type' => 'text',
'value' => $settings['server_path'] ?? '',
'required' => false
]
]
],
[
'title' => 'Url',
'desc' => 'Enter the url used to access your Cloudflare R2 Bucket',
'fields' => [
'url' => [
'type' => 'text',
'value' => $settings['url'] ?? '',
'required' => true
]
]
]
];
}
public function getBaseUrl()
{
return implode('/', array_filter([
'https://' . $this->settings['account_id'] . '.r2.cloudflarestorage.com',
$this->getBucket(),
$this->getPathPrefix()
]));
}
/**
* Get the object acl presented as a visibility.
*
* @param string $path
*
* @return string
*/
protected function getRawVisibility($path)
{
return Flysystem\AdapterInterface::VISIBILITY_PRIVATE;
}
}