also looking at this
Initial implementation
#2
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.
| @@ -1,51 +0,0 @@ | ||
| 1 | <?php | |
| 2 | ||
| 3 | namespace Meema\Flysystem; | |
| 4 | ||
| 5 | use GuzzleHttp\Client as GuzzleClient; | |
| 6 | ||
| 7 | class 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 | } | |
| @@ -2,9 +2,6 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Meema\Flysystem; |
| 4 | 4 | |
| 5 | use GuzzleHttp\Client as GuzzleClient; | |
| 6 | use GuzzleHttp\Psr7; | |
| 7 | use GuzzleHttp\Psr7\Request; | |
| 8 | 5 | use League\Flysystem\Adapter\AbstractAdapter; |
| 9 | 6 | use League\Flysystem\Config; |
| 10 | 7 | use Meema\MeemaApi\Client; |
| @@ -32,7 +29,7 @@ class MeemaAdapter extends AbstractAdapter | ||
| 32 | 29 | */ |
| 33 | 30 | public function write($path, $contents, Config $config) |
| 34 | 31 | { |
| 35 | return $this->upload($path); | |
| 32 | return $this->client->media()->upload($path); | |
| 36 | 33 | } |
| 37 | 34 | |
| 38 | 35 | /** |
| @@ -46,7 +43,7 @@ class MeemaAdapter extends AbstractAdapter | ||
| 46 | 43 | */ |
| 47 | 44 | public function writeStream($path, $resource, Config $config) |
| 48 | 45 | { |
| 49 | return $this->upload($path); | |
| 46 | return $this->client->media()->upload($path); | |
| 50 | 47 | } |
| 51 | 48 | |
| 52 | 49 | /** |
| @@ -150,7 +147,7 @@ class MeemaAdapter extends AbstractAdapter | ||
| 150 | 147 | */ |
| 151 | 148 | public function setVisibility($path, $visibility) |
| 152 | 149 | { |
| 153 | return $this->client->request('POST', 'aws/set-visibility', compact('path', 'visibility')); | |
| 150 | return $this->client->media()->setVisibility($path, $visibility); | |
| 154 | 151 | } |
| 155 | 152 | |
| 156 | 153 | /** |
| @@ -162,9 +159,7 @@ class MeemaAdapter extends AbstractAdapter | ||
| 162 | 159 | */ |
| 163 | 160 | public function has($path) |
| 164 | 161 | { |
| 165 | $data = $this->client->request('POST', 'aws/has', compact('path')); | |
| 166 | ||
| 167 | return $data['exists'] ?? false; | |
| 162 | return $this->client->media()->has($path); | |
| 168 | 163 | } |
| 169 | 164 | |
| 170 | 165 | /** |
| @@ -213,7 +208,7 @@ class MeemaAdapter extends AbstractAdapter | ||
| 213 | 208 | */ |
| 214 | 209 | public function getMetadata($path) |
| 215 | 210 | { |
| 216 | return $this->client->request('POST', 'aws/metadata', compact('path')); | |
| 211 | return $this->client->media()->getMetadata($path); | |
| 217 | 212 | } |
| 218 | 213 | |
| 219 | 214 | /** |
| @@ -263,67 +258,4 @@ class MeemaAdapter extends AbstractAdapter | ||
| 263 | 258 | { |
| 264 | 259 | return $this->getMetadata($path); |
| 265 | 260 | } |
| 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 | } | |
| 329 | 261 | } |