also looking at this
Storage Class
#59
3 files
+175
-105
| @@ -2,9 +2,6 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Meema\MeemaApi\Models; |
| 4 | 4 | |
| 5 | use GuzzleHttp\Client as GuzzleClient; | |
| 6 | use GuzzleHttp\Psr7; | |
| 7 | use GuzzleHttp\Psr7\Request; | |
| 8 | 5 | use Meema\MeemaApi\Client; |
| 9 | 6 | use Meema\MeemaApi\Exceptions\InvalidFormatException; |
| 10 | 7 | use Meema\MeemaApi\Response\Response; |
| @@ -179,69 +176,6 @@ class Media | ||
| 179 | 176 | return $this->client->request('DELETE', "media/{$id}", ['media_id' => $id]); |
| 180 | 177 | } |
| 181 | 178 | |
| 182 | /** | |
| 183 | * Upload a media file. | |
| 184 | * | |
| 185 | * @param string $path | |
| 186 | * | |
| 187 | * @return array | |
| 188 | */ | |
| 189 | public function upload($path) | |
| 190 | { | |
| 191 | $file = fopen($path, 'r'); | |
| 192 | $stream = Psr7\stream_for($file); | |
| 193 | ||
| 194 | $fileName = basename($path); | |
| 195 | $mimeType = mime_content_type($file); | |
| 196 | ||
| 197 | $vaporParams = ['content_type' => $mimeType]; | |
| 198 | ||
| 199 | $signedUrl = $this->client->request('POST', 'vapor/signed-storage-url', $vaporParams); | |
| 200 | ||
| 201 | if (is_array($signedUrl) && $signedUrl['url']) { | |
| 202 | $headers = $signedUrl['headers']; | |
| 203 | unset($headers['Host']); | |
| 204 | ||
| 205 | $this->uploadToS3($signedUrl['url'], $headers, $fileName, $stream); | |
| 206 | ||
| 207 | $uploadData = ['key' => $signedUrl['key'], 'file_name' => $fileName]; | |
| 208 | ||
| 209 | $response = $this->client->request('POST', 'upload', $uploadData); | |
| 210 | } | |
| 211 | ||
| 212 | return $response; | |
| 213 | } | |
| 214 | ||
| 215 | /** | |
| 216 | * Upload the file stream to s3. | |
| 217 | * | |
| 218 | * @param string $signedUrl | |
| 219 | * @param array $headers | |
| 220 | * @param string $fileName | |
| 221 | * @param GuzzleHttp\Psr7 $stream | |
| 222 | * | |
| 223 | * @return void | |
| 224 | */ | |
| 225 | protected function uploadToS3($signedUrl, $headers, $fileName, $stream) | |
| 226 | { | |
| 227 | $client = new GuzzleClient(); | |
| 228 | $request = new Request( | |
| 229 | 'PUT', | |
| 230 | $signedUrl, | |
| 231 | ['headers' => json_encode($headers)], | |
| 232 | new Psr7\MultipartStream( | |
| 233 | [ | |
| 234 | [ | |
| 235 | 'name' => $fileName, | |
| 236 | 'contents' => $stream, | |
| 237 | ], | |
| 238 | ] | |
| 239 | ) | |
| 240 | ); | |
| 241 | ||
| 242 | $client->send($request); | |
| 243 | } | |
| 244 | ||
| 245 | 179 | /** |
| 246 | 180 | * Archive a media. |
| 247 | 181 | * |
| @@ -473,45 +407,6 @@ class Media | ||
| 473 | 407 | return $this->client->request('GET', "tags/{$id}/media"); |
| 474 | 408 | } |
| 475 | 409 | |
| 476 | /** | |
| 477 | * Set the visibility for a file. | |
| 478 | * | |
| 479 | * @param string $path | |
| 480 | * @param string $visibility | |
| 481 | * | |
| 482 | * @return array|false file meta data | |
| 483 | */ | |
| 484 | public function setVisibility($path, $visibility) | |
| 485 | { | |
| 486 | return $this->client->request('POST', 'storage/set-visibility', compact('path', 'visibility')); | |
| 487 | } | |
| 488 | ||
| 489 | /** | |
| 490 | * Check whether a file exists. | |
| 491 | * | |
| 492 | * @param string $path | |
| 493 | * | |
| 494 | * @return bool | |
| 495 | */ | |
| 496 | public function has($path) | |
| 497 | { | |
| 498 | $data = $this->client->request('POST', 'storage/has', compact('path')); | |
| 499 | ||
| 500 | return $data['exists'] ?? false; | |
| 501 | } | |
| 502 | ||
| 503 | /** | |
| 504 | * Get all the meta data of a file or directory. | |
| 505 | * | |
| 506 | * @param string $path | |
| 507 | * | |
| 508 | * @return false|array | |
| 509 | */ | |
| 510 | public function getMetadata($path) | |
| 511 | { | |
| 512 | return $this->client->request('POST', 'storage/metadata', compact('path')); | |
| 513 | } | |
| 514 | ||
| 515 | 410 | /** |
| 516 | 411 | * Fetch child relations for this instance. |
| 517 | 412 | * |