-
Notifications
You must be signed in to change notification settings - Fork 22
/
StreamWrapperConfiguration.php
443 lines (391 loc) · 11.3 KB
/
StreamWrapperConfiguration.php
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
namespace Drupal\amazons3;
use Drupal\amazons3\DrupalAdapter\Bootstrap;
use Drupal\amazons3\Matchable\BasicPath;
use Drupal\amazons3\Matchable\MatchablePaths;
use Drupal\amazons3\Matchable\PresignedPath;
use Guzzle\Common\Collection;
/**
* Class to manage S3 stream wrapper configuration.
*
* PHP doesn't pass in any parameters when constructing a new stream wrapper.
* One possibility would be to use stream_context_create(), but Drupal doesn't
* use it when registering streams. This makes it near impossible to inject
* configuration, forcing us to rely on variable_get() and a bootstrapped
* database.
*
* This class defaults to using variable_get() and so on, but can be constructed
* manually to disable this behaviour. For this setup, use the various set
* methods to configure the stream wrapper.
*
* @class StreamWrapperConfiguration
* @package Drupal\amazons3
*/
class StreamWrapperConfiguration extends Collection {
use Bootstrap;
/**
* Generate a configuration object from an array.
*
* @param array $config
* (optional) An array of configuration data. Each key is a lower-cased
* string corresponding with a set method.
* @param array $defaults
* (optional) Override the default settings.
* @param array $required
* (optional) Override the required settings.
*
* @return StreamWrapperConfiguration
* The stream wrapper configuration.
*/
public static function fromConfig(array $config = array(), array $defaults = array(), array $required = array()) {
if (empty($defaults)) {
$defaults = self::defaults();
}
if (empty($required)) {
$required = self::required();
}
$data = $config + $defaults;
if ($data['caching']) {
$required[] = 'expiration';
}
if ($missing = array_diff($required, array_keys(array_filter($data, function($item) {
return !is_null($item) && $item !== '';
})))) {
throw new \InvalidArgumentException('Config is missing the following keys: ' . implode(', ', $missing));
}
if (!$data['domain']) {
$data['domain'] = self::getS3Domain($data['bucket']);
}
return new static($data);
}
/**
* @return array
*/
protected static function defaults() {
$defaults = array(
'hostname' => NULL,
'bucket' => NULL,
'region' => NULL,
'torrentPaths' => new MatchablePaths(),
'presignedPaths' => new MatchablePaths(),
'saveAsPaths' => new MatchablePaths(),
'cloudFront' => FALSE,
'cloudFrontPrivateKey' => NULL,
'cloudFrontKeyPairId' => NULL,
'domain' => NULL,
'caching' => FALSE,
'cacheLifetime' => NULL,
'reducedRedundancyPaths' => new MatchablePaths(),
);
return $defaults;
}
/**
* {@inheritdoc}
*/
protected static function required() {
$required = array(
'bucket',
'region',
);
return $required;
}
/**
* Get the S3 domain for a bucket.
*
* @param string $bucket
* The bucket to get the domain for.
* @return string
* The S3 domain, such as bucket.s3.amazonaws.com.
*/
protected static function getS3Domain($bucket) {
$domain = StreamWrapper::S3_API_DOMAIN;
// If the bucket does not contain dots, the S3 SDK generates URLs that
// use the bucket name in the host.
if (strpos($bucket, '.') === FALSE) {
$domain = $bucket . '.' . $domain;
}
return $domain;
}
/**
* Get the API hostname.
*
* @return string
*/
public function getHostname() {
return $this->data['hostname'];
}
/**
* Set the API hostname.
*
* @param string $hostname
*/
public function setHostname($hostname) {
$this->data['hostname'] = $hostname;
}
/**
* Get the bucket.
*
* @return string
*/
public function getBucket() {
return $this->data['bucket'];
}
/**
* Set the bucket.
*
* @param string $bucket
*/
public function setBucket($bucket) {
$this->data['bucket'] = $bucket;
}
/**
* Get the torrent paths.
*
* @return MatchablePaths
*/
public function getTorrentPaths() {
return $this->data['torrentPaths'];
}
/**
* Set the array of paths to serve as torrents.
*
* @param MatchablePaths $torrentPaths
*/
public function setTorrentPaths(MatchablePaths $torrentPaths) {
$this->data['torrentPaths'] = $torrentPaths;
}
/**
* Get the array of paths to serve with presigned URLs.
*
* @return MatchablePaths
*/
public function getPresignedPaths() {
return $this->data['presignedPaths'];
}
/**
* Set the array of paths to serve with presigned URLs.
*
* @param MatchablePaths $presignedPaths
*/
public function setPresignedPaths(MatchablePaths $presignedPaths) {
$this->data['presignedPaths'] = $presignedPaths;
}
/**
* Get the region associated with this stream wrapper.
*
* @return string
* The region, such as 'us-east-1'.
*/
public function getRegion() {
return $this->data['region'];
}
/**
* Set the region associated with this stream wrapper.
*
* @param string $region
* The region, such as 'us-east-1'.
*/
public function setRegion($region) {
$this->data['region'] = $region;
}
/**
* Return the paths to force to download instead of viewing in the browser.
*
* @return MatchablePaths
*/
public function getSaveAsPaths() {
return $this->data['saveAsPaths'];
}
/**
* Set the array of paths to force to download.
*
* @param MatchablePaths $saveAsPaths
*/
public function setSaveAsPaths(MatchablePaths $saveAsPaths) {
$this->data['saveAsPaths'] = $saveAsPaths;
}
/**
* Return if files should be served with CloudFront.
*
* @return bool
*/
public function isCloudFront() {
return $this->data['cloudFront'];
}
/**
* Set if objects should be served with CloudFront.
*/
public function serveWithCloudFront() {
$this->data['cloudFront'] = TRUE;
}
/**
* Set the CloudFront credentials to use.
*
* @param string $path
* The path to the file containing the private key.
* @param string $keyPairId
* The key pair ID.
*/
public function setCloudFrontCredentials($path, $keyPairId) {
if (!file_exists($path)) {
throw new \InvalidArgumentException("$path does not exist.");
}
$this->data['cloudFrontPrivateKey'] = $path;
$this->data['cloudFrontKeyPairId'] = $keyPairId;
}
/**
* @return \Aws\CloudFront\CloudFrontClient
*/
public function getCloudFront() {
if (!$this->isCloudFront()) {
throw new \LogicException('CloudFront is not enabled.');
}
return CloudFrontClient::factory(array(
'private_key' => $this->data['cloudFrontPrivateKey'],
'key_pair_id' => $this->data['cloudFrontKeyPairId'],
));
}
/**
* Set if objects should be served with S3 directly.
*/
public function serveWithS3() {
$this->data['cloudFront'] = FALSE;
}
/**
* @return string
*/
public function getDomain() {
return $this->data['domain'];
}
/**
* @param string $domain
*/
public function setDomain($domain) {
$this->data['domain'] = $domain;
}
/**
* @return boolean
*/
public function isCaching() {
return (bool) $this->data['caching'];
}
/**
* Enable metadata caching.
*/
public function enableCaching() {
$this->data['caching'] = TRUE;
}
/**
* Disable metadata caching.
*/
public function disableCaching() {
$this->data['caching'] = FALSE;
$this->data['expiration'] = NULL;
}
/**
* Set the cache expiration.
*
* This method must only be called if caching is enabled. Use CACHE_PERMANENT
* to cache with no expiration.
*
* @param int $expiration
*/
public function setCacheLifetime($expiration) {
if (!$this->isCaching()) {
throw new \LogicException('Caching must be enabled before setting a expiration.');
}
$this->data['expiration'] = $expiration;
}
/**
* @return int
* The cache expiration, in seconds. Zero means expiration is disabled.
*/
public function getCacheLifetime() {
return $this->data['expiration'];
}
/**
* @return MatchablePaths
*/
public function getReducedRedundancyPaths() {
return $this->data['reducedRedundancyPaths'];
}
/**
* @param MatchablePaths $reducedRedundancyPaths
*/
public function setReducedRedundancyPaths(MatchablePaths $reducedRedundancyPaths) {
$this->data['reducedRedundancyPaths'] = $reducedRedundancyPaths;
}
/**
* Set the stream wrapper configuration using Drupal variables.
*
* @throws \InvalidArgumentException
* Thrown when the StreamWrapper configuration is invalid, such as when a
* bucket is not defined.
*
* @return StreamWrapperConfiguration
* A StreamWrapperConfiguration object.
*/
public static function fromDrupalVariables() {
$config = self::fromConfig(array(
'bucket' => static::variable_get('amazons3_bucket', NULL),
'region' => static::variable_get('amazons3_region', NULL)
));
$defaults = $config->defaults();
$config->setHostname(static::variable_get('amazons3_hostname', $defaults['hostname']));
// CNAME support for customizing S3 URLs.
if (static::variable_get('amazons3_cname', FALSE)) {
$domain = static::variable_get('amazons3_domain', $defaults['domain']);
if (strlen($domain) > 0) {
$config->setDomain($domain);
}
else {
$config->setDomain($config->getBucket());
}
if (static::variable_get('amazons3_cloudfront', $defaults['cloudFront'])) {
$path = static::variable_get('amazons3_cloudfront_private_key', $defaults['cloudFrontPrivateKey']);
$keyPairId = static::variable_get('amazons3_cloudfront_keypair_id', $defaults['cloudFrontKeyPairId']);
$config->setCloudFrontCredentials($path, $keyPairId);
$config->serveWithCloudFront();
}
}
else {
$config->setDomain(static::getS3Domain($config->getBucket()));
}
// Check whether local file caching is turned on.
if (static::variable_get('amazons3_cache', $defaults['caching'])) {
$config->enableCaching();
$config->setCacheLifetime(static::variable_get('amazons3_cache_expiration', NULL));
}
else {
$config->disableCaching();
}
// Torrent list.
$torrentPaths = static::variable_get('amazons3_torrents', array());
$paths = BasicPath::factory($torrentPaths);
if (!empty($paths)) {
$config->setTorrentPaths(new MatchablePaths($paths));
}
// Presigned url-list.
$presigned_urls = static::variable_get('amazons3_presigned_urls', array());
$paths = array();
foreach ($presigned_urls as $presigned_url) {
$paths[] = new PresignedPath($presigned_url['pattern'], $presigned_url['timeout']);
}
if (!empty($paths)) {
$config->setPresignedPaths(new MatchablePaths($paths));
}
// Force "save as" list.
$saveAsPaths = static::variable_get('amazons3_saveas', array());
$paths = BasicPath::factory($saveAsPaths);
if (!empty($paths)) {
$config->setSaveAsPaths(new MatchablePaths($paths));
}
// Reduced Redundancy Storage.
$rrsPaths = static::variable_get('amazons3_rrs', array());
$paths = BasicPath::factory($rrsPaths);
if (!empty($paths)) {
$config->setReducedRedundancyPaths(new MatchablePaths($paths));
}
return $config;
}
}