1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EddTurtle\DirectUpload; |
4
|
|
|
|
5
|
|
|
use EddTurtle\DirectUpload\Exceptions\InvalidOptionException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Options |
9
|
|
|
* |
10
|
|
|
* @todo |
11
|
|
|
* |
12
|
|
|
* @package EddTurtle\DirectUpload |
13
|
|
|
*/ |
14
|
|
|
class Options |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Default options, these can be overwritten within the constructor. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $options = [ |
23
|
|
|
|
24
|
|
|
// If the upload is a success, this is the http code we get back from S3. |
25
|
|
|
// By default this will be a 201 Created. |
26
|
|
|
'success_status' => 201, |
27
|
|
|
|
28
|
|
|
// If the file should be private/public-read/public-write. |
29
|
|
|
// This is file specific, not bucket. More info: http://amzn.to/1SSOgwO |
30
|
|
|
'acl' => 'private', |
31
|
|
|
|
32
|
|
|
// The file's name on s3, can be set with JS by changing the input[name="key"]. |
33
|
|
|
// ${filename} will just mean the original filename of the file being uploaded. |
34
|
|
|
'default_filename' => '${filename}', |
35
|
|
|
|
36
|
|
|
// The maximum file size of an upload in MB. Will refuse with a EntityTooLarge |
37
|
|
|
// and 400 Bad Request if you exceed this limit. |
38
|
|
|
'max_file_size' => 500, |
39
|
|
|
|
40
|
|
|
// Request expiration time, specified in relative time format or in seconds. |
41
|
|
|
// minimum of 1 (+1 second), maximum of 604800 (+7 days) |
42
|
|
|
'expires' => '+6 hours', |
43
|
|
|
|
44
|
|
|
// Server will check that the filename starts with this prefix and fail |
45
|
|
|
// with a AccessDenied 403 if not. |
46
|
|
|
'valid_prefix' => '', |
47
|
|
|
|
48
|
|
|
// Strictly only allow a single content type, blank will allow all. Will fail |
49
|
|
|
// with a AccessDenied 403 is this condition is not met. |
50
|
|
|
'content_type' => '', |
51
|
|
|
|
52
|
|
|
// Sets whether AWS server side encryption should be applied to the uploaded files, |
53
|
|
|
// so that files will be encrypted with AES256 when at rest. |
54
|
|
|
'encryption' => false, |
55
|
|
|
|
56
|
|
|
// Allow S3 compatible solutions by specifying the domain it should POST to. Must be |
57
|
|
|
// a valid url (inc. http/https) otherwise will throw InvalidOptionException. |
58
|
|
|
'custom_url' => null, |
59
|
|
|
|
60
|
|
|
// Set Amazon S3 Transfer Acceleration |
61
|
|
|
'accelerate' => false, |
62
|
|
|
|
63
|
|
|
// Any additional inputs to add to the form. This is an array of name => value |
64
|
|
|
// pairs e.g. ['Content-Disposition' => 'attachment'] |
65
|
|
|
'additional_inputs' => [] |
66
|
|
|
|
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
public function __construct($options = []) |
70
|
|
|
{ |
71
|
|
|
$this->setOptions($options); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get all options. |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getOptions(): array |
80
|
|
|
{ |
81
|
|
|
return $this->options; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function get($name) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
if (!array_key_exists($name, $this->options)) { |
87
|
|
|
throw new InvalidOptionException("Invalid option given to get()"); |
88
|
|
|
} |
89
|
|
|
return $this->options[$name]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set/overwrite any default options. |
94
|
|
|
* |
95
|
|
|
* @param array $options any options to override. |
96
|
|
|
*/ |
97
|
|
|
public function setOptions(array $options): void |
98
|
|
|
{ |
99
|
|
|
// Overwrite default options |
100
|
|
|
$this->options = $options + $this->options; |
101
|
|
|
|
102
|
|
|
$this->options['acl'] = new Acl($this->options['acl']); |
103
|
|
|
|
104
|
|
|
// Return HTTP code must be a string |
105
|
|
|
$this->options['success_status'] = (string)$this->options['success_status']; |
106
|
|
|
|
107
|
|
|
// Encryption option is just a helper to set this header, but we need to set it early on so it |
108
|
|
|
// affects both the policy and the inputs generated. |
109
|
|
|
if ($this->options['encryption']) { |
110
|
|
|
$this->options['additional_inputs']['X-amz-server-side-encryption'] = 'AES256'; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function set(string $name, $value): void |
115
|
|
|
{ |
116
|
|
|
if (!array_key_exists($name, $this->options)) { |
117
|
|
|
throw new InvalidOptionException("Invalid option given to set()"); |
118
|
|
|
} |
119
|
|
|
$this->options[$name] = $value; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.