ReviewOS

also looking at this

meemalabs/flysystem-meema

Initial implementation

#2
Merged harlekoy wants to merge develop into master
4 files +3827 -0

Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.

src/Client.phpdeleted+0-51
Changes to src/Client.php
@@ -1,51 +0,0 @@
1<?php
2
3namespace Meema\Flysystem;
4
5use GuzzleHttp\Client as GuzzleClient;
6
7class Client
8{
9 /**
10 * Construct Meema client.
11 *
12 * @param string $accessKey
13 */
14 public function __construct($accessKey, $config = [])
15 {
16 $this->accessKey = $accessKey;
17
18 $this->config = $config;
19
20 $this->client = new GuzzleClient([
21 'base_uri' => $url ?? 'http://meema-api.test/api/',
22 ]);
23 }
24
25 /**
26 * Handle the API request.
27 *
28 * @param string $method
29 * @param string $path
30 */
31 public function request($method, $path, $data = null)
32 {
33 $content = $this->client->request($method, $path, [
34 'query' => $data,
35 'headers' => [
36 'Content-Type' => 'application/json',
37 'Authorization' => "Bearer {$this->accessKey}",
38 ],
39 ])
40 ->getBody()
41 ->getContents();
42
43 $body = json_decode($content, true);
44
45 if ($body && array_key_exists('data', $body)) {
46 return $body['data'];
47 }
48
49 return $body;
50 }
51}
src/MeemaAdapter.phpmodified+5-73
Changes to src/MeemaAdapter.php
@@ -2,9 +2,6 @@
22
33namespace Meema\Flysystem;
44
5use GuzzleHttp\Client as GuzzleClient;
6use GuzzleHttp\Psr7;
7use GuzzleHttp\Psr7\Request;
85use League\Flysystem\Adapter\AbstractAdapter;
96use League\Flysystem\Config;
107use Meema\MeemaApi\Client;
@@ -32,7 +29,7 @@ class MeemaAdapter extends AbstractAdapter
3229 */
3330 public function write($path, $contents, Config $config)
3431 {
35 return $this->upload($path);
32 return $this->client->media()->upload($path);
3633 }
3734
3835 /**
@@ -46,7 +43,7 @@ class MeemaAdapter extends AbstractAdapter
4643 */
4744 public function writeStream($path, $resource, Config $config)
4845 {
49 return $this->upload($path);
46 return $this->client->media()->upload($path);
5047 }
5148
5249 /**
@@ -150,7 +147,7 @@ class MeemaAdapter extends AbstractAdapter
150147 */
151148 public function setVisibility($path, $visibility)
152149 {
153 return $this->client->request('POST', 'aws/set-visibility', compact('path', 'visibility'));
150 return $this->client->media()->setVisibility($path, $visibility);
154151 }
155152
156153 /**
@@ -162,9 +159,7 @@ class MeemaAdapter extends AbstractAdapter
162159 */
163160 public function has($path)
164161 {
165 $data = $this->client->request('POST', 'aws/has', compact('path'));
166
167 return $data['exists'] ?? false;
162 return $this->client->media()->has($path);
168163 }
169164
170165 /**
@@ -213,7 +208,7 @@ class MeemaAdapter extends AbstractAdapter
213208 */
214209 public function getMetadata($path)
215210 {
216 return $this->client->request('POST', 'aws/metadata', compact('path'));
211 return $this->client->media()->getMetadata($path);
217212 }
218213
219214 /**
@@ -263,67 +258,4 @@ class MeemaAdapter extends AbstractAdapter
263258 {
264259 return $this->getMetadata($path);
265260 }
266
267 /**
268 * Upload a media file.
269 *
270 * @param string $path
271 *
272 * @return array
273 */
274 protected function upload($path)
275 {
276 $file = fopen($path, 'r');
277 $stream = Psr7\stream_for($file);
278
279 $fileName = basename($path);
280 $mimeType = mime_content_type($file);
281
282 $vaporParams = ['content_type' => $mimeType];
283
284 $signedUrl = $this->client->request('POST', 'vapor/signed-storage-url', $vaporParams);
285
286 if (is_array($signedUrl) && $signedUrl['url']) {
287 $headers = $signedUrl['headers'];
288 unset($headers['Host']);
289
290 $this->uploadToS3($signedUrl['url'], $headers, $fileName, $stream);
291
292 $uploadData = ['key' => $signedUrl['key'], 'file_name' => $fileName];
293
294 $response = $this->client->request('POST', 'upload', $uploadData);
295 }
296
297 return $response;
298 }
299
300 /**
301 * Upload the file stream to s3.
302 *
303 * @param string $signedUrl
304 * @param array $headers
305 * @param string $fileName
306 * @param GuzzleHttp\Psr7 $stream
307 *
308 * @return void
309 */
310 protected function uploadToS3($signedUrl, $headers, $fileName, $stream)
311 {
312 $client = new GuzzleClient();
313 $request = new Request(
314 'PUT',
315 $signedUrl,
316 ['headers' => json_encode($headers)],
317 new Psr7\MultipartStream(
318 [
319 [
320 'name' => $fileName,
321 'contents' => $stream,
322 ],
323 ]
324 )
325 );
326
327 $client->send($request);
328 }
329261}