|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TusPhp; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request as HttpRequest; |
|
6
|
|
|
|
|
7
|
|
|
class Request |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var HttpRequest */ |
|
10
|
|
|
protected $request; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Request constructor. |
|
14
|
|
|
*/ |
|
15
|
1 |
|
public function __construct() |
|
16
|
|
|
{ |
|
17
|
1 |
|
if (null === $this->request) { |
|
18
|
1 |
|
$this->request = HttpRequest::createFromGlobals(); |
|
19
|
|
|
} |
|
20
|
1 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Get http method from current request. |
|
24
|
|
|
* |
|
25
|
|
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
1 |
|
public function method() : string |
|
28
|
|
|
{ |
|
29
|
1 |
|
return $this->request->method(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get upload key from url. |
|
34
|
|
|
* |
|
35
|
|
|
* @return null|string |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function key() |
|
38
|
|
|
{ |
|
39
|
1 |
|
return $this->request->segment(2); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Supported http requests. |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function allowedHttpVerbs() : array |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
1 |
|
HttpRequest::METHOD_GET, |
|
51
|
1 |
|
HttpRequest::METHOD_POST, |
|
52
|
1 |
|
HttpRequest::METHOD_PATCH, |
|
53
|
1 |
|
HttpRequest::METHOD_DELETE, |
|
54
|
1 |
|
HttpRequest::METHOD_HEAD, |
|
55
|
1 |
|
HttpRequest::METHOD_OPTIONS, |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Retrieve a header from the request. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $key |
|
63
|
|
|
* @param string|array|null $default |
|
64
|
|
|
* |
|
65
|
|
|
* @return string|null |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function header(string $key, $default = null) |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->request->header($key, $default); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the root URL for the request. |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function url() : string |
|
78
|
|
|
{ |
|
79
|
1 |
|
return $this->request->root(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Extract metadata from header. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $key |
|
86
|
|
|
* @param string $value |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function extractFromHeader(string $key, string $value) : array |
|
91
|
|
|
{ |
|
92
|
2 |
|
$meta = $this->header($key); |
|
93
|
|
|
|
|
94
|
2 |
|
if (false !== strpos($meta, $value)) { |
|
95
|
2 |
|
$meta = trim(str_replace($value, '', $meta)); |
|
96
|
|
|
|
|
97
|
2 |
|
return explode(' ', $meta) ?? []; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
return []; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Extract base64 encoded filename from header. |
|
105
|
|
|
* |
|
106
|
|
|
* @return string|null |
|
107
|
|
|
*/ |
|
108
|
4 |
|
public function extractFileName() |
|
109
|
|
|
{ |
|
110
|
4 |
|
$meta = $this->header('Upload-Metadata'); |
|
111
|
|
|
|
|
112
|
4 |
|
if (empty($meta)) { |
|
113
|
1 |
|
return null; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
3 |
|
if (false !== strpos($meta, ',')) { |
|
117
|
2 |
|
$pieces = explode(',', $meta); |
|
118
|
2 |
|
list(, $file) = explode(' ', $pieces[0]); |
|
119
|
|
|
} else { |
|
120
|
1 |
|
list(, $file) = explode(' ', $meta); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
3 |
|
return base64_decode($file); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Extract partials from header. |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function extractPartials() : array |
|
132
|
|
|
{ |
|
133
|
1 |
|
return $this->extractFromHeader('Upload-Concat', 'final;'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Check if this is a partial upload request. |
|
138
|
|
|
* |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public function isPartial() : bool |
|
142
|
|
|
{ |
|
143
|
1 |
|
return $this->header('Upload-Concat') === 'partial'; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Check if this is a final concatenation request. |
|
148
|
|
|
* |
|
149
|
|
|
* @return bool |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function isFinal() : bool |
|
152
|
|
|
{ |
|
153
|
1 |
|
return false !== strpos($this->header('Upload-Concat'), 'final;'); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get request. |
|
158
|
|
|
* |
|
159
|
|
|
* @return HttpRequest |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function getRequest() |
|
162
|
|
|
{ |
|
163
|
1 |
|
return $this->request; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|