- chrisSun Oct 25 2026 00:09:39 GMT+0000 (Coordinated Universal Time)
$response may not be defined at this point
also looking at this
Storage Class
#59
3 files
+175
-105
| @@ -7,6 +7,7 @@ use GuzzleHttp\Client as GuzzleClient; | ||
| 7 | 7 | use Meema\MeemaApi\Models\Favorite; |
| 8 | 8 | use Meema\MeemaApi\Models\Folder; |
| 9 | 9 | use Meema\MeemaApi\Models\Media; |
| 10 | use Meema\MeemaApi\Models\Storage; | |
| 10 | 11 | use Meema\MeemaApi\Models\Tag; |
| 11 | 12 | |
| 12 | 13 | class Client |
| @@ -128,6 +129,16 @@ class Client | ||
| 128 | 129 | return new Media($this); |
| 129 | 130 | } |
| 130 | 131 | |
| 132 | /** | |
| 133 | * Initialize the storage model. | |
| 134 | * | |
| 135 | * @return Meema\MeemaApi\Models\Storage | |
| 136 | */ | |
| 137 | public function storage(): Storage | |
| 138 | { | |
| 139 | return new Storage($this); | |
| 140 | } | |
| 141 | ||
| 131 | 142 | /** |
| 132 | 143 | * Initialize the media model. |
| 133 | 144 | * |
| @@ -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 | * |
| @@ -0,0 +1,164 @@ | ||
| 1 | <?php | |
| 2 | ||
| 3 | namespace Meema\MeemaApi\Models; | |
| 4 | ||
| 5 | use GuzzleHttp\Client as GuzzleClient; | |
| 6 | use GuzzleHttp\Psr7; | |
| 7 | use GuzzleHttp\Psr7\Request; | |
| 8 | use Meema\MeemaApi\Client; | |
| 9 | ||
| 10 | class Storage | |
| 11 | { | |
| 12 | /** | |
| 13 | * @var \Meema\MeemaApi\Client | |
| 14 | */ | |
| 15 | protected $client; | |
| 16 | ||
| 17 | /** | |
| 18 | * Construct storage model. | |
| 19 | * | |
| 20 | * @param Meema\MeemaApi\Client $client | |
| 21 | */ | |
| 22 | public function __construct(Client $client) | |
| 23 | { | |
| 24 | $this->client = $client; | |
| 25 | } | |
| 26 | ||
| 27 | /** | |
| 28 | * Upload a media file. | |
| 29 | * | |
| 30 | * @param string $path | |
| 31 | * | |
| 32 | * @return array | |
| 33 | */ | |
| 34 | public function upload($path) | |
| 35 | { | |
| 36 | $file = fopen($path, 'r'); | |
| 37 | $stream = Psr7\stream_for($file); | |
| 38 | ||
| 39 | $fileName = basename($path); | |
| 40 | $mimeType = mime_content_type($file); | |
| 41 | ||
| 42 | $vaporParams = ['content_type' => $mimeType]; | |
| 43 | ||
| 44 | $signedUrl = $this->client->request('POST', 'vapor/signed-storage-url', $vaporParams); | |
| 45 | ||
| 46 | if (is_array($signedUrl) && $signedUrl['url']) { | |
| 47 | $headers = $signedUrl['headers']; | |
| 48 | unset($headers['Host']); | |
| 49 | ||
| 50 | $this->uploadToS3($signedUrl['url'], $headers, $fileName, $stream); | |
| 51 | ||
| 52 | $uploadData = ['key' => $signedUrl['key'], 'file_name' => $fileName]; | |
| 53 | ||
| 54 | $response = $this->client->request('POST', 'upload', $uploadData); | |
| 55 | } | |
| 56 | ||
| 57 | return $response; | |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Upload the file stream to s3. | |
| 62 | * | |
| 63 | * @param string $signedUrl | |
| 64 | * @param array $headers | |
| 65 | * @param string $fileName | |
| 66 | * @param GuzzleHttp\Psr7 $stream | |
| 67 | * | |
| 68 | * @return void | |
| 69 | */ | |
| 70 | protected function uploadToS3($signedUrl, $headers, $fileName, $stream) | |
| 71 | { | |
| 72 | $client = new GuzzleClient(); | |
| 73 | $request = new Request( | |
| 74 | 'PUT', | |
| 75 | $signedUrl, | |
| 76 | ['headers' => json_encode($headers)], | |
| 77 | new Psr7\MultipartStream( | |
| 78 | [ | |
| 79 | [ | |
| 80 | 'name' => $fileName, | |
| 81 | 'contents' => $stream, | |
| 82 | ], | |
| 83 | ] | |
| 84 | ) | |
| 85 | ); | |
| 86 | ||
| 87 | $client->send($request); | |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * Set the visibility for a file. | |
| 92 | * | |
| 93 | * @param string $path | |
| 94 | * @param string $visibility | |
| 95 | * | |
| 96 | * @return array|false file meta data | |
| 97 | */ | |
| 98 | public function setVisibility($path, $visibility) | |
| 99 | { | |
| 100 | return $this->client->request('POST', 'storage/set-visibility', compact('path', 'visibility')); | |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * Check whether a file exists. | |
| 105 | * | |
| 106 | * @param string $path | |
| 107 | * | |
| 108 | * @return bool | |
| 109 | */ | |
| 110 | public function has($path) | |
| 111 | { | |
| 112 | $data = $this->client->request('POST', 'storage/has', compact('path')); | |
| 113 | ||
| 114 | return $data['exists'] ?? false; | |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * Get all the meta data of a file or directory. | |
| 119 | * | |
| 120 | * @param string $path | |
| 121 | * | |
| 122 | * @return false|array | |
| 123 | */ | |
| 124 | public function getMetadata($path) | |
| 125 | { | |
| 126 | return $this->client->request('POST', 'storage/metadata', compact('path')); | |
| 127 | } | |
| 128 | ||
| 129 | /** | |
| 130 | * Destroy the file in storage. | |
| 131 | * | |
| 132 | * @param string $path | |
| 133 | * | |
| 134 | * @return array | |
| 135 | */ | |
| 136 | public function destroyFile($path) | |
| 137 | { | |
| 138 | return $this->client->request('POST', 'storage/destroy', compact('path')); | |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * Destroy the directory in storage. | |
| 143 | * | |
| 144 | * @param string $directory | |
| 145 | * | |
| 146 | * @return array | |
| 147 | */ | |
| 148 | public function destroyDirectory($directory) | |
| 149 | { | |
| 150 | return $this->client->request('POST', 'storage/destroy-directory', compact('directory')); | |
| 151 | } | |
| 152 | ||
| 153 | /** | |
| 154 | * Create a directory in storage. | |
| 155 | * | |
| 156 | * @param string $directory | |
| 157 | * | |
| 158 | * @return array | |
| 159 | */ | |
| 160 | public function createDirectory($directory) | |
| 161 | { | |
| 162 | return $this->client->request('POST', 'storage/create-directory', compact('directory')); | |
| 163 | } | |
| 164 | } | |