ReviewOS

also looking at this

meemalabs/meema-client-php

Storage Class

#59
Merged glennmichael123 wants to merge storage-class into main
3 files +175 -105
src/Models/Media.phpmodified+0-105
Changes to src/Models/Media.php
@@ -2,9 +2,6 @@
22
33namespace Meema\MeemaApi\Models;
44
5use GuzzleHttp\Client as GuzzleClient;
6use GuzzleHttp\Psr7;
7use GuzzleHttp\Psr7\Request;
85use Meema\MeemaApi\Client;
96use Meema\MeemaApi\Exceptions\InvalidFormatException;
107use Meema\MeemaApi\Response\Response;
@@ -179,69 +176,6 @@ class Media
179176 return $this->client->request('DELETE', "media/{$id}", ['media_id' => $id]);
180177 }
181178
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
245179 /**
246180 * Archive a media.
247181 *
@@ -473,45 +407,6 @@ class Media
473407 return $this->client->request('GET', "tags/{$id}/media");
474408 }
475409
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
515410 /**
516411 * Fetch child relations for this instance.
517412 *