1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Twistor\Flysystem; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
8
|
|
|
use League\Flysystem\AdapterInterface; |
9
|
|
|
use League\Flysystem\Config; |
10
|
|
|
use League\Flysystem\Util\MimeType; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Uses Guzzle as a backend for HTTP URLs. |
14
|
|
|
*/ |
15
|
|
|
class GuzzleAdapter implements AdapterInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The base URL. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $base; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The Guzzle HTTP client. |
26
|
|
|
* |
27
|
|
|
* @var \GuzzleHttp\ClientInterface |
28
|
|
|
*/ |
29
|
|
|
protected $client; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The visibility of this adapter. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $visibility = AdapterInterface::VISIBILITY_PUBLIC; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructs an Http object. |
40
|
|
|
* |
41
|
|
|
* @param string $base The base URL. |
42
|
|
|
* @param \GuzzleHttp\ClientInterface $client An optional Guzzle client. |
43
|
|
|
*/ |
44
|
3 |
|
public function __construct($base, ClientInterface $client = null) |
45
|
|
|
{ |
46
|
3 |
|
$this->client = $client ?: new Client(); |
47
|
|
|
|
48
|
3 |
|
$parsed = parse_url($base); |
49
|
3 |
|
$this->base = $parsed['scheme'] . '://'; |
50
|
|
|
|
51
|
3 |
|
if (isset($parsed['user'])) { |
52
|
3 |
|
$this->visibility = AdapterInterface::VISIBILITY_PRIVATE; |
53
|
3 |
|
$this->base .= $parsed['user']; |
54
|
|
|
|
55
|
3 |
View Code Duplication |
if (isset($parsed['pass']) && $parsed['pass'] !== '') { |
|
|
|
|
56
|
3 |
|
$this->base .= ':' . $parsed['pass']; |
57
|
3 |
|
} |
58
|
|
|
|
59
|
3 |
|
$this->base .= '@'; |
60
|
3 |
|
}; |
61
|
|
|
|
62
|
3 |
|
$this->base .= $parsed['host'] . '/'; |
63
|
|
|
|
64
|
3 |
View Code Duplication |
if (isset($parsed['path']) && $parsed['path'] !== '/') { |
|
|
|
|
65
|
3 |
|
$this->base .= trim($parsed['path'], '/') . '/'; |
66
|
3 |
|
} |
67
|
3 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns the base URL. |
71
|
|
|
* |
72
|
|
|
* @return string The base URL. |
73
|
|
|
*/ |
74
|
3 |
|
public function getBaseUrl() |
75
|
|
|
{ |
76
|
3 |
|
return $this->base; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
3 |
|
public function copy($path, $newpath) |
83
|
|
|
{ |
84
|
3 |
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
3 |
|
public function createDir($path, Config $config) |
91
|
|
|
{ |
92
|
3 |
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritdoc |
97
|
|
|
*/ |
98
|
3 |
|
public function delete($path) |
99
|
|
|
{ |
100
|
3 |
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritdoc |
105
|
|
|
*/ |
106
|
3 |
|
public function deleteDir($path) |
107
|
|
|
{ |
108
|
3 |
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritdoc |
113
|
|
|
*/ |
114
|
3 |
|
public function getMetadata($path) |
115
|
|
|
{ |
116
|
3 |
|
if (! $response = $this->head($path)) { |
117
|
3 |
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
if ($mimetype = $response->getHeader('Content-Type')) { |
121
|
3 |
|
list($mimetype) = explode(';', reset($mimetype), 2); |
122
|
3 |
|
$mimetype = trim($mimetype); |
123
|
3 |
|
} else { |
124
|
|
|
// Remove any query strings or fragments. |
125
|
3 |
|
list($path) = explode('#', $path, 2); |
126
|
3 |
|
list($path) = explode('?', $path, 2); |
127
|
3 |
|
$extension = pathinfo($path, PATHINFO_EXTENSION); |
128
|
3 |
|
$mimetype = $extension ? MimeType::detectByFileExtension($extension) : 'text/plain'; |
129
|
|
|
} |
130
|
|
|
|
131
|
3 |
|
$last_modified = $response->getHeader('Last-Modified'); |
132
|
3 |
|
$length = $response->getHeader('Content-Length'); |
133
|
|
|
|
134
|
|
|
return [ |
135
|
3 |
|
'type' => 'file', |
136
|
3 |
|
'path' => $path, |
137
|
3 |
|
'timestamp' => (int) strtotime(reset($last_modified)), |
138
|
3 |
|
'size' => (int) reset($length), |
139
|
3 |
|
'visibility' => $this->visibility, |
140
|
3 |
|
'mimetype' => $mimetype, |
141
|
3 |
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritdoc |
146
|
|
|
*/ |
147
|
3 |
|
public function getMimetype($path) |
148
|
|
|
{ |
149
|
3 |
|
return $this->getMetadata($path); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @inheritdoc |
154
|
|
|
*/ |
155
|
3 |
|
public function getSize($path) |
156
|
|
|
{ |
157
|
3 |
|
return $this->getMetadata($path); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritdoc |
162
|
|
|
*/ |
163
|
3 |
|
public function getTimestamp($path) |
164
|
|
|
{ |
165
|
3 |
|
return $this->getMetadata($path); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @inheritdoc |
170
|
|
|
*/ |
171
|
3 |
|
public function getVisibility($path) |
172
|
|
|
{ |
173
|
|
|
return [ |
174
|
3 |
|
'path' => $path, |
175
|
3 |
|
'visibility' => $this->visibility, |
176
|
3 |
|
]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @inheritdoc |
181
|
|
|
*/ |
182
|
3 |
|
public function has($path) |
183
|
|
|
{ |
184
|
3 |
|
if (! $response = $this->head($path)) { |
185
|
3 |
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
3 |
|
return $response->getStatusCode() === 200; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @inheritdoc |
193
|
|
|
*/ |
194
|
3 |
|
public function listContents($directory = '', $recursive = false) |
195
|
|
|
{ |
196
|
3 |
|
return []; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @inheritdoc |
201
|
|
|
*/ |
202
|
3 |
|
public function read($path) |
203
|
|
|
{ |
204
|
3 |
|
if (! $response = $this->get($path)) { |
205
|
3 |
|
return false; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return [ |
209
|
3 |
|
'path' => $path, |
210
|
3 |
|
'contents' => (string) $response->getBody(), |
211
|
3 |
|
]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @inheritdoc |
216
|
|
|
*/ |
217
|
3 |
|
public function readStream($path) |
218
|
|
|
{ |
219
|
3 |
|
if (! $response = $this->get($path)) { |
220
|
3 |
|
return false; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return [ |
224
|
3 |
|
'path' => $path, |
225
|
3 |
|
'stream' => $response->getBody()->detach(), |
226
|
3 |
|
]; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @inheritdoc |
231
|
|
|
*/ |
232
|
3 |
|
public function rename($path, $newpath) |
233
|
|
|
{ |
234
|
3 |
|
return false; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @inheritdoc |
239
|
|
|
*/ |
240
|
3 |
|
public function setVisibility($path, $visibility) |
241
|
|
|
{ |
242
|
3 |
|
if ($visibility === $this->visibility) { |
243
|
3 |
|
return $this->getVisibility($path); |
244
|
|
|
} |
245
|
|
|
|
246
|
3 |
|
return false; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @inheritdoc |
251
|
|
|
*/ |
252
|
3 |
|
public function update($path, $contents, Config $conf) |
253
|
|
|
{ |
254
|
3 |
|
return false; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @inheritdoc |
259
|
|
|
*/ |
260
|
3 |
|
public function updateStream($path, $resource, Config $config) |
261
|
|
|
{ |
262
|
3 |
|
return false; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @inheritdoc |
267
|
|
|
*/ |
268
|
3 |
|
public function write($path, $contents, Config $config) |
269
|
|
|
{ |
270
|
3 |
|
return false; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @inheritdoc |
275
|
|
|
*/ |
276
|
3 |
|
public function writeStream($path, $resource, Config $config) |
277
|
|
|
{ |
278
|
3 |
|
return false; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Performs a GET request. |
283
|
|
|
* |
284
|
|
|
* @param string $path The path to GET. |
285
|
|
|
* |
286
|
|
|
* @return \GuzzleHttp\Psr7\Response|false The response or false if failed. |
287
|
|
|
*/ |
288
|
6 |
View Code Duplication |
protected function get($path) |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
try { |
291
|
6 |
|
$response = $this->client->get($this->base . $path); |
292
|
6 |
|
} catch (BadResponseException $e) { |
293
|
6 |
|
return false; |
294
|
|
|
} |
295
|
|
|
|
296
|
6 |
|
if ($response->getStatusCode() !== 200) { |
297
|
3 |
|
return false; |
298
|
|
|
} |
299
|
|
|
|
300
|
6 |
|
return $response; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Performs a HEAD request. |
305
|
|
|
* |
306
|
|
|
* @param string $path The path to HEAD. |
307
|
|
|
* |
308
|
|
|
* @return \GuzzleHttp\Psr7\Response|false The response or false if failed. |
309
|
|
|
*/ |
310
|
6 |
View Code Duplication |
protected function head($path) |
|
|
|
|
311
|
|
|
{ |
312
|
|
|
try { |
313
|
6 |
|
$response = $this->client->head($this->base . $path); |
314
|
6 |
|
} catch (BadResponseException $e) { |
315
|
6 |
|
return false; |
316
|
|
|
} |
317
|
|
|
|
318
|
6 |
|
if ($response->getStatusCode() !== 200) { |
319
|
3 |
|
return false; |
320
|
|
|
} |
321
|
|
|
|
322
|
6 |
|
return $response; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.